Completed
Push — master ( 6e8dba...bda2ec )
by Andres
30s
created

angular.controller(ꞌct_reactionsꞌ)   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
nc 4
dl 0
loc 12
rs 9.2
nop 2
1
/**
2
 reactions
3
 Component that handles reactions and molecules.
4
5
 @namespace Components
6
 */
7
'use strict';
8
9
angular.module('game').component('reactions', {
10
  templateUrl: 'views/reactions.html',
11
  controller:  'ct_reactions',
12
  controllerAs: 'ct'
13
});
14
15
angular.module('game').controller('ct_reactions', ['state', 'data', 'visibility', 'util', 'format', 'reaction',
16
function (state, data, visibility, util, format, reactionService) {
17
  let ct = this;
18
  ct.state = state;
19
  ct.data = data;
20
  ct.util = util;
21
  ct.format = format;
22
23
  function update(player) {
24
    for(let slot of player.element_slots){
25
      for (let reaction of slot.reactions) {
26
        if (!reaction.active) {
27
          continue;
28
        }
29
        reactionService.react(numberToReact(player, reaction.reaction), reaction.reaction, player);
30
      }
31
    }
32
  }
33
34
  function numberToReact(player, reaction) {
35
    let power = ct.reactionPower(player);
36
    let number = power;
37
    for(let resource in reaction.reactants){
38
      number = Math.min(number, player.resources[resource].number);
39
    }
40
    return number;
41
  }
42
43
  /* Calculates the reaction power based on the reaction upgrades */
44
  ct.reactionPower = function(player) {
45
    let level = player.global_upgrades.reaction_bandwidth;
46
    let upgrade = data.global_upgrades.reaction_bandwidth;
47
    let basePower = upgrade.power;
48
    let polynomial = upgrade.power_poly;
49
    return basePower * Math.floor(Math.pow(level, polynomial));
50
  };
51
52
  /* Calculates the number of reaction slots based on the reaction upgrades */
53
  ct.reactionSlots = function (player) {
54
    let level = player.global_upgrades.reaction_slots;
55
    let upgrade = data.global_upgrades.reaction_slots;
56
    let basePower = upgrade.power;
57
    let multiplier = upgrade.power_mult;
58
    return basePower * Math.floor(multiplier * level);
59
  };
60
61
  ct.reactionSize = function (player) {
62
    let size = 0;
63
    for(let slot of player.element_slots){
64
      size += slot.reactions.length;
65
    }
66
    return size;
67
  };
68
69
  /* Adds a new reaction to the player list */
70
  ct.addReaction = function (player, slot, key) {
71
    if(ct.reactionSize(player) >= ct.reactionSlots(player)){
72
      return;
73
    }
74
    let reaction = data.reactions[key];
75
    slot.reactions.push({
76
      active: false,
77
      reaction: angular.copy(reaction)
78
    });
79
  };
80
81
  ct.removeReaction = function (slot, item) {
82
    for(let i = 0; i < slot.reactions.length; i++){
83
      if(slot.reactions[i] === item){
84
        slot.reactions.splice(i, 1);
85
      }
86
    }
87
  };
88
89
  ct.visibleReactions = function(slot) {
90
    return slot.reactions;
91
  };
92
93
  ct.availableReactions = function(slot) {
94
    return visibility.visible(data.reactions, isReactionAvailable, slot.element);
95
  };
96
97
  function isReactionAvailable(entry, currentElement) {
98
    let available = true;
99
    let reaction = data.reactions[entry];
100
    for(let resource in reaction.reactant){
101
      available = available && state.player.resources[resource].unlocked;
102
    }
103
    // Workaround to reuse the visibility function. It expects an object with the
104
    // reaction inside
105
    let reactionObject = {reaction:reaction};
106
    return available && isReactionVisible(reactionObject, currentElement);
107
  }
108
109
  function isReactionVisible(entry, currentElement) {
110
    let reaction = entry.reaction;
111
    if(reaction.elements.length === 0){
112
      return true;
113
    }
114
    for(let element of reaction.elements){
115
      if(element === currentElement){
116
        return true;
117
      }
118
    }
119
    return false;
120
  }
121
122
  state.registerUpdate('reactions', update);
123
}]);
124