-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworldEvents.js
More file actions
172 lines (152 loc) · 4.23 KB
/
worldEvents.js
File metadata and controls
172 lines (152 loc) · 4.23 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var GameUtil = require('./gameUtil')
var _ = require('lodash')
function WorldEvents(game){
this.game = game
this._added = []
this._removed = []
this.activeEvents = []
}
module.exports.WorldEvents = WorldEvents
module.exports.RainEvent = RainEvent
module.exports.DroughtEvent = DroughtEvent
WorldEvents.prototype.tick = function(){
if(this.activeEvents.length > 0){
var i = this.activeEvents.length-1
while(i >= 0){
this.activeEvents[i].tick()
i--
}
}
this._update()
}
WorldEvents.prototype._update = function(){
var self = this
this._added.forEach(function(e){
self.activeEvents.push(e)
})
this._removed.forEach(function(e){
var i = self.activeEvents.indexOf(e)
if(i > -1) return self.activeEvents.splice(i,1)
})
this._added = []
this._removed = []
}
WorldEvents.prototype.add = function(e){
this._added.push(e)
}
WorldEvents.prototype.remove = function(e){
this._removed.push(e)
}
function RainEvent(game){
this.game = game
this.age = 0
this.init()
console.log(this.center)
}
RainEvent.prototype.tick = function(){
this.life-=20
if(this.life > 0){
if( Math.random() > 0.3){
this.updateClouds()
var cloudCount = _.random(0, 5)
if(cloudCount > this.nonCloudNodes.length) cloudCount = this.nonCloudNodes.length
while(cloudCount > 0){
this.createCloud()
cloudCount--
}
}else{
this.nonCloudNodes.pop()
}
}else{
if(this.cloudNodes.length > 0){
this.cloudNodes.pop()
}else{
console.log("DEAD")
this.game.worldEvents.remove(this)
}
}
}
RainEvent.prototype.init = function(){
this.life = _.random(15, 45)
var startY = _.random(0, this.game.world.length-1)
var startX = _.random(0, this.game.world[0].length-1)
this.center = this.game.world[startY][startX]
this.size = _.random(2, 10)
this.nonCloudNodes = GameUtil.getNeighborsScale(this.game.world, startX, startY, this.size)
this.nonCloudNodes = _.shuffle(this.nonCloudNodes)
this.cloudNodes = []
this.life += (this.size * 8) * 0.25
this.life = ~~this.life
}
RainEvent.prototype.updateClouds = function(){
var self = this
this.cloudNodes.forEach(function(cn){
cn.eventData.rain.age++
self.totalAge++
var rain = cn.eventData.rain
if(rain.power + rain.age < 50) {
cn.vegetation.density += 3 + Math.floor(Math.random() * rain.power)
} else {
cn.vegetation.desity--
}
})
}
RainEvent.prototype.createCloud = function(){
if(this.nonCloudNodes.length === 0) return
var c = this.nonCloudNodes.pop()
c.eventData.rain.active = true
c.eventData.rain.power = _.random(10,50)
this.cloudNodes.push(c)
}
RainEvent.prototype.selectNextTile = function(){
var t = _.random(0, this.edgeNodes)
}
function DroughtEvent(game){
this.game = game
this.age = 0
this.init()
console.log(this.center)
}
DroughtEvent.prototype.tick = function(){
this.life--
var i = this.livingNodes.length-1
while(i >= 0){
var n = this.livingNodes[i]
n.age++
}
}
/*
function* createSpiral(arr, sx, sy){
// (di, dj) is a vector - direction in which we move right now
var di = 1;
var dj = 0;
// length of current segment
var segment_length = 1;
// current position (i, j) and how much of current segment we passed
var i = sx;
var j = sy;
var segment_passed = 0;
var coords = [0,0]
for (var k = 0; k < arr.length * arr.length; ++k) {
// make a step, add 'direction' vector (di, dj) to current position (i, j)
i += di;
j += dj;
coords[0] = i
coords[1] = j
++segment_passed;
if (segment_passed == segment_length) {
// done with current segment
segment_passed = 0;
// 'rotate' directions
var buffer = di;
di = -dj;
dj = buffer;
// increase segment length if necessary
if (dj === 0) {
++segment_length;
}
}
yield coords
}
}
*/