-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathantibody.js
86 lines (67 loc) · 2.25 KB
/
antibody.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
// --- inherits from seeker.js
// spec:
// game_object spec +
// speed = how fast the tkiller approaches the target
// target = cell to move towards
var antibody = function(p, spec) {
// --- defaults ---
spec.width = spec.width || 11;
spec.height = spec.height || 7;
spec.speed = (spec.speed || 4) * g_speed_factor;
spec.no_target_speed = (spec.no_target_speed || 2) * g_speed_factor;
//spec.vel = random_vel();
// obj to return
var obj = seeker(p, spec);
obj.get_type = function() {
return "antibody";
};
// --- private variables ---
// flag denoting whether the antibody is attached to a cell
var attached = false;
// --- public methods ---
// should be called when an antibody attaches to a cell
// tar will be the cell that it is attaching to (may not be its target)
obj.attach = function(tar) {
attached = true;
obj.face_target(tar);
// scoot it out just a bit
obj.get_vel().mult(-1);
obj.move();
//obj.set_target(null);
};
// implementing game_object interface
obj.my_update = function() {
if (!attached) {
obj.set_speed(obj.get_level() / 4 + 1)
obj.move();
// don't want two antibodies attacking one cell
var tar = obj.get_target();
if (tar && (tar.has_antibody()
// also don't want them to keep target
// if the target changes level
|| tar.get_level() !== obj.get_level()
// or if it starts to die
|| tar.get_state() === "dying")) {
obj.set_target(null);
}
}
};
// should point towards target
// Y-shaped (top of Y is front)
obj.draw = function() {
p.pushMatrix();
var pos = obj.get_pos();
var w = obj.get_width();
var h = obj.get_height();
p.shapeMode(obj.mode);
p.translate(pos.x, pos.y);
p.rotate(obj.get_target_angle());
p.stroke(obj.get_color());
p.strokeWeight(2);
p.line(-w, 0, w/3, 0);
p.line(w/3, 0, w, h/2);
p.line(w/3, 0, w, -h/2);
p.popMatrix();
};
return obj;
}