/
zeds.js
306 lines (252 loc) · 8.54 KB
/
zeds.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
var Zeds = function() {
var z = this; // reference to the Game
z.canvas = document.getElementById('board');
z.ctx = z.canvas.getContext("2d");
z.canvas.width = 600;
z.canvas.height = 400;
z.FPS = 2;
var Agent = function(x,y) {
this.x = x;
this.y = y;
this.width = this.height = 10;
this.hunger = 100;
this.hunger_decay = 1;
this.strength = 2;
this.health = 10;
this.movement_speed = 20;
this.attack_speed = 10;
this.intelligence = 1;
this.color = "#eee";
this.next_action = this.no_action;
};
Agent.prototype.draw = function() {
var half_w = this.width / 2;
var half_h = this.height / 2;
z.ctx.fillStyle = this.color;
z.ctx.fillRect(this.x-half_w, this.y-half_h,
this.width, this.height);
};
Agent.prototype.distance = function(otherAgent) {
var x_d, y_d;
x_d = (this.x - otherAgent.x);
x_d = x_d * x_d;
y_d = (this.y- otherAgent.y);
y_d = y_d * y_d;
var d = Math.abs(x_d + y_d);
return Math.sqrt(d);
};
Agent.prototype.take_turn = function() {
this.apply_hunger();
this.setup_next_action();
};
Agent.prototype.apply_hunger = function() {
this.hunger -= this.hunger_decay;
};
Agent.prototype.setup_next_action = function() {
if (this.hunger <= 0) {
this.next_action = this.death;
}
};
Agent.prototype.no_action = function() {
return this;
};
Agent.prototype.death = function() {
return new Dead(this);
}
Agent.prototype.remove = function() {
return;
}
var Squirrel = function(x,y) {
var squirrel = this;
Agent.call(this,x,y);
this.color = "#994";
this.setup_next_action = function() {
//Squirrels are pretty food driven...
// ARE YOU GON DIE??
if (this.hunger <= 0) {
return this.next_action = this.death;
}
// CAN YOU EAT???
var food = z.mushrooms();
var touching_food = _.filter(food, function(f) {
var touch_distance = squirrel.width;
return touch_distance >= squirrel.distance(f);
});
if (touching_food.length > 0) {
// return a function that will eat the food
return this.next_action = this.next_eat(touching_food[0]);
}
// WHERE IS NEAREST FOOD???
var sorted_foods = _.sortBy(food, function(f) {
return squirrel.distance(f);
});
if (sorted_foods.length > 0) {
// return a function to move to the nearest food
return this.next_action = this.move_to(food[0]);
}
else {
return this.next_action = this.no_action;
}
};
this.next_eat = function(food_agent) {
// eat the food, removing it from the game
return function() {
if (food_agent.health > 0) {
food_agent.health = -100;
this.hunger += 50;
}
return squirrel;
};
};
this.move_to = function(food_agent) {
// returns a function that updates the position of
// this agent, moving it as close as possible to
// the food agent
var new_x,new_y;
function vector_to(food) {
return {x : food.x - squirrel.x,
y : food.y - squirrel.y};
}
function scalar_multiply(vector,scalar) {
var v = {};
v.x = vector.x * scalar;
v.y = vector.y * scalar;
return v;
}
function get_magnitude(vector){
var origin = new Agent(0,0);
var to = new Agent(vector.x,vector.y);
return origin.distance(to);
//lol
}
function get_unit_vector(vector) {
var v = {};
var m = get_magnitude(vector);
m = 1/m;
return scalar_multiply(vector, m);
}
function apply_vector(agent, movement) {
agent.x += movement.x;
agent.y += movement.y;
}
var vector_to_food = vector_to(food_agent);
var normalized = get_unit_vector(vector_to_food);
//scale magnitude of movement back so you don't overshoot
if (get_magnitude(vector_to_food) > this.movement_speed){
var movement_distance = this.movement_speed;
}
else {
var movement_distance = get_magnitude(vector_to_food);
}
var movement = scalar_multiply(normalized, movement_distance);
return function() {
//actually update position
apply_vector(squirrel, movement);
return squirrel;
};
};
};
Squirrel.prototype = new Agent();
var Zombie = function(x,y) {
Agent.call(this,x,y);
this.color = "#488";
};
Zombie.prototype = new Agent();
var Mushroom = function(x,y) {
mushroom = this;
Agent.call(this,x,y);
this.width = this.height = 6;
this.hunger_decay = -20;
var default_mushroom_color = "#ddd";
this.color = default_mushroom_color;
this.setup_next_action = function() {
if (this.hunger < -10 || this.health < -10) {
return this.next_action = this.remove;
}
if (this.hunger < 0 || this.health < 0) {
return this.next_action = this.death;
}
if (this.hunger < 30) {
this.color = "#f00";
}
else {
this.color = default_mushroom_color;
}
if (this.hunger >= 200) {
return this.next_action = this.reproduce;
}
else {
return this.next_action = this.no_action;
}
};
this.reproduce = function() {
this.hunger = 1;
return this; //neuter
var x = Math.random() * z.canvas.width;
var y = Math.random() * z.canvas.height;
var m = new Mushroom(x,y);
// prevent new mushroom from getting added
// to the game if it's near another
var there_is_nearby_fungus = false;
var fungus_agents = z.mushrooms();
there_is_nearby_fungus = _.any(fungus_agents, function(fung){
var too_close = 100;
dist = m.distance(fung);
return dist < too_close;
});
if(!there_is_nearby_fungus){
z.agents.push(m);
}
return this;
};
};
Mushroom.prototype = new Agent();
var Dead = function(original) {
console.log(original + " died!");
this.x = original.x;
this.y = original.y;
this.width = original.width;
this.height = original.height;
this.death = function () {
// delete
}
this.color = "#444";
};
Dead.prototype = new Agent();
z.draw_environment = function() {
z.ctx.fillStyle = "#332";
z.ctx.fillRect(0,0,z.canvas.width,z.canvas.height);
};
z.mushrooms = function() {
return _.filter(z.agents, function(a){
return a instanceof Mushroom;
});
};
z.game_loop = function() {
z.ctx.clearRect(0,0,z.canvas.width,z.canvas.height);
z.draw_environment();
for(var i in z.agents) {
z.agents[i].draw();
z.agents[i].take_turn();
z.agents[i] = z.agents[i].next_action();
}
// garbage collect
z.agents = _.filter(z.agents,function(a){ return a; });
};
z.start = function() {
//background
z.agents = [];
z.agents.push(new Mushroom(30,30));
z.agents.push(new Mushroom(200,200));
z.agents.push(new Mushroom(30,250));
z.agents.push(new Mushroom(300,250));
z.agents.push(new Squirrel(30,100));
//z.agents.push(new Squirrel(10,10));
//z.agents.push(new Zombie(300,100));
z.loop = setInterval(z.game_loop, 1000 / z.FPS);
};
z.stop = function() {
clearInterval(z.loop);
};
return z;
};