-
Notifications
You must be signed in to change notification settings - Fork 3
/
cell.js
345 lines (284 loc) · 8.92 KB
/
cell.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// *** cell ***
// --- inherits from game_object
// spec:
// game_object spec +
// String state = "alive" or "dead" or "infected" or "active"
// mutation : Mutation_obj so cell can get current mutation color
var cell = function(p, spec) {
// --- defaults ---
// was 40
spec.width = spec.width || 25;
spec.height = spec.height || 25;
// obj to return
var obj = game_object(p, spec);
obj.get_type = function() {
return "cell";
};
// --- private variables ---
// images
var cell_image = random_from(
image_manager.get_images("infectable_cell")).image;
var burst_anim = animated_image("burst", {});
var infect_anim = animated_image("cell_infect", {});
// state can be "alive", "infected", "active", or "dead"
var state = spec.state || "alive";
// random initial angle
var arrow_angle = p.random(-p.PI/2, p.PI/2);
// random dir (1 or -1)
var arrow_dir = p.random() >= 0.5 ? 1 : -1;
// indicates whether has been hit by an arrow and is being targeted
//var is_targeted = false;
// holds an antibody that is attached to the cell, or null
var anti = null;
// --- public methods ---
// implementing game_object interface
// update is different depending on state
obj.update = function() {
obj.move();
if (state === "alive") {
// just chill
}
else if (state === "infected") {
// still chill
}
else if (state === "active") {
// spin the arrow
rotateArrow();
}
else if (state === "dying") {
if (burst_anim.is_finished()) {
obj.set_state("dead");
}
}
/*
else if (state === "dead") {
}
*/
};
// draw makes a cell with a different color depending on state
// (circle for now)
obj.draw = function() {
var pos = obj.get_pos();
p.shapeMode(obj.get_mode());
p.noStroke();
if (state === "dying") {
// draw frame and advance anim
p.image(burst_anim.get_frame(), pos.x, pos.y,
obj.get_width(), obj.get_height());
// skip the rest of the method
return;
}
if (state === "alive") {
p.fill(p.color(200, 50, 50));
}
else {
if (state === "infected") {
p.fill(obj.get_color());
// draw after drawing the image
}
else if (state === "active") {
drawArrow();
// Draw a separate circle for the red outline
// so that we can more accurately fill in the cell
// red outline for now
p.stroke(255, 0, 0);
p.strokeWeight(4);
p.ellipse(pos.x, pos.y, obj.get_width(), obj.get_height());
p.noStroke();
p.fill(obj.get_color());
}
//if (infect_anim.is_finished() || obj.is_illustration()) {
p.ellipse(pos.x, pos.y, obj.get_width() * 4/5, obj.get_height() * 4/5);
//}
}
p.imageMode(obj.get_mode());
p.image(cell_image, pos.x, pos.y, obj.get_width(), obj.get_height());
if (state === "infected" || state === "active") {
// draw after the image
if (!infect_anim.is_finished() && !obj.is_illustration()) {
// draw frame and advance anim
p.image(infect_anim.get_frame(), pos.x, pos.y,
obj.get_width(), obj.get_height());
//p.fill(obj.get_color());
}
}
/*
else if (state === "dead") {
p.fill(0);
p.ellipse(pos.x, pos.y,
obj.get_width(), obj.get_height());
}
*/
};
// to be used in illustration only
obj.set_image = function(i) {
cell_image = i;
};
obj.get_image = function() {
return cell_image;
};
obj.is_dead = function() {
//console.log(burst_anim.is_finished());
return state === "dead";
};
obj.stop_animation = function() {
burst_anim.pause();
infect_anim.pause();
};
obj.resume_animation = function() {
burst_anim.start();
infect_anim.start();
};
obj.set_state = function(s) {
if (s === "infected") {
infect_anim.start();
}
state = s;
};
obj.set_antibody = function(a) {
anti = a;
anti.attach(obj);
};
obj.has_antibody = function() {
return (anti !== null);
};
obj.get_state = function() {
return state;
};
obj.die = function() {
if (anti) {
anti.die();
}
obj.set_state("dying");
burst_anim.start();
};
// explodes this cell if it is active
obj.fire = function() {
if (state === "active") {
// Make sounds
sounds.play_sound("cell_fire");
obj.die();
var pos = obj.get_pos();
var ang = arrow_angle;
// use width cuz it's a circle
var r = obj.get_width()/2;
// gen particles at edge of cell for now
var x = r*p.cos(ang) + pos.x;
var y = r*p.sin(ang) + pos.y;
var num_particles = get_num_particles();
// angle between all the shots
var range = p.PI/6;
var incr = range/num_particles;
var particles = [];
ang = arrow_angle - range/2;
// special case
if (num_particles === 1) {
ang = arrow_angle;
}
while (num_particles > 0) {
var new_vel = new p.PVector(p.cos(ang), p.sin(ang));
// mult by speed scalar
new_vel.mult(7);
// if we want to add velocity of cell
new_vel.add(obj.get_vel());
particles.push(particle(p, {
pos: new p.PVector(x, y),
vel: new_vel,
mutation_info: obj.get_mutation_info()
}));
num_particles--;
ang += incr;
}
return particles;
}
throw "Can't fire on "+state+" cell!";
};
// override for circular object
obj.calc_radius = function() {
return obj.get_width()/2;
};
obj.set_radius(obj.calc_radius());
// --- private functions ---
// rotates according to arrow_dir
// switches direction at certain angles
var rotateArrow = function() {
if (arrow_angle > p.PI/2
|| arrow_angle < -p.PI/2) {
arrow_dir = (2-arrow_dir)-2;
}
arrow_angle += p.radians(5+obj.get_level()/2)*arrow_dir * g_speed_factor;
};
var drawArrow = function() {
p.pushMatrix();
var pos = obj.get_pos();
var w = obj.get_width();
// move to center of circle
p.translate(pos.x, pos.y);
// rotate first
p.rotate(arrow_angle);
// move out to right edge of circle
p.translate(w/2, 0);
// red outline for now
p.stroke(255, 0, 0);
p.strokeWeight(2);
p.fill(obj.get_color());
var x1 = w/2;
// draw facing out to right
p.beginShape();
p.vertex(0, -5);
p.vertex(x1, -5);
p.vertex(x1, -10);
p.vertex(x1+15, 0);
p.vertex(x1, 10);
p.vertex(x1, 5);
p.vertex(0, 5);
p.endShape();
p.popMatrix();
// Draw Dots if easy
if (GLOBAL_is_easy) {
var pos = obj.get_pos();
var ang = arrow_angle;
// use width cuz it's a circle
var r = obj.get_width()/2;
// gen particles at edge of cell for now
var x = r*p.cos(ang) + pos.x;
var y = r*p.sin(ang) + pos.y;
var num_particles = get_num_particles();
// angle between all the shots
var range = p.PI/6;
var incr = range/num_particles;
ang = arrow_angle - range/2;
// special case
if (num_particles === 1) {
ang = arrow_angle;
}
var num_dots = 5;
while (num_particles > 0) {
var offset = 4;
p.fill(153);
p.noStroke();
for (var i = 0; i < num_dots; i++) {
x = r*p.cos(ang)*offset + pos.x ;
y = r*p.sin(ang)*offset + pos.y ;
p.ellipse(x, y, 2, 2);
offset += 2;
}
num_particles--;
ang += incr;
}
}
};
var get_num_particles = function() {
var extra_particles = 0;
var mut_info = obj.get_mutation_info();
/*
for_each(mut_info.abilities, function(a) {
if (a === "extra_particle") {
extra_particles++;
}
});
*/
return mut_info.particles + extra_particles;
};
return obj;
};