-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerActor.java
executable file
·570 lines (469 loc) · 15.1 KB
/
PlayerActor.java
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.*;
import java.util.*;
import javax.sound.sampled.*;
import java.net.URL;
import java.io.File;
import java.io.IOException;
/**********************************************************************
*
* Main Class
*/
public class PlayerActor extends PointUpActor
{
public static int setting = 0;
public static enum State {
FLYING,
DYING,FALLING,NAN}
State playerState = State.NAN;
int LeftWingOffsetX = -5;
int LeftWingOffsetY = -5;
int RightWingOffsetX = 7;
int RightWingOffsetY = -5;
LeftWingActor leftw = null;
RightWingActor rightw = null;
long startTime = 0;
int counter = 0;
public static GreenfootImage cracking[] = new GreenfootImage[10];
public static GreenfootImage body = null;
public boolean UseDarkerBody = false;
Platform landedon = null;
v2 pos = new v2();
v2 velocity = new v2();
v2 gravityv = new v2(0.0,700);
v2 rightv = new v2(-280.0,-450.0);
v2 leftv = new v2(280.0,-450.0);
public PlayerActor()
{
body = new GreenfootImage("bubble.png");
for(int i = 0; i < cracking.length ; i++)
{
cracking[i] = new GreenfootImage("bubble-crack" + (i+1) + ".png");
}
playerState = State.NAN;
velocity = new v2(0.0,0.0);
UseDarkerBody = false;
//breakspeed = 20;
}
public void ToggleDarkerBody()
{
UseDarkerBody = !UseDarkerBody;
if(UseDarkerBody)
{
body = new GreenfootImage("bubble-2.png");
}
else
{
body = new GreenfootImage("bubble.png");
}
setImage(body);
}
private boolean ShouldBreakOnLanding()
{
double velocitySq = velocity.Inner(velocity);
double velocityYSq = velocity.y * velocity.y;
if((velocityYSq < 300) || (safetycounter > 0))
{
//sliding is not breaking
return(false);
}
switch (setting)
{
case 5:
//Not Fair Setting
if(velocityYSq > 5100 || velocitySq > 8500)
{
return (true);
}
break;
case 4:
//Hard Setting
if(velocityYSq > 11000 || velocitySq > 170000)
{
return (true);
}
break;
case 3:
//Notmal setting
if(velocityYSq > 26000 || velocitySq > 40000)
{
return (true);
}
break;
case 2:
//Easy Setting
if(velocityYSq > 66000)
{
return(true);
}
break;
case 1:
default:
//practice mode never breaks;
break;
}
return(false);
}
int safetycounter = 10;
int pingcount = 0;
int taps = 0;
public long timeStart = -1;
public long timeEnd = -1;
public int millsPassed = 0;
public double timeDelta = 0.0;
int pingIndex = 0;
public void hitWithRock()
{
if(playerState == State.FLYING)
{
playerState = State.FALLING;
leftw.connected = false;
rightw.connected = false;
taps = 0;
pingIndex = -1;
}
}
public void act()
{
if(timeStart < 0)
{
timeStart = System.currentTimeMillis();
}
timeEnd = System.currentTimeMillis();
millsPassed = (int)(timeEnd - timeStart);
timeDelta = (double)millsPassed/1000.0;
timeStart = System.currentTimeMillis();
boolean thrustL = leftw.flapping;
boolean thrustR = rightw.flapping;
//Copy Global Time Delta
//double timeDelta = Background.timeDelta;
if(timeDelta > 0.066)
{
timeDelta = 0.066;
}
v2 Acceleration = gravityv;
long endTime = System.currentTimeMillis();
if(playerState == State.DYING)
{
safetycounter = 10;
if(counter < 1)
{
leftw.connected = false;
rightw.connected = false;
startTime = 0;
//SoundManager.playBreak();
//SoundEffect.PING.stop();
//SoundEffect.PING2.stop();
SoundEffect.BUZZ.stop();
}
if(counter == 1)
{
//SoundManager.playBreak();
SoundEffect.BREAK.playIfNotPlaying();
}
if((endTime - startTime) > 100)
{
counter++;
startTime = endTime;
}
int index = counter;
if(index >= cracking.length)
index = cracking.length-1;
setImage(cracking[index]);
if(counter > 30)
{
playerState = State.NAN;
InfoLayer.landings = 0;
InfoLayer.crashes++;
//leftw.connected = true;
//rightw.connected = true;
}
return;
}
if(playerState == State.NAN)
{
velocity = new v2(0.0,0.0);
playerState = State.FLYING;
setImage(body);
if(leftw == null)
leftw = new LeftWingActor(this);
if(rightw == null)
rightw = new RightWingActor(this);
leftw.connected = true;
rightw.connected = true;
safetycounter = 12;
return;
}
boolean flapping = false;
if(playerState == State.FLYING)
{
if(safetycounter > 0)
{
safetycounter--;
return;
}
if(thrustL)
{
Acceleration = Acceleration.Add(leftv);
flapping = true;
}
if(thrustR)
{
Acceleration = Acceleration.Add(rightv);
flapping = true;
}
}
if(flapping)
{
SoundEffect.BUZZ.playIfNotPlaying();
}
else
{
SoundEffect.BUZZ.stop();
}
//System.out.println(timeDelta);
/*
if(timeDelta > 0.09)
{
timeDelta = 0.09;
}
*/
velocity = velocity.Add(Acceleration.Scale(timeDelta));
//pos = pos.Add(velocity.Scale(timeDelta));
v2 PlayerDelta = velocity.Scale(timeDelta).Add(Acceleration.Scale(timeDelta * timeDelta * 0.5f));
//pos = pos.Add(PlayerDelta);
//Faking Drag
v2 minusvelocity = new v2(-velocity.x,-velocity.y);
velocity = velocity.Add(minusvelocity.Scale(0.7 * timeDelta));
/**********************************************
* Platform Colissions
*/
wrappedDouble tMin = new wrappedDouble(1.0f);
/*
Get all objects for now - maybe cut down on them if
there are lots
*/
List<Platform> Platforms = getWorld().getObjects
(Platform.class);
v2 WallNormal = new v2();
boolean hit = false;
int side = -1;
//v2 PlayerDelta = velocity.Scale(timeDelta);
Platform contact = null;
v2 Rel = pos;
for(Platform p: Platforms)
{
if(p instanceof Ground)
{
continue;
}
/*All ground pieces here - we take X + Y radius of
platform and add radius
of the flier
*/
//Todo remove hard coded things
//double MinX = p.getX() - 33 - 10;
//double MinY = p.getY() - 8 - 10;
//double MaxX = p.getX() + 33 + 10;
//double MaxY = p.getY() + 8 + 10;
/*Base Box Size for platform 34x 9y each way and 10x
and 10y for ball*/
v2 MinCorner = new v2(p.getX() - 30 - 10, p.getY() - 9
- 10);
v2 MaxCorner = new v2(p.getX() + 30 + 10, p.getY() + 9
+ 10);
if(MiscUtil.TestWall(MaxCorner.x, Rel.x, Rel.y,
PlayerDelta.x, PlayerDelta.y, tMin, MinCorner.y, MaxCorner.y))
{
WallNormal = new v2(1.0f,0.0f);
hit = true;
side = 0;//right
contact = p;
}
if(MiscUtil.TestWall(MinCorner.x, Rel.x, Rel.y,
PlayerDelta.x, PlayerDelta.y, tMin, MinCorner.y, MaxCorner.y))
{
WallNormal = new v2(-1.0f,0.0f);
hit = true;
side = 1;//left
contact = p;
}
if(MiscUtil.TestWall(MaxCorner.y, Rel.y, Rel.x,
PlayerDelta.y, PlayerDelta.x, tMin, MinCorner.x, MaxCorner.x))
{
WallNormal = new v2(0.0f,1.0f);
hit = true;
side = 2;//bottom
contact = p;
}
if(MiscUtil.TestWall(MinCorner.y, Rel.y, Rel.x,
PlayerDelta.y, PlayerDelta.x, tMin, MinCorner.x, MaxCorner.x))
{
WallNormal = new v2(0.0f,-1.0f);
hit = true;
side = 3; //top
contact = p;
}
}
//Ground Test
if(MiscUtil.TestWall(461.0, Rel.y, Rel.x,
PlayerDelta.y, PlayerDelta.x, tMin, -300, 900))
{
WallNormal = new v2(0.0f,-1.0f);
hit = true;
side = 3;//bottom
contact = null;
}
pos = pos.Add(PlayerDelta.Scale(tMin.val));
if(hit)
{
switch(side)
{
case 3:
velocity.y = -velocity.y;
/*top landing*/
/*
if(PlayerDelta.Inner(PlayerDelta) > 4)
{
System.out.println("Delta Squared " + PlayerDelta.Inner(PlayerDelta));
}
*/
if(velocity.Inner(velocity) > 2000)
{
System.out.println("Velocity Squared " + velocity.Inner(velocity));
}
if((playerState == State.FLYING) && ShouldBreakOnLanding())
{
/*
counter = 0;
playerState = State.DYING;
setLocation((int)pos.x,(int)pos.y);
return;
*/
playerState = State.FALLING;
leftw.connected = false;
rightw.connected = false;
taps = 0;
pingIndex = -1;
}
//credit the landing even if it is a crash
if(/*PlayerDelta.Inner(PlayerDelta) < 16.0f*/ velocity.Inner(velocity) < 2000)
{
if((null != contact) && (contact.isActive()))
{
contact.landedOnMe();
world.activateNewPlatform();
if(playerState == State.FLYING)
{
SoundEffect.BELL.play();
InfoLayer.landings++;
}
else
{
SoundEffect.FAIL.play();
}
}
}
else
{
minusvelocity = new v2(-velocity.x,-velocity.y);
velocity = velocity.Add(minusvelocity.Scale(0.9 * timeDelta));
}
if(playerState == State.FALLING)
{
//SoundManager.playPing();
pingcount++;
int tmpnextIndex = (pingIndex + 1) % 3;
if(pingcount == 1 && ((velocity.y * velocity.y) > 14000))
{
pingIndex = (pingIndex + 1) % 3;
if (taps < 4)
{
SoundEffect.getPing(pingIndex).play();
}
taps++;
}
else
{
SoundEffect.getPing(tmpnextIndex).stop();
}
if(/*velocity.Inner(velocity) < 2000*/ (velocity.y * velocity.y) < 2000 )
{
counter = 0;
//SoundEffect.PING.stop();
//SoundEffect.PING2.stop();
playerState = State.DYING;
setLocation((int)pos.x,(int)pos.y);
return;
}
if((taps > 3) || ((velocity.y * velocity.y) < 9000))
{
velocity.x = 0;
velocity.y = 0;
}
}
//No Adjustment - take 2 wings to get off of surface
//if((/*(PlayerDelta.y * PlayerDelta.y) < 5*/ ((velocity.y * velocity.y)) < 100) && (thrustL || thrustR))
//{
// velocity.y -= 10;
//}
break;
case 2:
velocity.y = -velocity.y;
minusvelocity = new v2(-velocity.x,-velocity.y);
velocity = velocity.Add(minusvelocity.Scale(0.9 * timeDelta));
break;
default:
velocity.x = -velocity.x;
break;
}
}
if(side != 3)
{
pingcount = 0;
}
/*
v2 minusvelocity = new v2(-velocity.x,-velocity.y);
velocity = velocity.Add(minusvelocity.Scale(0.80 *
timeDelta));
*/
/**********************************************
* Platform Colissions
*/
/*
if(pos.y > 460.0)
{
velocity.y = -velocity.y;
pos.y = 460.0;
minusvelocity = new v2(-velocity.x,-velocity.y);
velocity = velocity.Add(minusvelocity.Scale(0.9 * timeDelta));
}
*/
if(pos.x < 10.0)
{
velocity.x = -velocity.x;
pos.x = 10.0;
}
//todo remove this - ground is a platform
if(pos.x > 630.0)
{
velocity.x = -velocity.x;
pos.x = 630.0;
}
setLocation((int)pos.x,(int)pos.y);
}
@Override
public void addedToWorld(World world) {
super.addedToWorld(world);
leftw = new LeftWingActor(this);
rightw = new RightWingActor(this);
world.addObject(leftw,getX() + LeftWingOffsetX,getY() + LeftWingOffsetY);
world.addObject(rightw,getX() + RightWingOffsetX,getY() + RightWingOffsetY);
leftw.offx = LeftWingOffsetX;
leftw.offy = LeftWingOffsetY;
rightw.offx = RightWingOffsetX;
rightw.offy = RightWingOffsetY;
pos = new v2((double)getX(),(double)getY());
velocity = new v2(0.0,0.0);
}
}