-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfungi-slot.js
86 lines (77 loc) · 2.13 KB
/
fungi-slot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Rudimentary slot object that knows nothing about the world around it.
Still, it can populate and advance itself, given that it receives
appropriate data from its parent.
Provides functionalities required by the board and the inventory.
*/
class Slot {
constructor () {
this.type = null
this.stage = null
}
// Debug mode of population, not used anywhere
populateFixed (_type, _stage) {
this.type = _type
this.stage = _stage
}
// Proper mode of population - requires data about population conditions
populateRandom (mushroomTable, tresholdTable, frequencySum) {
let seed = Math.random() * frequencySum
this.type = null
this.stage = null
for (let treshold of tresholdTable) {
if (treshold > seed) {
this.type = mushroomTable[tresholdTable.indexOf(treshold)].name
this.type == 'rock' ? this.stage = 3 : this.stage = 1
return
}
}
}
/*
Three functions below (each named with a single verb) are used to control
the slot and are usually called by its parent (board or inventory).
*/
pickUp () {
let returnType = this.type
let returnStage = this.stage
this.empty()
return [returnType, returnStage]
}
empty () {
this.type = null
this.stage = null
}
advance (mushroomTable, tresholdTable, frequencySum, repopulationFactor) {
if (this.type != null && this.type != 'rock') {
if (this.stage < 3) {
this.stage += 1
}
else {
this.empty()
}
}
else if (this.type == 'rock') {
if (this.stage > 1) {
this.stage -= 1
}
else {
this.type = null
this.stage = null
}
}
else if (this.type == null) {
if (repopulationFactor > Math.random()) {
this.populateRandom(mushroomTable, tresholdTable, frequencySum)
}
}
}
/*
Two helper functions used by the Game (board) to determine slot pickability
*/
getPickupCost (baseCost, pickupPenalty, pickupPenaltyExponent) {
return baseCost + pickupPenalty * Math.pow(this.stage-1 , pickupPenaltyExponent)
}
isPickable () {
return (this.type != 'rock' && this.type != null)
}
}