Incremental Game in AngularJS
by kevmus
HTML
<body ng-app="myApp">
<div ng-controller="IncCtrl">
<button ng-click="click('tin')">{{items['tin'] tin}}</button>
<button ng-click="buyClicker('tin)">{{clickers['tin']}} Miners (costs {{clickerPrice('tin')}} tin)</button>
{{clickers['tin']}}
<hr>
</div>
</body>
JavaScript
var myApp = angular.module('myApp', []);
myApp.controller('IncCtrl',['$scope','$document','$interval',function($scope,$document,$interval) {
$scope.items = {};
$scope.clickers = {};
$scope.click = function(item) {
$scope.items[item] = ($scope.items[item] || 0) + 1;
}
$scope.clickerPrice = function(item){
return (10 * Math.pow(1.1,$scope.clickers[item] || 0)).toFixed();
}
$scope.buyClicker = function(item) {
if ($scope.items[item] || 0 >= $scope.clickerPrice(item)) {
$scope.items[item] || 0 -= $scope.clickerPrice(item);
$scope.clickers[item] = ($scope.clickers[item || 0]) + 1;
}
}
function update() {
$scope.items['tim'] += ($scope.clickers['tin'] || 0);
};
$document.ready(function(){
$interval(update,1000);
});
}]);