1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.Assets; |
---|
4 | using Jypeli.Controls; |
---|
5 | using Jypeli.Effects; |
---|
6 | using Jypeli.Widgets; |
---|
7 | |
---|
8 | public class Player : GenCharacter |
---|
9 | { |
---|
10 | const int speed = 200; |
---|
11 | const int jumpSpeed = 600; |
---|
12 | const int mass = 10; |
---|
13 | Image portrait = Game.LoadImage("Images/character"); |
---|
14 | Image reversedPortrait = Game.LoadImage("Images/characterreverse"); |
---|
15 | SoundEffect walking = Game.LoadSoundEffect("Sounds/walkingsound"); |
---|
16 | |
---|
17 | string currentWeapon; |
---|
18 | Pistol pistol; |
---|
19 | Rifle rifle; |
---|
20 | |
---|
21 | Timer walker; |
---|
22 | |
---|
23 | public Player(double width, double height, Shape shape, int HP = 100) |
---|
24 | : base(width, height, shape, HP) |
---|
25 | { |
---|
26 | hitPoints = 100; |
---|
27 | AddedToGame += addControls; |
---|
28 | this.Image = portrait; |
---|
29 | this.CollisionIgnoreGroup = 1; |
---|
30 | this.Mass = mass; |
---|
31 | |
---|
32 | // Creating the timer system that is *supposed* to enable a nice walking sound effect |
---|
33 | walker = new Timer(); |
---|
34 | walker.Interval = 0.7; |
---|
35 | walker.Trigger += playWalkingSound; |
---|
36 | |
---|
37 | // Creating the weapon; for now only a pistol |
---|
38 | pistol = new Pistol(20.0, 10.0); |
---|
39 | pistol.X = 18; |
---|
40 | pistol.Y = 11; |
---|
41 | pistol.ProjectileCollision += bulletReachedTarget; |
---|
42 | |
---|
43 | rifle = new Rifle(59.0, 11.0); |
---|
44 | rifle.X = 14; |
---|
45 | rifle.Y = 8; |
---|
46 | rifle.ProjectileCollision += bulletReachedTarget; |
---|
47 | |
---|
48 | this.changeWeapon(rifle.name()); |
---|
49 | } |
---|
50 | |
---|
51 | private void addControls() |
---|
52 | { |
---|
53 | Game.Keyboard.Listen(Key.Right, ButtonState.Down, moveRight, "Moves right", speed); |
---|
54 | Game.Keyboard.Listen(Key.Left, ButtonState.Down, moveLeft, "Moves left", speed); |
---|
55 | Game.Keyboard.Listen(Key.Up, ButtonState.Down, jump, "Jumps", jumpSpeed); |
---|
56 | Game.Keyboard.Listen(Key.Space, ButtonState.Down, shoot, "Shoots"); |
---|
57 | Game.Keyboard.Listen(Key.D1, ButtonState.Pressed, changeWeapon, "Changes weapon to the pistol", pistol.name()); |
---|
58 | Game.Keyboard.Listen(Key.D2, ButtonState.Pressed, changeWeapon, "Changes weapon to the rifle", rifle.name()); |
---|
59 | } |
---|
60 | |
---|
61 | private void changeWeapon(string weaponName) |
---|
62 | { |
---|
63 | if (weaponName == "Pistol") |
---|
64 | { |
---|
65 | this.Weapon = pistol; |
---|
66 | } |
---|
67 | else if (weaponName == "Rifle") |
---|
68 | { |
---|
69 | this.Weapon = rifle; |
---|
70 | } |
---|
71 | else |
---|
72 | { |
---|
73 | throw new Exception("Invalid weapon name: '" + weaponName + "'"); |
---|
74 | } |
---|
75 | |
---|
76 | // We only get here if the weaponName is valid |
---|
77 | currentWeapon = weaponName; |
---|
78 | } |
---|
79 | |
---|
80 | // Because all the weapons have infinite ammo, we don't have to check for null |
---|
81 | private void shoot() |
---|
82 | { |
---|
83 | PhysicsObject bullet = this.Weapon.Shoot(); |
---|
84 | } |
---|
85 | |
---|
86 | private void bulletReachedTarget(PhysicsObject bullet, PhysicsObject target) |
---|
87 | { |
---|
88 | bullet.Destroy(); |
---|
89 | |
---|
90 | if (target.Tag == "MeleeAlien") |
---|
91 | { |
---|
92 | MeleeAlien alien = (MeleeAlien)target; |
---|
93 | |
---|
94 | int damage = 0; |
---|
95 | |
---|
96 | if (currentWeapon == pistol.name()) |
---|
97 | { |
---|
98 | damage = pistol.firePower(); |
---|
99 | } |
---|
100 | else if (currentWeapon == rifle.name()) |
---|
101 | { |
---|
102 | damage = rifle.firePower(); |
---|
103 | } |
---|
104 | |
---|
105 | alien.reduceHitPointsBy(damage); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | private void playWalkingSound(Timer sender) |
---|
110 | { |
---|
111 | this.walking.Play(); |
---|
112 | } |
---|
113 | |
---|
114 | private void moveRight(int speed) |
---|
115 | { |
---|
116 | walker.Start(); |
---|
117 | this.Walk(speed); |
---|
118 | this.Image = portrait; |
---|
119 | } |
---|
120 | |
---|
121 | private void moveLeft(int speed) |
---|
122 | { |
---|
123 | walker.Start(); |
---|
124 | this.Walk(-speed); |
---|
125 | this.Image = reversedPortrait; |
---|
126 | } |
---|
127 | |
---|
128 | public void jump(int jumpSpeed) |
---|
129 | { |
---|
130 | this.Jump(jumpSpeed); |
---|
131 | } |
---|
132 | } |
---|