Completed
Push — master ( 47ce1a...1dc86b )
by Andres
32s
created

mechanics.js ➔ ... ➔ ct.canBuyGlobalUpgrade   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 3
dl 0
loc 11
rs 9.4285
nop 1
1
/**
2
 mechanics
3
 Component that handles the logic of purchasing new mechancs, plus global upgrades.
4
5
 @namespace Components
6
 */
7
'use strict';
8
9
angular.module('game').component('mechanics', {
10
  templateUrl: 'views/mechanics.html',
11
  controller: ['state', 'visibility', 'data', mechanics
12
  ],
13
  controllerAs: 'ct'
14
});
15
16
function mechanics (state, visibility, data) {
17
  let ct = this;
18
  ct.state = state;
19
  ct.data = data;
20
21
  ct.unlockMechanic = function(player, mechanic) {
22
    if(ct.unlockedMechanics(player) >= player.mechanic_slots){
23
      return;
24
    }
25
    player.mechanics[mechanic] = true;
26
  };
27
28
  ct.unlockedMechanics = function(player) {
29
    let count = 0;
30
    for(let key in player.mechanics){
31
      if(player.mechanics[key]){
32
        count++;
33
      }
34
    }
35
    return count;
36
  };
37
38
  /* Global upgrades are non-resource specific, repeatable upgrades */
39
  ct.buyGlobalUpgrade = function (name) {
40
    if (!ct.canBuyGlobalUpgrade(name)) {
41
      return;
42
    }
43
44
    let up = data.global_upgrades[name];
45
    for (let currency in up.price) {
46
      let value = up.price[currency] * ct.priceMultiplier(name);
47
      state.player.resources[currency].number -= value;
48
    }
49
50
    state.player.global_upgrades[name]++;
51
  };
52
53
  ct.canBuyGlobalUpgrade = function (name) {
54
    let up = data.global_upgrades[name];
55
    for (let currency in up.price) {
56
      let value = up.price[currency] * ct.priceMultiplier(name);
57
      if (state.player.resources[currency].number < value) {
58
        return false;
59
      }
60
    }
61
62
    return true;
63
  };
64
65
  ct.priceMultiplier = function (name) {
66
    let level = state.player.global_upgrades[name];
67
    return Math.ceil(Math.pow(data.global_upgrades[name].price_exp, level));
68
  };
69
70
  ct.visibleMechanicUpgrades = function() {
71
    return visibility.visible(data.global_upgrades, isMechanicUpgradeVisible);
72
  };
73
74
  function isMechanicUpgradeVisible(name) {
75
    let mechanic = data.global_upgrades[name].mechanic;
76
    return state.player.mechanics[mechanic];
77
  }
78
}
79