forked from vincent/machinejs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.js
86 lines (67 loc) · 1.91 KB
/
demo.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
// a little demo ecosystem that betrays my horrifically incomplete knowledge of plant biology
// the oak object - sucks up water, photosynthesises, grows
function Oak(landscape) {
this.state = null;
this.landscape = landscape;
this.height = 1;
this.energy = false;
this.oxygen = false;
this.water = 0;
this.sun = 0;
}
Oak.prototype = {
hasSun: function() { return this.sun > 0; },
hasWater: function() { return this.water > 0; },
};
Oak.states = {
idle: function() { },
grow: function() {
this.energy = false;
this.height += 1;
},
canGrow: function() { return this.energy; },
canMakeEnergy: function() {
return this.hasSun() && this.hasWater();
},
makeEnergy: function() {
this.sun -= 1;
this.water -= 1;
this.energy = true;
},
canPhotosynthesise: function() {
return this.hasSun() && this.hasWater();
},
canEmitOxygen: function() { return this.oxygen; },
emitOxygen: function() {
this.oxygen = false;
this.landscape.oxygenate();
},
gatherSun: function() { this.sun += 1; },
canGatherSun: function() { return this.landscape.isShining(); },
gatherWater: function() {
this.water += this.landscape.giveWater();
},
canGatherWater: function() { return this.landscape.hasWater(); },
};
// the landscape object - rains or shines
function Landscape() {
this.state = null;
this.groundwater = 0;
this.oxygen = 0;
}
Landscape.prototype = {
isShining: function() { return this.state.identifier == "shine"; },
hasWater: function() { return this.groundwater > 0; },
giveWater: function() {
this.groundwater -= 1;
return 1;
},
oxygenate: function() { this.oxygen += 1; },
};
Landscape.states = {
idle: function() { },
rain: function() { this.groundwater += 1; },
canRain: function() { return Math.random() > 0.5; },
shine: function() { }, // nothing to do - just a state
canShine: function() { return Math.random() > 0.1; },
};