-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cs
3227 lines (2758 loc) · 140 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.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
//using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace RedMan
{
/*
* Gun Types
*/
enum Gun { Pistol, Uzi, Sniper, Harpoon, SpaceBlaster, Multidirectional };
/*
* Vehicle Types
*/
enum Vehicle { Submarine, Spaceship, Snowmobile, Broomstick, Car, Wings};
/*
* Implements graphics and physics for player
*/
class Player
{
// Animations
private Animation idleAnimation;
private Animation walkAnimation;
private Animation sprintAnimation;
private Animation jumpAnimation;
private Animation duckAnimation;
private Animation climbAnimation;
private Animation climbIdleAnimation;
private Animation monkeyBarAnimation;
private Animation monkeyBarIdleAnimation;
private Animation celebrateAnimation;
private Animation rollAnimation;
private Animation swimAnimation;
private Animation swimIdleAnimation;
private Animation flyAnimation;
private Animation dieAnimation;
private Animation vehicleAnimation;
// Sudo 3D extra animations
public bool inSudo3D;
private Animation walkUpAnimation;
private Animation walkDownAnimation;
private Texture2D swingPoleTex;
private Vector2 swingOrigin;
private float RotationAngle;
private SpriteEffects flip = SpriteEffects.None;
private AnimationPlayer sprite;
// Sounds
private SoundEffect killedSound;
private SoundEffect jumpSound;
private SoundEffect boingSound;
private SoundEffect fallSound;
private SoundEffect gunReloadSound;
private SoundEffect checkpointSound;
private SpriteFont infoFont;
public Level Level
{
get { return level; }
}
Level level;
public bool IsAlive
{
get { return isAlive; }
}
bool isAlive;
// Physics state
public Vector2 Position
{
get { return position; }
set { position = value; }
}
Vector2 position;
private float previousBottom;
public Vector2 Velocity
{
get { return velocity; }
set { velocity = value; }
}
Vector2 velocity;
// Score for the whole game so far
public static int Score;
// Constants for controling horizontal movement
private const float MoveAcceleration = 13000.0f;
private const float MaxMoveSpeed = 1750.0f;
private const float GroundDragFactor = 0.48f;
private const float AirDragFactor = 0.58f;
// Constants for controlling vertical movement
private const float MaxJumpTime = 0.35f;
private const float JumpLaunchVelocity = -3500.0f;
private const float GravityAcceleration = 3400.0f;
private const float MaxFallSpeed = 550.0f;
private const float JumpControlPower = 0.14f;
private const float MaxTrampolineJumpTime = 5f;
private const float BounceLaunchVelocity = -15000.0f;
private const float BounceControlPower = 10f;
private float timeSinceShot = 50;
// Constants for controlling vertical movement in space
private const float SpaceMaxJumpTime = 2.0f;
private const float SpaceGravityAcceleration = 5000.0f;
private const float SpaceMaxFallSpeed = 150.0f;
private const float SpaceJumpLaunchVelocity = -200.0f;
/// <summary>
/// Gets whether or not the player's feet are on the ground.
/// </summary>
public bool IsOnGround
{
get { return isOnGround; }
set { isOnGround = value; }
}
bool isOnGround;
/// <summary>
/// Current user movement input.
/// </summary>
private float movement;
public float vertMovement;
// Player state
private float playerHealth;
private float playerMaxHealth = 100.0f;
public bool isJumping;
private bool wasJumping;
private float jumpTime;
private bool isDucking;
private bool onLadder;
private bool onMonkeyBar;
public bool fallDown;
private bool onSlope;
private bool wasOnSlope;
public bool isSprinting;
private bool onSwingPole;
private bool swingPoleRight;
private bool onTrampoline;
private bool onRightConveyor;
private bool onLeftConveyor;
private bool inWind;
private float windCounter = 0;
private bool inSpace;
private bool inAirLock;
private bool onRamp;
private const float RampDelay = 150;
private float rampTimer = RampDelay;
private bool onOilSlick;
private const float OilSlickDelay = 100;
private float oilSlickTimer = OilSlickDelay;
private bool onBooster;
private const float BoosterDelay = 1000;
private float boosterTimer = BoosterDelay;
public bool shootWithMouse = false;
public Vector2 mousePos;
public Vector2 bulletVector;
// Position of player within game window (ie not whole level)
public Vector2 relativePosition;
// Sublevel info
public bool atDoor;
public int levelToLoad;
// Vehicle state
public bool inVehicle;
public Vehicle currVehicle;
// Automatic vehicles move by themselves (horizontally)
private bool inAutomaticVehicle;
private bool automaticVehicleRight;
private float automaticVehicleSpeed;
public bool carRaceMode;
private const float NormalVehicleSpeed = 3;
// Automatic player variables
private bool automaticRun;
private bool automaticRunRight;
private bool automaticRunUp = false;
private float automaticRunSpeed;
private float automaticRunVertSpeed = 4;
private bool automaticJump;
private List<Vector2> automaticJumpPoints = new List<Vector2>();
private const float NormalRunSpeed = 1;
private const float FastRunSpeed = 2;
// Weapon state
public bool hasGun;
public int powerBulletCount;
private bool isShooting;
public Gun currGun;
private float reloadTime;
public bool sniperMode;
// Level specific
public bool showStats;
public int level2AmuletCount = 0;
public int level2AmuletMax = 5;
public int level4StarCount = 0;
public int level4StarMax = 5;
public int level6PumpkinCount = 0;
public int level6PumpkinMax = 6;
public int level11StarCount = 0;
public int level11StarMax = 4;
private bool periodicLoop = false;
private int periodCounter = 199;
public bool DrawInfo
{
get { return drawInfo; }
set { drawInfo = value; }
}
bool drawInfo;
public string InfoString
{
get { return infoString; }
set { infoString = value; }
}
string infoString;
private int infoTimer;
private const int InfoTimerMax = 300;
public bool FacingRight
{
get { return facingRight; }
}
bool facingRight = true;
public bool IsSwimming
{
get { return isSwimming; }
}
bool isSwimming;
public bool OnWaterSurface
{
get { return onWaterSurface; }
}
bool onWaterSurface;
private Rectangle localBounds;
// Gets a rectangle which bounds this player in world space.
public Rectangle BoundingRectangle
{
get
{
int left = (int)Math.Round(Position.X - sprite.Origin.X);
int top = (int)Math.Round(Position.Y - sprite.Origin.Y);
return new Rectangle(left, top, idleAnimation.FrameWidth, idleAnimation.FrameHeight);
}
}
// Gets a small rectangle which bounds this player in world space.
public Rectangle SmallBoundingRectangle
{
get
{
int left = (int)Math.Round(Position.X - sprite.Origin.X + localBounds.X);
int top = (int)Math.Round(Position.Y - sprite.Origin.Y + localBounds.Y);
return new Rectangle(left, top, localBounds.Width, localBounds.Height);
}
}
// Constructors a new player.
public Player(Level level, Vector2 position)
{
this.level = level;
this.showStats = true;
this.inSudo3D = level.inSudo3D();
hasGun = false;
this.carRaceMode = level.inCarRaceMode();
if (carRaceMode)
{
inVehicle = true;
currVehicle = Vehicle.Car;
inAutomaticVehicle = true;
automaticVehicleRight = true;
automaticVehicleSpeed = NormalVehicleSpeed;
}
this.automaticRun = level.inRunningMode();
if (automaticRun)
{
automaticRunRight = true;
automaticRunSpeed = NormalRunSpeed;
this.playerHealth = playerMaxHealth;
}
sniperMode = level.inSniperMode();
if (level.LevelIndex == 10)
AssignAutomaticJumpPoints();
LoadContent();
Reset(position);
}
// Special case for level 10
// Adds all automated jump point to list of vectors
private void AssignAutomaticJumpPoints()
{
CreateJumpPoint(10, 23);
CreateJumpPoint(37, 23);
CreateJumpPoint(41, 21);
CreateJumpPoint(54, 19);
CreateJumpPoint(98, 21);
CreateJumpPoint(105, 18);
CreateJumpPoint(119, 20);
CreateJumpPoint(127, 18);
CreateJumpPoint(161, 11);
CreateJumpPoint(173, 9);
CreateJumpPoint(190, 23);
CreateJumpPoint(224, 21);
CreateJumpPoint(240, 17);
CreateJumpPoint(254, 17);
CreateJumpPoint(264, 9);
CreateJumpPoint(274, 13);
CreateJumpPoint(262, 21);
CreateJumpPoint(279, 21);
CreateJumpPoint(345, 16);
CreateJumpPoint(367, 16);
CreateJumpPoint(392, 21);
CreateJumpPoint(403, 18);
CreateJumpPoint(447, 22);
CreateJumpPoint(423, 18);
CreateJumpPoint(431, 14);
CreateJumpPoint(433, 12);
CreateJumpPoint(436, 14);
CreateJumpPoint(437, 5);
}
// Helper method for AssignAutomaticJumpPoints()
private void CreateJumpPoint(int x, int y)
{
automaticJumpPoints.Add(new Vector2(x, y));
}
// Loads the player sprite sheet and sounds.
public void LoadContent()
{
// Gets whether in space/water etc.
UpdateLevelSpecificState();
if (carRaceMode)
{
idleAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "vehicle"), 0.1f, false);
walkAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "vehicle"), 0.1f, true);
jumpAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "jump"), 0.15f, false);
rollAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "spin"), 0.1f, true);
//celebrateAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/Celebrate"), 0.1f, false);
dieAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/Die"), 0.1f, false);
}
else
{
// Load animated textures depending on level
idleAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "Idle"), 0.1f, true);
walkAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "Walk"), 0.1f, true);
jumpAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "Jump"), 0.1f, false);
duckAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "Duck"), 0.1f, false);
sprintAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "Sprint"), 0.1f, true);
rollAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "Roll"), 0.1f, true);
vehicleAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "vehicle"), 0.1f, true);
climbAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "Climb"), 0.1f, true);
climbIdleAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "Climb"), 0.1f, false);
if (inSudo3D)
{
walkUpAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "WalkUp"), 0.1f, true);
walkDownAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "WalkDown"), 0.1f, true);
}
celebrateAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/Celebrate"), 0.1f, false);
if (level.LevelIndex == 4 || level.LevelIndex == 5 || level.LevelIndex == 10)
dieAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/Die2"), 0.1f, false);
else
dieAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/Die"), 0.1f, false);
// If playing as square player
if (level.LevelIndex == 10)
{
monkeyBarAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "MonkeyBar"), 0.1f, true);
monkeyBarIdleAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "MonkeyBar"), 0.1f, false);
}
else
{
monkeyBarAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/MonkeyBar"), 0.1f, true);
monkeyBarIdleAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/MonkeyBar"), 0.1f, false);
}
swimAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/Swim"), 0.1f, true);
swimIdleAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/SwimIdle"), 0.1f, true);
// If playing as square player
if (level.LevelIndex == 10)
flyAnimation = new Animation(Level.Content.Load<Texture2D>(GetPlayerTheme() + "Fly"), 0.1f, true);
else
flyAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/Fly"), 0.1f, true);
swingPoleTex = level.Content.Load<Texture2D>("Sprites/Player/Swing");
swingOrigin.X = swingPoleTex.Width / 2;
swingOrigin.Y = swingPoleTex.Height / 10;
}
// Calculate bounds within texture size.
int width = (int)(idleAnimation.FrameWidth * 0.4);
int left = (idleAnimation.FrameWidth - width) / 2;
int height = (int)(idleAnimation.FrameWidth * 0.8);
int top = idleAnimation.FrameHeight - height;
localBounds = new Rectangle(left, top, width, height);
// Load sounds.
killedSound = Level.Content.Load<SoundEffect>("Sounds/PlayerKilled");
jumpSound = Level.Content.Load<SoundEffect>("Sounds/PlayerJump");
boingSound = Level.Content.Load<SoundEffect>("Sounds/Boing");
fallSound = Level.Content.Load<SoundEffect>("Sounds/PlayerFall");
gunReloadSound = Level.Content.Load<SoundEffect>("Sounds/GunReload");
checkpointSound = Level.Content.Load<SoundEffect>("Sounds/Checkpoint");
// Fonts
infoFont = Level.Content.Load<SpriteFont>("Fonts/Hud");
}
private string GetPlayerTheme()
{
string location = "Sprites/Player/";
switch (level.LevelIndex)
{
case -1: return location + "City/";
case 4: return location + "Space/";
case 5: return location + "Snow/";
case 6: return location + "House/";
case 7: return location + "Street/";
case 8: return location + "Race/";
case 10: return location + "Sniper/";
case 11: return location + "Heaven/";
case 12: return location + "Hell/";
default: return location;
}
}
/// Resets the player (brings him back to life).
public void Reset(Vector2 position)
{
Level.Score -= 100;
Position = position;
Velocity = Vector2.Zero;
isAlive = true;
level.movingPlatformContact = false;
// At moment player keeps gun if already collected
powerBulletCount = 0;
// If player has health bar restore health to max
if (automaticRun)
this.playerHealth = playerMaxHealth;
if (carRaceMode)
automaticVehicleSpeed = NormalVehicleSpeed;
sprite.PlayAnimation(idleAnimation);
}
// Handles input, performs physics, and animates the player sprite.
public void Update(
GameTime gameTime,
KeyboardState keyboardState,
MouseState mouseState,
DisplayOrientation orientation)
{
infoTimer++;
if (infoTimer > InfoTimerMax)
{
drawInfo = false;
infoTimer = 0;
}
GetInput(keyboardState, mouseState, orientation);
ApplyPhysics(gameTime);
if (inVehicle && !carRaceMode)
sprite.PlayAnimation(vehicleAnimation);
else if (level.LevelIndex == 11 && inSpace)
sprite.PlayAnimation(flyAnimation);
else if (inSudo3D)
{
if (carRaceMode && onRamp)
sprite.PlayAnimation(jumpAnimation);
else if (carRaceMode && onOilSlick)
sprite.PlayAnimation(rollAnimation);
else if (Math.Abs(Velocity.X) - 0.02f > 0)
sprite.PlayAnimation(walkAnimation);
else if (vertMovement > 0)
sprite.PlayAnimation(walkDownAnimation);
else if (vertMovement < 0)
sprite.PlayAnimation(walkUpAnimation);
else
sprite.PlayAnimation(idleAnimation);
}
else if (IsAlive && IsOnGround)
{
if (onSlope)
sprite.PlayAnimation(rollAnimation);
else if (isSwimming)
sprite.PlayAnimation(swimAnimation);
else if ((onRightConveyor || onLeftConveyor) && movement == 0)
sprite.PlayAnimation(idleAnimation);
// Ignores small movements
else if (isSprinting && Math.Abs(Velocity.X) - 0.02f > 0)
{
sprite.PlayAnimation(sprintAnimation);
}
else if (Math.Abs(Velocity.X) - 0.02f > 0)
{
sprite.PlayAnimation(walkAnimation);
}
else if (isDucking)
{
sprite.PlayAnimation(duckAnimation);
}
else
{
sprite.PlayAnimation(idleAnimation);
}
}
else if (inWind)
sprite.PlayAnimation(flyAnimation);
else if (onWaterSurface)
sprite.PlayAnimation(swimIdleAnimation);
else if (isSwimming)
sprite.PlayAnimation(swimAnimation);
// If falling downwards, crouch into ball
else if (Velocity.Y > 0)
{
sprite.PlayAnimation(idleAnimation);
}
// Clear input.
movement = 0.0f;
isJumping = false;
}
// Gets player movement and jump commands from input.
private void GetInput(
KeyboardState keyboardState,
MouseState mouseState,
DisplayOrientation orientation)
{
if (!onSlope)
{
float moveSpeed;
if (isDucking)
moveSpeed = 0;
else if (keyboardState.IsKeyDown(Keys.LeftControl) && (!isSwimming || (isSwimming && inVehicle)) && !onMonkeyBar)
{
moveSpeed = 2.0f;
isSprinting = true;
}
else if (onLeftConveyor || onRightConveyor)
{
moveSpeed = 1.5f;
isSprinting = false;
}
else if (onMonkeyBar)
{
moveSpeed = 0.5f;
isSprinting = false;
}
else
{
moveSpeed = 1.0f;
isSprinting = false;
}
// If any digital horizontal movement input is found, override the analog movement.
if (onSwingPole)
{
if (keyboardState.IsKeyDown(Keys.Left) ||
keyboardState.IsKeyDown(Keys.A))
{
swingPoleRight = false;
facingRight = false;
}
else if (keyboardState.IsKeyDown(Keys.Right) ||
keyboardState.IsKeyDown(Keys.D))
{
swingPoleRight = true;
facingRight = true;
}
}
else
{
if (keyboardState.IsKeyDown(Keys.Left) ||
keyboardState.IsKeyDown(Keys.A))
{
movement = -moveSpeed;
facingRight = false;
}
else if (keyboardState.IsKeyDown(Keys.Right) ||
keyboardState.IsKeyDown(Keys.D))
{
movement = moveSpeed;
facingRight = true;
}
}
if(isOnGround)
isDucking = keyboardState.IsKeyDown(Keys.Down);
if (onWaterSurface)
{
if (keyboardState.IsKeyDown(Keys.Down))
{
isSwimming = true;
onWaterSurface = false;
vertMovement = 3.0f;
}
}
if (onLadder || isSwimming || inWind || (inVehicle && !inAutomaticVehicle) || inSudo3D)
{
isDucking = false;
if (automaticRun && automaticRunUp)
vertMovement = -3.0f;
else if (keyboardState.IsKeyDown(Keys.Up))
vertMovement = -3.0f;
else if (keyboardState.IsKeyDown(Keys.Down))
vertMovement = 3.0f;
else vertMovement = 0.0f;
if (inSudo3D && keyboardState.IsKeyDown(Keys.LeftControl))
vertMovement *= 2;
}
if (onMonkeyBar)
{
fallDown = keyboardState.IsKeyDown(Keys.Down);
}
}
if (!isDucking)
{
// Check if the player wants to jump.
isJumping =
((keyboardState.IsKeyDown(Keys.Space) ||
keyboardState.IsKeyDown(Keys.Up) ||
keyboardState.IsKeyDown(Keys.W)) && level.LevelIndex != 10) ||
(onTrampoline ||
automaticJump);
if (!onSwingPole && hasGun)
{
if (shootWithMouse)
{
mouseState = Mouse.GetState();
// Get current mouse position
mousePos = new Vector2(mouseState.X, mouseState.Y);
// Need to compensate for car velocity (fix this later)
if (carRaceMode)
mousePos += new Vector2(180, 0);
// Find top-left camera position
int left = (int)Math.Floor(level.cameraPosition.X);
int top = (int)Math.Floor(level.cameraPosition.Y);
// Get player's position relative to top-left of game window
relativePosition = new Vector2(this.Position.X - left, this.Position.Y - top);
// Get movement vector for bullet and normalize
bulletVector = mousePos - this.relativePosition;
if (bulletVector != Vector2.Zero)
bulletVector.Normalize();
// Determine if player wants to shoot
isShooting = mouseState.LeftButton == ButtonState.Pressed;
}
else
isShooting = keyboardState.IsKeyDown(Keys.S);
}
}
}
// Updates the player's velocity and position based on input, gravity, etc.
public void ApplyPhysics(GameTime gameTime)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
Vector2 previousPosition = Position;
// Base velocity is a combination of horizontal movement control and
// acceleration downward due to gravity.
if (inVehicle && inAutomaticVehicle)
{
if (automaticVehicleRight)
velocity.X = automaticVehicleSpeed * MoveAcceleration * elapsed;
else
velocity.X = -automaticVehicleSpeed * MoveAcceleration * elapsed;
}
// Only allow horizontal movement in automaticRun mode on level 11
else if (automaticRun && level.LevelIndex != 11)
{
if (automaticRunRight)
velocity.X = automaticRunSpeed * MoveAcceleration * elapsed;
else
velocity.X = -automaticRunSpeed * MoveAcceleration * elapsed;
}
else if (onSlope)
{
velocity.X = 2 * MoveAcceleration * elapsed;
wasOnSlope = true;
}
else if (wasOnSlope && isJumping)
{
velocity.X = 2 * MoveAcceleration * elapsed;
}
else if (onRightConveyor)
velocity.X = 1 * MoveAcceleration * elapsed + movement * MoveAcceleration * elapsed;
else if (onLeftConveyor)
velocity.X = -1 * MoveAcceleration * elapsed + movement * MoveAcceleration * elapsed;
else
velocity.X += movement * MoveAcceleration * elapsed;
if (onLadder || isSwimming || onWaterSurface || onMonkeyBar || (inVehicle && !inAutomaticVehicle) || inSudo3D)// || onSwingPole)
velocity.Y = 0;
// If in wind make character flutter up and down alternately
else if (inWind)
{
windCounter++;
if (windCounter < 10)
velocity.Y = -100;
else if (windCounter < 18)
velocity.Y = 100;
else windCounter = 0;
}
else
{
if (inSpace)
velocity.Y = MathHelper.Clamp(velocity.Y + SpaceGravityAcceleration * elapsed, -SpaceMaxFallSpeed, SpaceMaxFallSpeed);
else
velocity.Y = MathHelper.Clamp(velocity.Y + GravityAcceleration * elapsed, -MaxFallSpeed, MaxFallSpeed);
}
velocity.Y = DoJump(velocity.Y, gameTime);
// Apply pseudo-drag horizontally.
if (IsOnGround)
velocity.X *= GroundDragFactor;
else
velocity.X *= AirDragFactor;
// Prevent the player from running faster than his top speed.
velocity.X = MathHelper.Clamp(velocity.X, -MaxMoveSpeed, MaxMoveSpeed);
if (!isAlive)
Velocity = Vector2.Zero;
// Apply velocity.
Position += velocity * elapsed;
Position = new Vector2((float)Math.Round(Position.X), (float)Math.Round(Position.Y));
if (onSwingPole)
{
if (swingPoleRight)
RotationAngle -= 8*elapsed;
else
RotationAngle += 8*elapsed;
float circle = MathHelper.Pi * 2;
RotationAngle = RotationAngle % circle;
}
if (onMonkeyBar)
{
if (velocity.X != 0)
sprite.PlayAnimation(monkeyBarAnimation);
else
sprite.PlayAnimation(monkeyBarIdleAnimation);
if (fallDown)
position.Y += 33;
}
if (onLadder || isSwimming || inWind || (inVehicle && !inAutomaticVehicle) || inSudo3D) // || onWaterSurface)
{
position.Y += vertMovement;
if (onLadder)
{
if (vertMovement == 0)
sprite.PlayAnimation(climbIdleAnimation);
else
sprite.PlayAnimation(climbAnimation);
}
else if (isSwimming)
{
Animation swimType = (inVehicle && currVehicle == Vehicle.Submarine) ? vehicleAnimation : swimAnimation;
if (level.LevelIndex == 2)
sprite.PlayAnimation(swimType);
else
sprite.PlayAnimation(swimAnimation);
}
}
timeSinceShot++;
if (powerBulletCount == 0)
{
Bullet.DropBomb = false;
if (hasGun)
UpdateReloadTime();
}
if (isShooting && timeSinceShot > reloadTime)
{
// Slight bullet offset from player depending on which direction we are firing
int bulletOffsetX = 0;
// Having 0 bullet offset when firing with mouse makes shots more accurate
int bulletOffsetY = (shootWithMouse) ? 0 : -Tile.Width/2;
Vector2 pos;
if (sniperMode)
pos = mousePos + new Vector2(level.cameraPosition.X, level.cameraPosition.Y);
else
pos = new Vector2(position.X + bulletOffsetX, position.Y + bulletOffsetY);
bool inWater = isSwimming || onWaterSurface;
// If out of water cannot fire harpoon / if in water can only fire harpoon
if ((!inWater && currGun != Gun.Harpoon) || (inWater && currGun == Gun.Harpoon))
{
level.Bullets.Add(new Bullet(level, pos, gameTime, (Turret)null));
timeSinceShot = 0;
if (powerBulletCount > 0)
powerBulletCount--;
}
}
// If the player is now colliding with the level, separate them.
HandleCollisions();
UpdateLevelSpecificState();
// If the collision stopped us from moving, reset the velocity to zero.
if (Position.X == previousPosition.X)
velocity.X = 0;
if (Position.Y == previousPosition.Y)
velocity.Y = 0;
}
// Updates gun reload time every time player picks up a gun
private void UpdateReloadTime()
{
// Default to false and set to true if required
shootWithMouse = false;
if (powerBulletCount > 0)
reloadTime = Bullet.powerBulletReload;
else if (inVehicle)
{
switch (currVehicle)
{
case Vehicle.Submarine:
reloadTime = Bullet.uziReload;
break;
case Vehicle.Spaceship:
reloadTime = Bullet.spaceBlasterReload;
break;
case Vehicle.Snowmobile:
reloadTime = Bullet.spaceBlasterReload;
break;
case Vehicle.Car:
reloadTime = Bullet.multiDirectionalReload;
shootWithMouse = true;
break;
case Vehicle.Wings:
reloadTime = Bullet.multiDirectionalReload;
shootWithMouse = (level.LevelIndex == 11);
break;
default:
reloadTime = Bullet.uziReload;
break;
}
}
else
{
switch (currGun)
{
case Gun.Pistol:
reloadTime = Bullet.pistolReload; break;
case Gun.Uzi:
reloadTime = Bullet.uziReload; break;
case Gun.Sniper:
reloadTime = Bullet.sniperReload; break;
case Gun.Harpoon:
reloadTime = Bullet.harpoonReload; break;
case Gun.SpaceBlaster:
reloadTime = Bullet.spaceBlasterReload; break;
case Gun.Multidirectional:
reloadTime = (sniperMode) ? Bullet.sniperReload : Bullet.multiDirectionalReload;
shootWithMouse = true;
break;
}
}
}
/* Calculates the Y velocity accounting for jumping and
* animates accordingly.
* During the accent of a jump, the Y velocity is completely
* overridden by a power curve. During the decent, gravity takes
* over. The jump velocity is controlled by the jumpTime field
* which measures time into the accent of the current jump.
*/
private float DoJump(float velocityY, GameTime gameTime)
{
// If the player wants to jump
if (!onLadder && isJumping)
{
// Begin or continue a jump
if ((!wasJumping && (IsOnGround || onWaterSurface)) || jumpTime > 0.0f) // add monkey bar jump here if required
{
if (jumpTime == 0.0f)
{
if (onTrampoline)
Game.PlaySound(boingSound);
else
Game.PlaySound(jumpSound);
}
jumpTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
sprite.PlayAnimation(jumpAnimation);
}
// If we are in the ascent of the jump
float maxTime;
if (inSpace)
maxTime = SpaceMaxJumpTime;
else
maxTime = (onTrampoline) ? MaxTrampolineJumpTime : MaxJumpTime;
if (0.0f < jumpTime && jumpTime <= maxTime)
{
// Fully override the vertical velocity with a power curve that gives players more control over the top of the jump
if (onTrampoline)
velocityY = BounceLaunchVelocity * (1.0f - (float)Math.Pow(jumpTime / MaxTrampolineJumpTime, BounceControlPower));
else if (inSpace)
velocityY = SpaceJumpLaunchVelocity * (1.0f - (float)Math.Pow(jumpTime / MaxTrampolineJumpTime, BounceControlPower));
else