/
Player.cs
1588 lines (1411 loc) · 58.2 KB
/
Player.cs
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using Engine;
using OpenTK.Input;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace U5Designs {
public enum PlayerAnim { stand2d=0, stand3d=1, walk2d=2, walk3d=3, spin2d=4, spin3d=5, throwRun2d=6, throwRun3d=7, throwStand2d=8, throwStand3d=9 };
public class Player : GameObject, RenderObject, PhysicsObject, CombatObject
{
private const int fallDamage = 1;
private const int runSpeed = 125;
private const int spinSpeed = 175;
private PlayState playstate; //Keep a reference since we'll need this regularly
internal Decoration arms;
//Knockback physics constants:
private Vector3 kbspeed;
private float jumpspeed;
//Player state
public PlayerState p_state;
public Vector3 velocity;
private Vector3 accel;
public double stamina, maxStamina;
public int maxHealth;
internal bool Invincible, HasControl, isMobile;
private Vector3 lastPosOnGround; // used for falling off detection
//timers
private double NoControlTimer, projectileTimer, spinTimer;
public double Invincibletimer, fallTimer, viewSwitchJumpTimer, lookDownTimer, squishTimer;
//projectile managment
List<ProjectileProperties> projectiles;
public ProjectileProperties curProjectile;
public SpriteSheet marker;
public List<Decoration> markerList;
public float deltax; //used for updating position of camera, etc.
public Camera cam; //pointer to the camera, used for informing of y position changes
private bool enable3d; //track current world state (used in spawning projectiles)
public bool onGround; //true when on ground, or when we just switched to 3d and were in midair
//spin attack
public bool spinning; //true when spin attack is in progress
public double spinSize; // the distance from the center of the player the spin attack extends
public int spinDamage;
private bool spaceDown, eDown, pDown;
public bool oldMouseState = false;
public bool inLevelDesignMode = false; // THIS IS ONLY USED FOR LEVEL DESIGNER
// SOUND FILES
AudioFile jumpSound, bananaSound;
public AudioFile HitSound, HurtSound, SpinSound;
public Player(SpriteSheet sprite, SpriteSheet arms, List<ProjectileProperties> projectiles, PlayState ps) : base(Int32.MaxValue) //player always has largest ID for rendering purposes
{
p_state = new PlayerState("TEST player");
//p_state.setSpeed(130);
this.playstate = ps;
_speed = runSpeed;
_location = new Vector3(25, 12.5f, 50);
_scale = new Vector3(25f, 25f, 25f);
_pbox = new Vector3(5f, 11.0f, 5f);
_cbox = new Vector3(4f, 10.0f, 4f);
spinSize = 12; //?? experiment with this, TODO: change it to match the sprites size
velocity = new Vector3(0, 0, 0);
accel = new Vector3(0, 0, 0);
kbspeed = new Vector3(70, 100, 70);
jumpspeed = 250.0f;
_cycleNum = 0;
_frameNum = 0;
_sprite = sprite;
_hascbox = true;
_type = 0; //type 0 is the player and only the player
_existsIn2d = true;
_existsIn3d = true;
onGround = true;
//arms
this.arms = new Decoration(_location + new Vector3(0.005f, 0.0f, 0.005f), _scale, true, true, Billboarding.Yes, arms);
ps.renderList.Add(this.arms);
//combat things
_damage = 1;
spinDamage = 2;
maxHealth = _health = 7;
stamina = 5.0;
maxStamina = 5.0;
spaceDown = false;
eDown = false;
pDown = false;
isMobile = true;
Invincible = false;
HasControl = true;
Invincibletimer = 0.0;
NoControlTimer = 0.0;
fallTimer = 0.0;
viewSwitchJumpTimer = 0.0;
projectileTimer = 0.0;
spinTimer = 0.0;
lookDownTimer = -1.0;
squishTimer = -1.0;
lastPosOnGround = new Vector3(_location);
_animDirection = 1;
this.projectiles = projectiles;
curProjectile = projectiles[0];
curProjectile.damage = _damage;
markerList = new List<Decoration>();
Assembly assembly = Assembly.GetExecutingAssembly();
jumpSound = new AudioFile(assembly.GetManifestResourceStream("U5Designs.Resources.Sound.jump_sound.ogg"));
bananaSound = new AudioFile(assembly.GetManifestResourceStream("U5Designs.Resources.Sound.banana2.ogg"));
HurtSound = new AudioFile(assembly.GetManifestResourceStream("U5Designs.Resources.Sound.hurt.ogg"));
HitSound = new AudioFile(assembly.GetManifestResourceStream("U5Designs.Resources.Sound.hit.ogg"));
SpinSound = new AudioFile(assembly.GetManifestResourceStream("U5Designs.Resources.Sound.spin.ogg"));
}
~Player() {
projectiles.Clear();
}
private float locX {
get { return _location.X; }
set {
_location.X = value;
arms.locX = value + 0.005f;
}
}
private float locY {
get { return _location.Y; }
set {
_location.Y = value;
arms.locY = value;
}
}
private float locZ {
get { return _location.Z; }
set {
_location.Z = value;
arms.locZ = value + 0.005f;
}
}
public override Vector3 location {
get { return _location; }
set {
_location = value;
arms.location = value + new Vector3(0.005f, 0.0f, 0.005f);
}
}
/// <summary>
/// Updates the player specific state. Gets called every update frame
/// </summary>
/// <param name="enable3d"> true if we are in 3d view</param>
/// <param name="keyboard"> the keyboard object, for handling input</param>
/// <param name="time">time elapsed since last update</param>
internal void updateState(bool enable3d, KeyboardDevice keyboard, double time) {
this.enable3d = enable3d;
//Update timers
if(stamina < maxStamina) {
stamina = Math.Min(stamina + time, maxStamina);
}
//Update spinTimer even if not spinning, because it's also the timeout before you can spin again
if(spinTimer > 0.0) {
spinTimer -= time;
}
if(spinning) {
stamina = Math.Max(stamina - 5.0 * time, 0.0);
if(stamina <= 0.0 || spinTimer <= 0.0) {
spinning = false;
_speed = 75;
if(velocity.X == 0) {
cycleNumber = (int)(enable3d ? PlayerAnim.stand3d : PlayerAnim.stand2d);
} else {
cycleNumber = (int)(enable3d ? PlayerAnim.walk3d : PlayerAnim.walk2d);
}
}
} else {
_speed = runSpeed;
}
if(projectileTimer > 0.0) {
projectileTimer -= time;
}
if (Invincible)
Invincibletimer = Invincibletimer - time;
if (Invincibletimer <= 0) { // invincible is gone
Invincibletimer = 0;
Invincible = false;
}
if (!HasControl && NoControlTimer > 0.0)
NoControlTimer = NoControlTimer - time;
if (NoControlTimer <= 0.0) {
NoControlTimer = 0.0;
HasControl = true;
}
if(viewSwitchJumpTimer > 0.0) {
viewSwitchJumpTimer -= time;
}
if(lookDownTimer >= 0.0) {
lookDownTimer += time;
if(lookDownTimer >= 0.35) {
cam.moveToYPos(_location.Y - 50.0f);
lookDownTimer = -2.0;
isMobile = false;
}
}
if(squishTimer >= 0.0) {
squishTimer += time;
float oldPboxY = _pbox.Y;
if(squishTimer <= 0.1) {
arms.scaleY = _scale.Y = (float)(250.0 * (0.1 - squishTimer));
_pbox.Y = _scale.Y / 2.0f;
locY -= oldPboxY - _pbox.Y;
} else if(squishTimer <= 3.1) {
arms.scaleY = _scale.Y = _pbox.Y = 0.0f;
locY -= oldPboxY - _pbox.Y;
} else if(squishTimer <= 3.35) {
arms.scaleY = _scale.Y = (float)(100.0 * (squishTimer - 3.1));
_pbox.Y = _scale.Y / 2.0f;
locY += _pbox.Y - oldPboxY;
} else {
arms.scaleY = _scale.Y = 25.0f;
_pbox.Y = 11.0f;
locY += _pbox.Y - oldPboxY;
squishTimer = -1.0;
isMobile = true;
}
}
//If the grenade is selected, draw parabola
//if(curProjectile.gravity) {
//addMarkers();
//}
if (HasControl) {
handleKeyboardInput(keyboard);
//Keep this last
handleMouseInput();
}
}
#region leveldesigner
/// <summary>
/// FOR LEVEL DESIGNER ONLY
/// </summary>
/// <param name="enable3d"> true if we are in 3d view</param>
/// <param name="keyboard"> the keyboard object, for handling input</param>
/// <param name="time">time elapsed since last update</param>
internal void updateState(bool enable3d, KeyboardDevice keyboard, double time, bool leveldesign =true)
{
this.enable3d = enable3d;
//Update timers
if (stamina < maxStamina)
{
stamina = Math.Min(stamina + time, maxStamina);
}
//Update spinTimer even if not spinning, because it's also the timeout before you can spin again
if (spinTimer > 0.0)
{
spinTimer -= time;
}
if(spinning) {
stamina = Math.Max(stamina - 5.0 * time, 0.0);
if(stamina <= 0.0 || spinTimer <= 0.0) {
spinning = false;
_speed = runSpeed;
if(velocity.X == 0) {
cycleNumber = (int)(enable3d ? PlayerAnim.stand3d : PlayerAnim.stand2d);
} else {
cycleNumber = (int)(enable3d ? PlayerAnim.walk3d : PlayerAnim.walk2d);
}
}
} else {
_speed = runSpeed;
}
if (projectileTimer > 0.0) {
projectileTimer -= time;
}
if (Invincible)
Invincibletimer = Invincibletimer - time;
if (Invincibletimer <= 0) {
// invincible is gone
Invincibletimer = 0;
Invincible = false;
}
if (!HasControl && NoControlTimer > 0.0)
NoControlTimer = NoControlTimer - time;
if (NoControlTimer <= 0.0) {
NoControlTimer = 0.0;
HasControl = true;
}
if (viewSwitchJumpTimer > 0.0) {
viewSwitchJumpTimer -= time;
}
//If the grenade is selected, draw parabola
if (curProjectile.gravity)
{
addMarkers();
}
if (HasControl)
{
handleKeyboardInput(keyboard);
//Keep this last
//handleMouseInput();
}
}
#endregion
private double prevArmFrameNum;
/// <summary>
/// Called during draw; decides if an animation needs to change cycles
/// </summary>
public void animUpdate() {
switch((PlayerAnim)arms.cycleNumber) {
case PlayerAnim.throwRun2d:
case PlayerAnim.throwRun3d:
case PlayerAnim.throwStand2d:
case PlayerAnim.throwStand3d:
if(arms.frameNumber < prevArmFrameNum) {
arms.cycleNumber = _cycleNum;
arms.frameNumber = _frameNum;
} else {
prevArmFrameNum = arms.frameNumber;
}
break;
}
}
/// <summary>
/// Handles all keyboard input from the player
/// </summary>
/// <param name="keyboard">Contains the current keypresses</param>
private void handleKeyboardInput(KeyboardDevice keyboard) {
if(isMobile) {
if(enable3d) {
Vector2 newVel = new Vector2(0);
if(keyboard[Key.W]) { newVel.X++; }
if(keyboard[Key.S]) { newVel.X--; }
if(keyboard[Key.A]) { newVel.Y--; }
if(keyboard[Key.D]) { newVel.Y++; }
newVel.NormalizeFast();
if(viewSwitchJumpTimer > 0.0 && (newVel.X != 0 || newVel.Y != 0)) {
viewSwitchJumpTimer = Math.Min(viewSwitchJumpTimer, 0.025);
}
velocity.X = newVel.X * speed;
velocity.Z = newVel.Y * speed;
//update current animation and flip scale if necessary
//arms.animDirection = _animDirection = (velocity.X >= 0 ? 1 : 1);
if(!spinning) {
cycleNumber = (int)(velocity.X == 0 ? PlayerAnim.stand3d : PlayerAnim.walk3d);
if(arms.cycleNumber != _cycleNum) {
arms.cycleNumber = (int)(velocity.X == 0 ? PlayerAnim.throwStand3d : PlayerAnim.throwRun3d);
}
}
if(_scale.X < 0) {
_scale.X = -_scale.X;
arms.scaleX = -arms.scaleX;
}
} else {
if(keyboard[Key.A] == keyboard[Key.D]) {
velocity.X = 0f;
} else if(keyboard[Key.D]) {
velocity.X = _speed;
} else { //a
velocity.X = -_speed;
}
//update current animation and flip scale if necessary
//arms.animDirection = _animDirection = 1;
if(!spinning) {
cycleNumber = (int)(velocity.X == 0 ? PlayerAnim.stand2d : PlayerAnim.walk2d);
if((velocity.X < 0 && _scale.X > 0) || (velocity.X > 0 && _scale.X < 0)) {
_scale.X = -_scale.X;
arms.scaleX = -arms.scaleX;
}
if(arms.cycleNumber != _cycleNum) {
arms.cycleNumber = (int)(velocity.X == 0 ? PlayerAnim.throwStand2d : PlayerAnim.throwRun2d);
}
}
}
#if true
// //Cloud
// //TODO: Implement animation etc, possibly change which key triggers this
// if(keyboard[Key.C]) {
// velocity.Y = _speed;
// fallTimer = 0.0;
// }
// //TMP PHYSICS TEST BUTTON suicide button
// if (keyboard[Key.X]) {
// _health = 0;
// }
// if (keyboard[Key.P] && !pDown) {
// pDown = true;
// Console.WriteLine("Player position: (" + location.X + ", " + location.Y + ", " + location.Z + ")");
// }
// else if (!keyboard[Key.P])
// pDown = false;
//
// if (keyboard[Key.BackSlash])
// location = new Vector3(3779, 138, 43);
if (keyboard[Key.Semicolon]) {
playstate.bossAI.killBoss(playstate);
playstate.bossMode = false;
//transition to next level
playstate.waitingToSwitchLevels = true;
}
#endif
//Jump
if(keyboard[Key.Space] && !spaceDown) {
if(onGround) {
accelerate(Vector3.UnitY * jumpspeed);
onGround = false;
viewSwitchJumpTimer = 0.0;
//jumpSound.Play();
}
spaceDown = true;
} else if(!keyboard[Key.Space]) {
spaceDown = false;
}
} else { //not mobile - grenade selected
velocity.X = 0.0f;
velocity.Z = 0.0f;
}
//Look down
if(!enable3d && keyboard[Key.S] && !keyboard[Key.A] && !keyboard[Key.D]) {
if(lookDownTimer == -1.0) {
lookDownTimer = 0.0;
}
} else if(lookDownTimer != -1.0) {
cam.moveToYPos(_location.Y);
lookDownTimer = -1.0;
isMobile = true;
}
//Toggle Grenade
if(keyboard[Key.E] && !eDown) {
if(curProjectile.gravity) { //Turn grenades off
curProjectile = projectiles[0];
isMobile = true;
markerList.Clear();
} else { //Turn grenades on
if(onGround) {
curProjectile = projectiles[1];
velocity.X = 0.0f;
velocity.Z = 0.0f;
isMobile = false;
cycleNumber = (int)(enable3d ? PlayerAnim.stand3d : PlayerAnim.stand2d);
if(arms.cycleNumber != _cycleNum) {
arms.cycleNumber = (int)(enable3d ? PlayerAnim.throwStand3d : PlayerAnim.throwStand2d);
}
}
}
eDown = true;
} else if(!keyboard[Key.E]) {
eDown = false;
}
//Secret skip to boss
if(keyboard[Key.Period] && keyboard[Key.Comma]) {
//floor at 125 + 12.5 player pbox
location = new Vector3(playstate.bossSpawn) + Vector3.UnitY * (_pbox.Y + 0.1f);
//The following should maybe be moved to a function in Camera
playstate.camera.eye.Y = _location.Y + (enable3d ? 24.0f : 31.25f);
playstate.camera.lookat.Y = _location.Y + (enable3d ? 20.5f : 31.25f);
cam.moveToYPos(_location.Y);
//playstate.enterBossMode();
}
}
/// <summary>
/// Provides all the mouse input for the player. Will currently check for a left click and shoot a projectile in the direction
/// </summary>
/// <param name="playstate">a pointer to play state, for accessing the obj lists in playstate</param>
private void handleMouseInput()
{
if (playstate.eng.ThisMouse.LeftPressed()) {
if(stamina >= curProjectile.staminaCost && projectileTimer <= 0.0) {
spawnProjectile(calcProjDir());
stamina -= curProjectile.staminaCost;
projectileTimer = 0.25;
if(_cycleNum == (int)(enable3d ? PlayerAnim.walk3d : PlayerAnim.walk2d)) {
arms.cycleNumber = (int)(enable3d ? PlayerAnim.throwRun3d : PlayerAnim.throwRun2d);
} else { //standing
arms.cycleNumber = (int)(enable3d ? PlayerAnim.throwStand3d : PlayerAnim.throwStand2d);
}
arms.frameNumber = 0;
prevArmFrameNum = -1.0;
bananaSound.Play();
}
//TODO: If the player was out of stamina, flash the stamina bar to let them know what happened
//Note: do this whether we actually launched a grenade or not, because if we didn't, it's
// probably because of stamina, and the player may not have time to switch back or to
// wait for stamina to refill.
if(curProjectile.gravity) {
//Automatically switch back to normal projectile
curProjectile = projectiles[0];
isMobile = true;
markerList.Clear();
projectileTimer = 0.25;
}
}
if (isMobile && playstate.eng.ThisMouse.RightPressed() && spinTimer <= 0.0 && stamina > 0.0) {
spinning = true;
spinTimer = 0.5;
_speed = spinSpeed;
arms.cycleNumber = cycleNumber = (int)(enable3d ? PlayerAnim.spin3d : PlayerAnim.spin2d); //explicitly set arms to clear possible throw animation
oldMouseState = playstate.eng.ThisMouse.RightPressed();
}
}
/// <summary>
/// Spawns the players projectile in the param direction
/// </summary>
/// <param name="playstate">a pointer to play state, for accessing the obj lists in playstate</param>
/// <param name="direction">The direction to fire the projectile in</param>
private void spawnProjectile(Vector3 direction) {
Vector3 projlocation = new Vector3(location);
//break the rendering tie between player and projectile, or else they flicker
if(!enable3d) {
projlocation.Z += 0.001f;
}
Projectile shot = new Projectile(projlocation, direction, true, curProjectile, playstate.player); // spawn the actual projectile
//shot.accelerate(new Vector3(velocity.X, 0.0f, velocity.Z)); //account for player's current velocity
// add projectile to appropriate lists
playstate.objList.Add(shot);
playstate.renderList.Add(shot);
playstate.collisionList.Add(shot);
playstate.physList.Add(shot);
playstate.combatList.Add(shot);
}
/// <summary>
/// Adds indicators to show where the players projectile is going to go
/// Used for gravity based projectiles (specifically the grenade)
/// </summary>
public void addMarkers() {
markerList.Clear();
if(curProjectile.gravity) {
Vector3 vel = calcProjDir() * curProjectile.speed;
//vel -= new Vector3(velocity.X, 0.0f, velocity.Z);
Vector3 pos = new Vector3(_location);
// for(int i = 0; i < 40; i++) {
// vel.Y -= (float)gravity / 20.0f;
// //vel.Y -= 20.0f;
// pos += (vel / 20.0f);
// markerList.Add(new Decoration(pos, new Vector3(5, 5, 5), true, true, Billboarding.Yes, marker));
// }
if(enable3d) {
for(int i = 0; i < 10; i++) {
double time = 1 / 20.0;
vel.Y -= (float)(gravity * time);
//now check for collisions and move
List<PhysicsObject> alreadyCollidedList = new List<PhysicsObject>();
while(time > 0.0) {
PhysicsObject collidingObj = null;
float collidingT = 1.0f / 0.0f; //pos infinity
int collidingAxis = -1;
foreach(PhysicsObject obj in playstate.physList) {
// don't do collision physics to things you already hit this frame
if(obj.collidesIn3d && !alreadyCollidedList.Contains(obj)) {
//find possible ranges of values for which a collision may have occurred
Vector3 maxTvals = VectorUtil.div(obj.location + obj.pbox - pos + curProjectile.pbox, vel);
Vector3 minTvals = VectorUtil.div(obj.location - obj.pbox - pos - curProjectile.pbox, vel);
VectorUtil.sort(ref minTvals, ref maxTvals);
float minT = VectorUtil.maxVal(minTvals);
float maxT = VectorUtil.minVal(maxTvals);
// both axes? || forward? || within range?
if(minT > maxT || minT < 0.0f || minT > time) {
continue; //no intersection
}
//if we're here, there's a collision
//see if this collision is the first to occur
if(minT < collidingT) {
collidingT = minT;
collidingObj = obj;
collidingAxis = VectorUtil.maxIndex(minTvals.Xy);
}
}
}
if(collidingAxis == -1) { //no collision
pos += vel * (float)time;
time = 0.0;
} else {
alreadyCollidedList.Add(collidingObj);
Vector3 startLoc = new Vector3(pos);
switch(collidingAxis) {
case 0: //x
if(pos.X < collidingObj.location.X) {
pos.X = collidingObj.location.X - (curProjectile.pbox.X + collidingObj.pbox.X) - 0.0001f;
} else {
pos.X = collidingObj.location.X + curProjectile.pbox.X + collidingObj.pbox.X + 0.0001f;
}
if(vel.X != 0) { //should always be true, but just in case...
double deltaTime = (pos.X - startLoc.X) / vel.X;
pos.Y += (float)(vel.Y * deltaTime);
pos.Z += (float)(vel.Z * deltaTime);
time -= deltaTime;
}
vel.X *= -0.6f; //bounce
break;
case 1: //y
if(pos.Y < collidingObj.location.Y) {
pos.Y = collidingObj.location.Y - (curProjectile.pbox.Y + collidingObj.pbox.Y) - 0.0001f;
} else {
pos.Y = collidingObj.location.Y + curProjectile.pbox.Y + collidingObj.pbox.Y + 0.0001f;
}
if(vel.Y != 0) { //should always be true, but just in case...
double deltaTime = (pos.Y - startLoc.Y) / vel.Y;
pos.X += (float)(vel.X * deltaTime);
pos.Z += (float)(vel.Z * deltaTime);
time -= deltaTime;
}
vel.Y *= -0.6f; //bounce
if(pos.Y > collidingObj.location.Y) {
//Simulate friction for rolling
if(Math.Abs(vel.X) >= 50.0f) {
vel.X = (vel.X >= 0 ? vel.X - (float)(Projectile.friction * time) : vel.X + (float)(Projectile.friction * time));
}
if(vel.Y < 25.0f) {
vel.Y = 0.0f; //stop bounces when they get too small
}
}
break;
case 2: //z
if(pos.Z < collidingObj.location.Z) {
pos.Z = collidingObj.location.Z - (curProjectile.pbox.Z + collidingObj.pbox.Z) - 0.0001f;
} else {
pos.Z = collidingObj.location.Z + curProjectile.pbox.Z + collidingObj.pbox.Z + 0.0001f;
}
if(vel.Z != 0) { //should always be true, but just in case...
double deltaTime = (pos.Z - startLoc.Z) / vel.Z;
pos.X += (float)(vel.X * deltaTime);
pos.Y += (float)(vel.Y * deltaTime);
time -= deltaTime;
}
vel.Z *= -0.6f; //bounce
break;
}
}
}
markerList.Add(new Decoration(pos, new Vector3(2, 2, 2), true, true, Billboarding.Yes, marker));
}
} else { //2D
for(int i = 0; i < 10; i++) {
double time = 1 / 20.0;
vel.Y -= (float)(gravity * time);
//now check for collisions and move
List<PhysicsObject> alreadyCollidedList = new List<PhysicsObject>();
while(time > 0.0) {
PhysicsObject collidingObj = null;
float collidingT = 1.0f / 0.0f; //pos infinity
int collidingAxis = -1;
foreach(PhysicsObject obj in playstate.physList) {
// don't do collision physics to things you already hit this frame
if(obj.collidesIn2d && !alreadyCollidedList.Contains(obj)) {
//find possible ranges of values for which a collision may have occurred
Vector3 maxTvals = VectorUtil.div(obj.location + obj.pbox - pos + curProjectile.pbox, vel);
Vector3 minTvals = VectorUtil.div(obj.location - obj.pbox - pos - curProjectile.pbox, vel);
VectorUtil.sort(ref minTvals, ref maxTvals);
float minT = VectorUtil.maxVal(minTvals.Xy);
float maxT = VectorUtil.minVal(maxTvals.Xy);
// both axes? || forward? || within range?
if(minT > maxT || minT < 0.0f || minT > time) {
continue; //no intersection
}
//if we're here, there's a collision
//see if this collision is the first to occur
if(minT < collidingT) {
collidingT = minT;
collidingObj = obj;
collidingAxis = VectorUtil.maxIndex(minTvals.Xy);
}
}
}
if(collidingAxis == -1) { //no collision
pos += vel * (float)time;
time = 0.0;
} else {
alreadyCollidedList.Add(collidingObj);
Vector3 startLoc = new Vector3(pos);
switch(collidingAxis) {
case 0: //x
if(pos.X < collidingObj.location.X) {
pos.X = collidingObj.location.X - (curProjectile.pbox.X + collidingObj.pbox.X) - 0.0001f;
} else {
pos.X = collidingObj.location.X + curProjectile.pbox.X + collidingObj.pbox.X + 0.0001f;
}
if(vel.X != 0) { //should always be true, but just in case...
double deltaTime = (pos.X - startLoc.X) / vel.X;
pos.Y += (float)(vel.Y * deltaTime);
pos.Z += (float)(vel.Z * deltaTime);
time -= deltaTime;
}
vel.X *= -0.6f; //bounce
break;
case 1: //y
if(pos.Y < collidingObj.location.Y) {
pos.Y = collidingObj.location.Y - (curProjectile.pbox.Y + collidingObj.pbox.Y) - 0.0001f;
} else {
pos.Y = collidingObj.location.Y + curProjectile.pbox.Y + collidingObj.pbox.Y + 0.0001f;
}
if(vel.Y != 0) { //should always be true, but just in case...
double deltaTime = (pos.Y - startLoc.Y) / vel.Y;
pos.X += (float)(vel.X * deltaTime);
pos.Z += (float)(vel.Z * deltaTime);
time -= deltaTime;
}
vel.Y *= -0.6f; //bounce
if(pos.Y > collidingObj.location.Y) {
//Simulate friction for rolling
if(Math.Abs(vel.X) >= 50.0f) {
vel.X = (vel.X >= 0 ? vel.X - (float)(Projectile.friction * time) : vel.X + (float)(Projectile.friction * time));
}
if(vel.Y < 25.0f) {
vel.Y = 0.0f; //stop bounces when they get too small
}
}
break;
}
}
}
markerList.Add(new Decoration(pos, new Vector3(2, 2, 2), true, true, (enable3d ? Billboarding.Lock3d : Billboarding.Lock2d), marker));
}
}
}
}
private Vector3 getMouseWorldPos() {
Vector3d mousecoord = new Vector3d((double)playstate.eng.Mouse.X, (double)(playstate.eng.Height - playstate.eng.Mouse.Y), 1.0);
// Pull the projection and model view matrix from the camera.
Matrix4d project = playstate.camera.GetProjectionMatrix();
Matrix4d model = playstate.camera.GetModelViewMatrix();
if(enable3d) {
//mousecoord.X -= 400.0;
//If 3d view, get z coordinate of mouse
//float[] z = new float[1];
//GL.ReadPixels((int)mousecoord.X, (int)mousecoord.Y, 1, 1, OpenTK.Graphics.OpenGL.PixelFormat.DepthComponent, OpenTK.Graphics.OpenGL.PixelType.Float, z);
//mousecoord.Z = z[0];
// Unproject the coordinates to convert from mouse to world coordinates
mousecoord.Z = 0.0;
Vector3d near = GameMouse.UnProject(mousecoord, model, project, playstate.camera.getViewport());
mousecoord.Z = 1.0;
Vector3d far = GameMouse.UnProject(mousecoord, model, project, playstate.camera.getViewport());
//Interpolate to find coordinates just in front of player
double t = (_location.X + 100.0f - near.X) / (far.X - near.X);
return new Vector3(_location.X + 100.0f, (float)(near.Y + (far.Y - near.Y) * t), (float)(near.Z + (far.Z - near.Z) * t));
} else {
// Unproject the coordinates to convert from mouse to world coordinates
Vector3d mouseWorld = GameMouse.UnProject(mousecoord, model, project, playstate.camera.getViewport());
return new Vector3((float)mouseWorld.X, (float)mouseWorld.Y, _location.Z);
}
}
private Vector3 calcProjDir() {
Vector3 mouseWorld = getMouseWorldPos();
Vector3 projDir;
if(curProjectile.gravity) {
if(!playstate.enable3d) {
projDir = mouseWorld - _location;
projDir.X = Math.Abs(projDir.X); //do this to simplify equation - will undo at end
} else {
projDir = new Vector3((float)(Math.Sqrt(Math.Pow(mouseWorld.X - _location.X, 2) + Math.Pow(mouseWorld.Z - _location.Z, 2))),
(float)(mouseWorld.Y - _location.Y), 0.0f);
}
//Following is adapted from http://en.wikipedia.org/wiki/Trajectory_of_a_projectile#Angle_required_to_hit_coordinate_.28x.2Cy.29
double velsquared = curProjectile.speed * curProjectile.speed;
double sqrtPart = Math.Sqrt(velsquared * velsquared - gravity * (gravity * projDir.X * projDir.X + 2 * projDir.Y * velsquared));
double theta;
if(sqrtPart == sqrtPart) { //false when sqrtPart == NaN
theta = Math.Atan((velsquared - sqrtPart) / (gravity * projDir.X));
} else {
//Calculate how far in that direction we can get
if(projDir.X == 0) { //Avoid divide by zero
theta = Math.PI / 2;
} else {
double phi = Math.Atan(projDir.Y / projDir.X);
double cosphi = Math.Cos(phi);
double r = (gravity * velsquared * (1 - Math.Sin(phi))) / (gravity * gravity * cosphi * cosphi);
r -= 0.01f; //This prevents a floating point roundoff bug
projDir.X = (float)(r * cosphi);
projDir.Y = (float)(r * Math.Sin(phi));
sqrtPart = Math.Sqrt(velsquared * velsquared - gravity * (gravity * projDir.X * projDir.X + 2 * projDir.Y * velsquared));
theta = Math.Atan((velsquared - sqrtPart) / (gravity * projDir.X));
}
}
projDir.X = (float)Math.Cos(theta);
projDir.Y = (float)Math.Sin(theta);
if(enable3d) {
double phi = Math.Atan((mouseWorld.X - _location.X) / (mouseWorld.Z - _location.Z));
if(mouseWorld.Z < _location.Z) {
phi += Math.PI;
}
projDir.Z = (float)(projDir.X * Math.Cos(phi));
projDir.X = (float)(projDir.X * Math.Sin(phi));
} else {
projDir.Z = 0.0f;
if(mouseWorld.X < _location.X) {
projDir.X = -projDir.X;
}
}
} else { //projectile doesn't do gravity
projDir = mouseWorld - _location;
if(!enable3d) {
projDir.Z = 0.0f;
}
projDir.NormalizeFast();
}
return projDir;
}
/// <summary>
/// Starts the timer and animation to squish the player if he got squished in the zookeeper encounter
/// </summary>
public void squish() {
//TODO: implement, + implement a timer so you cant get double squished in 2d
if (squishTimer < 0.0) {
health = health - 3;
squishTimer = 0.0;
isMobile = false;
}
}
/// <summary>
/// Helper method for physUpdate2d and 3d, updates current scale when squishing
/// </summary>
/// <param name="time"></param>
private void squishUpdate(double time) {
squishTimer += time;
if(squishTimer <= 0.125) {
_scale.Y = (float)(25.0 * (0.125 - squishTimer));
} else if(squishTimer <= 3.125) {
_scale.Y = 0.0f;
} else if(squishTimer <= 3.5) {
_scale.Y = (float)(25.0 * (squishTimer - 3.125));
} else {
_scale.Y = 25.0f;
squishTimer = -1.0;
isMobile = true;
}
}
/// <summary>
/// Does a physics update for the player if we are in 3d view
/// </summary>
/// <param name="time">Time elapsed since last update</param>
/// <param name="physList">a pointer to physList, the list of all physics objects</param>
public void physUpdate3d(double time, List<PhysicsObject> physList) {
double origTime = time;
bool collidedWithGround = false;
//first deal with gravity
if(viewSwitchJumpTimer <= 0.0) {
accel.Y -= (float)(gravity * time);
}
//now do acceleration
velocity += accel;
accel.X = 0;
accel.Y = 0;
accel.Z = 0;
//now check for collisions and move
deltax = 0.0f;
List<PhysicsObject> alreadyCollidedList = new List<PhysicsObject>();
while(time > 0.0) {
PhysicsObject collidingObj = null;
float collidingT = 1.0f / 0.0f; //pos infinity
int collidingAxis = -1;
bool endspin = false; // flag to end spin
foreach (PhysicsObject obj in physList) {
//if we are spinning, do a simple non-physics collision test against combat objects
//If we hit a Combat obj, damage it, despwan it w/e, then flag the spin attack to end
if (spinning && obj.collidesIn3d && obj.hascbox && obj != this && !alreadyCollidedList.Contains(obj)) {
//note: we cant do collisions the 'good' way like below with a cbox or pbox
// because we may start spinning with an enemy already inside of our box. Thus we must just simply check whether an enemy
// is inside our 'zone'(location + spinSize in x and z)
//Note: if we implement an enemy with a not square cbox this may look... odd. until then, doing zones is very fast/easy
double enemyZone = (((CombatObject)obj).cbox.X + ((CombatObject)obj).cbox.Z) / 2; // avg(in case z and x are not equal)
double xdist = Math.Abs(obj.location.X - location.X);
double zdist = Math.Abs(obj.location.Z - location.Z);
double ydist = Math.Abs(obj.location.Y - location.Y);
if ((xdist < (enemyZone + spinSize)) && (zdist < (enemyZone + spinSize)) && ydist < ((CombatObject)obj).cbox.Y + cbox.Y) {
if(((CombatObject)obj).type == (int)CombatType.enemy) {// obj is an enemy
((CombatObject)obj).health = ((CombatObject)obj).health - spinDamage;
HasControl = false;
NoControlTimer = 0.5;
knockback(true, obj);
((Enemy)obj).frozen = true;
} else if (((CombatObject)obj).type == (int)CombatType.projectile) { // obj is a projectile, despawn projectile do damage
//if projectile was not spawned by the player, deal with it. Ignore all player spawned projectiles
if (!((Projectile)obj).playerspawned) {
//despawn the projectile
((CombatObject)obj).health = 0;
}
}
else if (((CombatObject)obj).type == 3){ // obj is boss
((BossObject)obj).dodamage(spinDamage);
HasControl = false;
NoControlTimer = 0.5;
knockback(true, obj);
}
endspin = true;
}
}
// don't do collision physics to yourself, or on things you already hit this frame
if(obj.collidesIn3d && obj != this && !alreadyCollidedList.Contains(obj)) {
Vector3 mybox, objbox;