Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var display = {
  3.     updateValue : function(container, value) {
  4.         // Make it a nice looking number
  5.         $('#'+container+' .infoValue').html(value.toFixed(2));
  6.     },
  7.     button : function(name, display) {
  8.         if(display == true) {
  9.             $('#upgrade-'+name).prop("disabled",false);
  10.         } else {
  11.             $('#upgrade-'+name).prop("disabled",true);
  12.         }
  13.     }
  14. }
  15.  
  16. var upgrades = {
  17.     upgradeDetails : [
  18.                         //[0]Name - [1]Owned - [2]Added cps - [3]Cost
  19.                         ['Type 1', 0, 1,  20],
  20.                         ['Type 2', 0, 5,  95],
  21.                         ['Type 3', 0, 50, 9100]
  22.                      ],
  23.  
  24.     construct : function() {
  25.     },
  26.  
  27.     buy : function(item) {
  28.         // Purchase an upgrade, upgrading the money per second
  29.  
  30.         if(dollars.spend(this.upgradeDetails[item][3])) {
  31.             // We have enough money? Great! Buy it.
  32.             this.upgradeDetails[item][1] += 1;
  33.             dollars.increaseRate(this.upgradeDetails[item][2]);
  34.         } else {
  35.             // Can't afford it :(
  36.             return false;
  37.         }
  38.     },
  39.  
  40.     affordIt : function () {
  41.         // Update disabled state of buttons
  42.  
  43.         for(i=0;i<this.upgradeDetails.length; i++) {
  44.             if(dollars.owned >= this.upgradeDetails[i][3]) {
  45.                 display.button(i, true);
  46.             } else {
  47.                 display.button(i, false);
  48.             }
  49.         }
  50.     }
  51.  
  52.  
  53. }
  54.  
  55. var dollars = {
  56.     owned : 0,
  57.     rate:0,
  58.     clickValue:0,
  59.  
  60.     construct : function() {
  61.         dollars.setOwned(0);
  62.         this.clickValue = 1;
  63.         display.updateValue('moneyPerSec', this.rate);
  64.     },
  65.  
  66.     setOwned : function(value){
  67.         // Set the total cash
  68.         this.owned = value;
  69.     },
  70.  
  71.     spend : function(value) {
  72.         // Spend some moneys
  73.         if(value > this.owned) {
  74.             return false;
  75.         } else {
  76.             this.owned -= value;
  77.             upgrades.affordIt();
  78.             this.refresh();
  79.             return true;
  80.         }
  81.     },
  82.  
  83.     increaseRate : function(value) {
  84.         // Increase the money per second rate
  85.         this.rate += value;
  86.         display.updateValue('moneyPerSec', this.rate);
  87.     },
  88.  
  89.     click : function() {
  90.         // Click handler
  91.         this.owned += this.clickValue;
  92.         display.updateValue('money', this.owned);
  93.     },
  94.  
  95.     increment : function() {
  96.         // Timed event
  97.         this.owned += this.rate;
  98.         display.updateValue('money', this.owned);
  99.     },
  100.  
  101.     refresh : function() {
  102.         // Force refresh the cash value
  103.         display.updateValue('money', this.owned);
  104.     }
  105. }
  106.  
  107. dollars.construct();
  108. upgrades.construct();
  109.  
  110. window.setInterval(function(){
  111.     // Fires every second
  112.     dollars.increment();
  113.     upgrades.affordIt();
  114. }, 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement