-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actor.pde
450 lines (379 loc) · 12.7 KB
/
Actor.pde
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
* To be used when box collision with rotation is needed
*
*/
class Actor extends GameObject{
// collision mode with all its modes under as FINALS
int collisionMode;
final int NORMC = 0;
final int TRIGGERC = 1;
final int NOC = 2;
float cRight, cLeft, cTop, cBottom;
// face codes on which face did the object hit
final int TOP_FACE = 1;
final int RIGHT_FACE = 2;
final int BOTTOM_FACE = 3;
final int LEFT_FACE = 4;
// if the object is grounded
boolean grounded;
// temp collison objects, cleared every frame
ArrayList<Platform> collidedPlatforms;
// temp trigger objects, cleared every frame
ArrayList<Platform> triggerObjects;
// basically, how much push back collision should do
float deltaColision;
// which face of this object did b collided with.
int aSideCollided;
// which face of the other object did this object collided with.
int bSideCollided;
// the gravityDirection in rad
float gravityDirection;
/**
* Starts an actor up at 00
*/
Actor() {
super();
// default collision box;
cTop = 50;
cBottom = 50;
cLeft = 50;
cRight = 50;
collidedPlatforms = new ArrayList<Platform>();
triggerObjects = new ArrayList<Platform>();
}
/**
* Star an actor up with location
* @param x_ the X locaiton of the new actor
* @param y_ the Y locaiton of the new actor
*/
Actor(float x_, float y_) {
super(x_,y_);
// default collision box;
cTop = 50;
cBottom = 50;
cLeft = 50;
cRight = 50;
collidedPlatforms = new ArrayList<Platform>();
triggerObjects = new ArrayList<Platform>();
}
/**
* Star an actor up with location and an name for animations
* @param x_ the X locaiton of the new actor
* @param y_ the Y locaiton of the new actor
* @param minFrame_ where the frame gatherer should start its image grabbing
* @param maxFrame_ where the frame gatherer should end its image grabbing
* @param fileName_ the file path to the image files. has to be in "fileName_"+00000.png format
*/
Actor(float x_, float y_, int minFrame_, int maxFrame_, String fileName_) {
super(x_, y_, minFrame_, maxFrame_, fileName_);
// default collision box;
cTop = 50;
cBottom = 50;
cLeft = 50;
cRight = 50;
collidedPlatforms = new ArrayList<Platform>();
triggerObjects = new ArrayList<Platform>();
}
/**
* Star an actor up with location and the usage of image array reference
* @param x_ the X locaiton of the new actor
* @param y_ the Y locaiton of the new actor
* @param minFrame_ the min frame the actor can use from the image array
* @param maxFrame_ the max frame the actor can use from the image array
* @param frames_ the preloaded image array
*/
Actor(float x_, float y_, int minFrame_, int maxFrame_, PImage[] frames_) {
super(x_, y_, minFrame_, maxFrame_, frames_);
// default collision box;
cTop = 50;
cBottom = 50;
cLeft = 50;
cRight = 50;
collidedPlatforms = new ArrayList<Platform>();
triggerObjects = new ArrayList<Platform>();
}
/**
* Star an actor up with location and the usage of image array reference
* @param b_ the object actor is trying to check the collision to
* @return boolean that is true if it has collided
*/
boolean collision(Actor b_) {
float aVel1, aVel2, aVel3, aVel4, bVel1, bVel2, bVel3, bVel4, avb, bva, minDelta;
PVector roedACBoxTR, roedACBoxTL, roedACBoxBR, roedACBoxBL, roedBCBoxTR, roedBCBoxTL, roedBCBoxBR, roedBCBoxBL;
Actor b = b_;
// stores the smallest delta
deltaColision = MAX_FLOAT;
minDelta = MAX_FLOAT;
// Rotated locations for each of the angles
PVector aLocRotatedA = loc.copy().rotate(-angle);
PVector bLocRotatedA = b.loc.copy().rotate(-angle);
PVector aLocRotatedB = loc.copy().rotate(-b.angle);
PVector bLocRotatedB = b.loc.copy().rotate(-b.angle);
// Collision boxes
PVector aCBoxTR = new PVector(cRight,-cTop);
PVector aCBoxTL = new PVector(-cLeft,-cTop);
PVector aCBoxBR = new PVector(cRight,cBottom);
PVector aCBoxBL = new PVector(-cLeft,cBottom);
PVector bCBoxTR = new PVector(b.cRight,-b.cTop);
PVector bCBoxTL = new PVector(-b.cLeft,-b.cTop);
PVector bCBoxBR = new PVector(b.cRight,b.cBottom);
PVector bCBoxBL = new PVector(-b.cLeft,b.cBottom);
// a's planes
// rotate the collision b's collisoin box to match
roedBCBoxTR = bCBoxTR.copy().rotate(-angle+b.angle);
roedBCBoxTL = bCBoxTL.copy().rotate(-angle+b.angle);
roedBCBoxBR = bCBoxBR.copy().rotate(-angle+b.angle);
roedBCBoxBL = bCBoxBL.copy().rotate(-angle+b.angle);
// a's x plane
aVel1 = aCBoxTR.x + aLocRotatedA.x;
aVel2 = aCBoxTL.x + aLocRotatedA.x;
aVel3 = aCBoxBR.x + aLocRotatedA.x;
aVel4 = aCBoxBL.x + aLocRotatedA.x;
bVel1 = roedBCBoxTR.x + bLocRotatedA.x;
bVel2 = roedBCBoxTL.x + bLocRotatedA.x;
bVel3 = roedBCBoxBR.x + bLocRotatedA.x;
bVel4 = roedBCBoxBL.x + bLocRotatedA.x;
float[] aXAVels = {aVel1, aVel2, aVel3, aVel4};
float[] aXBVels = {bVel1, bVel2, bVel3, bVel4};
avb = max(aXAVels) - min(aXBVels);
bva = max(aXBVels) - min(aXAVels);
if (avb < minDelta) {
minDelta = avb;
aSideCollided = LEFT_FACE;
}
if (bva < minDelta) {
minDelta = bva;
aSideCollided = RIGHT_FACE;
}
// collided?
boolean aXC = avb > 0 && bva > 0;
if (!aXC)
return false;
else {
if (avb < deltaColision)
deltaColision = avb;
if (bva < deltaColision)
deltaColision = bva;
}
// a's y plane
aVel1 = aCBoxTR.y + aLocRotatedA.y;
aVel2 = aCBoxTL.y + aLocRotatedA.y;
aVel3 = aCBoxBR.y + aLocRotatedA.y;
aVel4 = aCBoxBL.y + aLocRotatedA.y;
bVel1 = roedBCBoxTR.y + bLocRotatedA.y;
bVel2 = roedBCBoxTL.y + bLocRotatedA.y;
bVel3 = roedBCBoxBR.y + bLocRotatedA.y;
bVel4 = roedBCBoxBL.y + bLocRotatedA.y;
float[] aYAVels = {aVel1, aVel2, aVel3, aVel4};
float[] aYBVels = {bVel1, bVel2, bVel3, bVel4};
avb = max(aYAVels) - min(aYBVels);
bva = max(aYBVels) - min(aYAVels);
// println("LEFT/TOP:"+avb+" RIGHT/BOTTOM:"+bva);
if (avb < minDelta) {
minDelta = avb;
aSideCollided = TOP_FACE;
}
if (bva < minDelta) {
minDelta = bva;
aSideCollided = BOTTOM_FACE;
}
// collided?
boolean aYC = avb > 0 && bva > 0;
if (!aYC)
return false;
else {
if (avb < deltaColision)
deltaColision = avb;
if (bva < deltaColision)
deltaColision = bva;
}
// reset minDelta so it will work again for b's planes
minDelta = MAX_FLOAT;
// b's planes
// rotate the collision a's collisoin box to match
roedACBoxTR = aCBoxTR.copy().rotate(-b.angle+angle);
roedACBoxTL = aCBoxTL.copy().rotate(-b.angle+angle);
roedACBoxBR = aCBoxBR.copy().rotate(-b.angle+angle);
roedACBoxBL = aCBoxBL.copy().rotate(-b.angle+angle);
// b's x plane
bVel1 = bCBoxTR.x + bLocRotatedB.x;
bVel2 = bCBoxTL.x + bLocRotatedB.x;
bVel3 = bCBoxBR.x + bLocRotatedB.x;
bVel4 = bCBoxBL.x + bLocRotatedB.x;
aVel1 = roedACBoxTR.x + aLocRotatedB.x;
aVel2 = roedACBoxTL.x + aLocRotatedB.x;
aVel3 = roedACBoxBR.x + aLocRotatedB.x;
aVel4 = roedACBoxBL.x + aLocRotatedB.x;
float[] bXAVels = {aVel1, aVel2, aVel3, aVel4};
float[] bXBVels = {bVel1, bVel2, bVel3, bVel4};
avb = max(bXAVels) - min(bXBVels);
bva = max(bXBVels) - min(bXAVels);
if (avb < minDelta) {
minDelta = avb;
bSideCollided = LEFT_FACE;
}
if (bva < minDelta) {
minDelta = bva;
bSideCollided = RIGHT_FACE;
}
// collided?
boolean bXC = avb > 0 && bva > 0;
if (!bXC)
return false;
else {
if (avb < deltaColision)
deltaColision = avb;
if (bva < deltaColision)
deltaColision = bva;
}
// b's y plane
bVel1 = bCBoxTR.y + bLocRotatedB.y;
bVel2 = bCBoxTL.y + bLocRotatedB.y;
bVel3 = bCBoxBR.y + bLocRotatedB.y;
bVel4 = bCBoxBL.y + bLocRotatedB.y;
aVel1 = roedACBoxTR.y + aLocRotatedB.y;
aVel2 = roedACBoxTL.y + aLocRotatedB.y;
aVel3 = roedACBoxBR.y + aLocRotatedB.y;
aVel4 = roedACBoxBL.y + aLocRotatedB.y;
float[] bYAVels = {aVel1, aVel2, aVel3, aVel4};
float[] bYBVels = {bVel1, bVel2, bVel3, bVel4};
avb = max(bYAVels) - min(bYBVels);
bva = max(bYBVels) - min(bYAVels);
if (avb < minDelta) {
minDelta = avb;
bSideCollided = TOP_FACE;
}
if (bva < minDelta) {
minDelta = bva;
bSideCollided = BOTTOM_FACE;
}
// collided?
boolean bYC = avb > 0 && bva > 0;
if (!bYC)
return false;
else {
if (avb < deltaColision)
deltaColision = avb;
if (bva < deltaColision)
deltaColision = bva;
}
return aXC && aYC && bXC && bYC;
}
/**
* For use when object needs to collide with platforms.
* switches gravity automatically and sends the trigger objects into the arraylist
* also sends the collided platforms into the arraylist as well.
*/
void collisionController() {
// clear all the temp reference arraylists so it can be used again.
collidedPlatforms.clear();
triggerObjects.clear();
grounded = false;
for(int c = 0; c < world.platforms.size(); c ++) {
Platform p = world.platforms.get(c);
if (collision(p)) {
if (p.collisionMode == NORMC) {
// if the the platform is colliding with THIS, add it to the collidedPlatforms list
collidedPlatforms.add(p);
float pushAngle = p.angle;
if (p.tag != null && p.tag.equals("O")) {
// opposite day
// pushes different direction depending on which side is touching.
if (bSideCollided == TOP_FACE) {
if (p.gTop) {
angleDes = pushAngle;
gravityDirection = pushAngle+PI;
grounded = true;
} else {
grounded = false;
}
} else if (bSideCollided == BOTTOM_FACE) {
pushAngle += PI;
if (p.gBottom) {
angleDes = pushAngle;
gravityDirection = pushAngle+PI;
grounded = true;
} else {
grounded = false;
}
} else if (bSideCollided == LEFT_FACE) {
pushAngle -= PI/2;
if (p.gLeft) {
angleDes = pushAngle;
gravityDirection = pushAngle+PI;
grounded = true;
} else {
grounded = false;
}
} else if (bSideCollided == RIGHT_FACE) {
pushAngle += PI/2;
if (p.gRight) {
angleDes = pushAngle;
gravityDirection = pushAngle+PI;
grounded = true;
} else {
grounded = false;
}
} else {
grounded = false;
}
} else {
// normal gravity blocks
// pushes different direction depending on which side is touching.
if (bSideCollided == TOP_FACE) {
if (p.gTop) {
angleDes = pushAngle;
gravityDirection = pushAngle;
grounded = true;
} else {
grounded = false;
}
} else if (bSideCollided == BOTTOM_FACE){
pushAngle += PI;
if (p.gBottom) {
angleDes = pushAngle;
gravityDirection = pushAngle;
grounded = true;
} else {
grounded = false;
}
} else if (bSideCollided == LEFT_FACE) {
pushAngle -= PI/2;
if (p.gLeft) {
angleDes = pushAngle;
gravityDirection = pushAngle;
grounded = true;
} else {
grounded = false;
}
} else if (bSideCollided == RIGHT_FACE) {
pushAngle += PI/2;
if (p.gRight) {
angleDes = pushAngle;
gravityDirection = pushAngle;
grounded = true;
} else {
grounded = false;
}
} else {
grounded = false;
}
}
// push the object up to the place it should be
loc.sub(new PVector(0,deltaColision).rotate(pushAngle));
// eliminate the velocity that hits
vel.rotate(-pushAngle);
vel.y = 0;
vel.rotate(pushAngle);
}
else if (p.collisionMode == TRIGGERC) {
// add to the trigger arraylist
triggerObjects.add(p);
}
}
}
}
// float roundDes(float num_, int numOfDes_) {
// return round(num_ * pow(numOfDes_,numOfDes_)) / pow(numOfDes_,numOfDes_);
// }
}