-
Notifications
You must be signed in to change notification settings - Fork 3
/
game_object.js
276 lines (221 loc) · 6.67 KB
/
game_object.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
// *** game_object ***
// spec:
// mode = p.CENTER or p.CORNERS (constants from processing)
// - describes how the coords work, set by p.shapeMode()
// p.PVector pos = initial position (x,y)
// float width = width of the entire object
// float height = height of the entire object
// float radius = circle collision radius (will be auto-calced)
// p.PVector vel = initial velocity
// p.PVector accel = initial acceleration
// mutation_info = object with level, num_particles and color props
// illustration = boolean that says whether or not to draw animations
var game_object = function (p, spec) {
// object to return
var obj = {};
// should be overridden by subtypes
obj.get_type = function() {
return "game_object";
};
obj.DEFAULT_SCROLL_DIST = -1.2;
// --- private variables ---
var mode = spec.mode || p.CENTER;
var pos = spec.pos || new p.PVector(0, 0);
var width = spec.width || 0;
var height = spec.height || 0;
var vel = spec.vel || new p.PVector(0, 0);
var accel = spec.accel || new p.PVector(0, 0);
// used for circle collision testing
var radius = spec.radius // default set at bottom of file
// used for mutation
var mutation_info = spec.mutation_info ||
{ level: -1, color: p.color(0, 0, 0), particles: 0 };
var illustration = spec.illustration || false;
var alive = true;
// --- public methods ---
// all game_objects must implement this interface:
// void update() - called each frame to move/update objects
// void draw() - called each frame to display the object
// void scroll() - moves the object left by a certain amount
// update moves obj by default
obj.update = function() { obj.move(); };
// draw does nothing by default
obj.draw = function() {};
// by default returns the negated alive variable
obj.is_dead = function() { return !alive; };
// Generic methods for all game_objects:
// returns true if the object is completely offscreen
// i.e. checks pos + dimensions
obj.is_offscreen = function() {
var left_edge = pos.x - width/2;
var right_edge = pos.x + width/2;
var top_edge = pos.y - height/2;
var btm_edge = pos.y + height/2;
return (left_edge > p.width
|| right_edge < 0
|| top_edge > p.height
|| btm_edge < 0);
};
// Returns if the obj is off the right of the screen
obj.is_off_right = function() {
var left_edge = pos.x - width/2;
return left_edge > p.width;
};
// Bounces the object off a wall, if it is at one
obj.bounce = function() {
var left_edge = pos.x - width;
var right_edge = pos.x + width;
var top_edge = pos.y - height;
var btm_edge = pos.y + height;
if (top_edge <= 0 && vel.y < 0) {
vel.y = -vel.y;
}
if (btm_edge >= p.height && vel.y > 0) {
vel.y = -vel.y;
}
if (left_edge <= 0 && vel.x < 0) {
vel.x = -vel.x;
}
if (right_edge >= p.width && vel.x > 0) {
vel.x = -vel.x;
}
};
// reverses the y velocity
// used for bouncing vertically
obj.reverse_y = function() {
var vel = obj.get_vel();
obj.set_vel(new p.PVector(vel.x, -vel.y));
};
// reverses the x velocity
// used for bouncing horizontally
obj.reverse_x = function() {
var vel = obj.get_vel();
obj.set_vel(new p.PVector(- vel.x, vel.y));
};
// updates the position according to accel and vel
// Bounces off walls
obj.move = function() {
vel.add(accel);
pos.add(vel);
};
// stops the object in its tracks by zeroing out vel
obj.stop = function() {
vel = new p.PVector(0, 0);
};
// Scrolls the obj a specified distance left or right (positive is right)
obj.scroll = function(scroll_factor) {
var scroll_x = obj.get_scroll_dist();
if (scroll_x === null) {
scroll_x = obj.DEFAULT_SCROLL_DIST;
}
pos.add(new p.PVector(scroll_x*scroll_factor, 0));
};
obj.die = function() {
alive = false;
};
obj.is_illustration = function() {
return illustration;
};
// --- setters ---
obj.set_illustration = function(i) {
illustration = i;
};
obj.set_pos = function(new_pos) {
pos = new_pos;
};
obj.set_accel = function(new_accel) {
accel = new_accel;
};
obj.set_vel = function(new_vel) {
vel = new_vel;
};
// should only be used once, just for circular objects
obj.set_radius = function(new_radius) {
radius = new_radius;
};
// Should only be used for collisions
obj.set_pos = function(posn) {
pos = posn;
};
obj.set_mutation_info = function(m) {
mutation_info = m;
};
// --- getters ---
// Override to set different scroll dist
// or leave as null to use default
obj.get_scroll_dist = function() {
return null;
};
obj.get_pos = function() {
return pos;
};
obj.get_left = function() {
return pos.x - (width / 2);
};
obj.get_top = function() {
return pos.y - (height / 2);
};
obj.get_right = function() {
return pos.x + (width / 2);
};
obj.get_bottom = function() {
return pos.y + (height / 2);
};
// returns the x offset for rectangle collisions
// should be overridden for rectangular objects
obj.get_x_offset = function() {
return 0;
};
// returns the y offset for rectangle collisions
// should be overridden for rectangular objects
obj.get_y_offset = function() {
return 0;
};
obj.get_width = function() {
return width;
};
obj.get_height = function() {
return height;
};
obj.get_mode = function() {
return mode;
};
obj.get_vel = function() {
return vel;
};
obj.get_accel = function() {
return accel;
};
obj.get_radius = function() {
return radius;
};
obj.get_mutation_info = function() {
return mutation_info;
};
obj.get_color = function() {
return mutation_info.color;
};
obj.get_level = function() {
return mutation_info.level;
};
obj.is = function(type) {
return obj.get_type() === type;
};
obj.to_string = function() {
return obj.get_type()+" ("+pos.x+", "+pos.y+")";
};
// draws the collision circle as an overlay
obj.draw_circle = function() {
p.fill(255, 50);
p.noStroke();
p.shapeMode(p.CENTER);
p.ellipse(pos.x, pos.y, 2*radius, 2*radius);
};
// uses pythagorean theorem to calc radius of bounding circle
obj.calc_radius = function() {
return 0.5*p.sqrt(width*width+height*height);
};
// calc radius if not already set
radius = radius || obj.calc_radius();
return obj;
}