-
Notifications
You must be signed in to change notification settings - Fork 0
/
Objects.js
263 lines (185 loc) · 5.25 KB
/
Objects.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
// cache pi for performance
var PI = Math.PI;
// Game objects are anything displayed on the screen to a user. Can be
// a player, NPC, goal, wall, item, etc
var GameObject = Class.create({
initialize: function(spec){
// allows objects to be called without providing an spec object
spec = spec || {};
// neutral, player, enemy, item
this.class = "neutral";
// positions to tell how to render
this.x = spec.x || 0;
this.y = spec.y || 0;
this.angle = spec.angle || PI/2; // defines an angle in radians
// Defines the inner circle to be used for collision detection with
// other game objects
this.hitRadius = spec.hitRadius || 0;
},
// a method to draw the object with their x-position and y-position
draw: function() {
},
// return object's x- and y-coordinates as a vector-like object
vector: function() {
return {x: this.x, y: this.y};
},
// shift the object's position
shift: function(x, y) {
this.x += x;
this.y += y;
}
});
// Characters are any moving game objects (such as the player and NPC's)
var Character = Class.create(GameObject, {
initialize: function($super, spec) {
$super(spec);
spec = spec || {};
this.class="neutral";
// movement
this.speed = spec.speed || 0;
this.acceleration = spec.acceleration || 0;
this.deceleration = spec.deceleration || 0;
this.delay = spec.delay || 0;
},
draw: function() {
}
});
// The player's main character
var Hero = Class.create(Character, {
initialize: function($super, spec) {
$super(spec);
spec = spec || {};
this.class = "player";
},
draw: function() {
}
});
// Urchins are the primary enemy. They have multiple arms which extend
// outward in an attempt to strike the player or block their path
var Urchin = Class.create(Character, {
initialize: function($super, spec) {
$super(spec);
spec = spec || {};
this.class = "enemy";
this.hitRadius = 3;
// 0 = no rotation, (+) = clockwise, (-) = counterclockwise
this.rotateSpeed = spec.rotateSpeed || 0;
// determines whether the urchin grows, rotates, etc. when instantiated
this.active = spec.active==undefined ? true : spec.active;
// the distance from the player at which the special behavior activates
this.activeRadius = spec.activeRadius || 0;
this.exploded = false;
this.explodeToLength = spec.explodeToLength || null;
this.explodeByLength = spec.explodeByLength || null;
this.arms = spec.arms || []; // stores the actual arms
this.initialArmLengths = [];
// initialize arms, setting body and x- and y-coordinates
this.initArms();
},
//
initArms: function() {
// set the body, x- and y-coords of each arm
for(var i in this.arms){
var arm = this.arms[i];
arm.body = this;
arm.x = this.x;
arm.y = this.y;
this.initialArmLengths.push(this.arms[i].length);
}
},
explode: function() {
if(!this.exploded){
if(this.explodeToLength){
this.explodeTo(this.explodeToLength);
} else if (this.explodeByLength) {
this.explodeBy(this.explodeByLength);
this.explodeByLength = 0; // have to reset so it does not continue to expand
} else {
for(var i=0; i<this.arms.length; i++){
var arm = this.arms[i];
arm.length = arm.explodeLength;
}
}
this.exploded = true;
this.active = true;
}
},
explodeTo: function(length) {
for(var i=0; i<this.arms.length; i++){
var arm = this.arms[i];
arm.length = length;
}
},
// arms increase by amount parameter
explodeBy: function(length) {
for(var i=0; i<this.arms.length; i++){
var arm = this.arms[i];
arm.length += length;
}
},
getArmCount: function() {
return this.arms.length;
},
// groups everything an urchin will do (e.g. grow arms, rotate, etc.)
step: function() {
if(this.active){
// rotation, rotate all arms and body
if(this.rotateSpeed != 0){
// increase all arms' angles by the rotate speed
for(var i=0; i<this.arms.length; i++){
this.arms[i].angle += this.rotateSpeed;
}
this.angle += this.rotateSpeed;
}
// moving, move all arms and body
if(this.speed > 0){
}
// growing arms
for(var i=0; i<this.arms.length; i++){
this.arms[i].grow();
}
}
}
});
//
var Arm = Class.create(Character, {
initialize: function($super, spec) {
$super(spec);
spec = spec || {};
this.class = "enemy";
this.body; // the arms's parent body
this.length = spec.length || 0;
this.growSpeed = spec.growSpeed || 0;
this.explodeLength = spec.explodeLength || 0;
this.implodeLength = spec.implodeLength || 0;
},
// the point from which the arm expands outward
origin: function() {
return {x: this.body.x, y: this.body.y};
},
// the end point of the arm (away from the body)
end: function() {
var endX = this.body.x + this.length * Math.cos(this.angle);
var endY = this.body.y + this.length * Math.sin(this.angle);
return {x: endX, y: endY};
},
// increase the arm's length by the amount parameter, if provided,
// default to the default grow speed
grow: function(amount) {
if(amount){
this.length += amount;
} else {
this.length += this.growSpeed;
}
}
});
var Item = Class.create(Character, {
initialize: function($super, spec) {
$super(spec);
spec = spec || {};
this.class = "item";
},
// execute when player gets item
claimed: function() {
}
});