-
Notifications
You must be signed in to change notification settings - Fork 6
/
Player.cs
259 lines (220 loc) · 7.15 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
using System;
using SplashKitSDK;
using System.Collections.Generic;
using static AsteroidsGame.Program;
using System.IO;
public class Player
{
private Window _gameWindow;
public double X { get; private set; }
public double Y { get; private set; }
private Bitmap _Ship;
private double _Angle;
private string _Player;
private List<Shooting> _shots = new List<Shooting>();
//private List<Shooting> _KillShots = new List<Shooting>();
public bool IsDead { get; private set; }
private SplashKitSDK.Timer _InvulnerableTime;
//private bool _IsInvulnerable;
public Score PlayerScore { get; set; }
public bool IsInvulnerable { get; private set; }
public string Name { get { return _Player; } }
public SoundEffect shootSound;
public SoundEffect[] collisionSounds;
public Player(Window gameWindow, string Player, string PlayerShip, int PlayersNo)
{
_gameWindow = gameWindow;
_Ship = SplashKit.LoadBitmap(Player, PlayerShip);
_Player = Player;
collisionSounds = new SoundEffect[]
{
new SoundEffect("hit1", "ship-explode-01.ogg"),
new SoundEffect("hit2", "ship-explode-02.ogg"),
new SoundEffect("hit3", "ship-explode-03.ogg")
};
Respawn(PlayersNo);
shootSound = new SoundEffect("laser", "blaster-2-81267.ogg");
}
public void PlayRandomCollisionSound()
{
int randomIndex = new Random().Next(0, collisionSounds.Length);
collisionSounds[randomIndex].Play(0.5f);
}
public void Respawn(int PlayersNo)
{
_Angle = 0;
_shots = new List<Shooting>();
IsDead = false;
_InvulnerableTime = new SplashKitSDK.Timer($"{_Player} Invulnerable");
_InvulnerableTime.Start();
IsInvulnerable = true;
if (PlayersNo == 1)
{
Y = (_gameWindow.Height - _Ship.Height) / 2;
X = (_gameWindow.Width - _Ship.Width) / 2;
}
else
{
int gameWindow_8th = _gameWindow.Width / 3;
if (_Player == "Player 1")
{
Y = (_gameWindow.Height - _Ship.Height) / 2;
X = (gameWindow_8th - _Ship.Width / 2);
}
else if (_Player == "Player 2")
{
Y = (_gameWindow.Height - _Ship.Height) / 2;
X = (gameWindow_8th * 2 - _Ship.Width / 2);
}
}
}
public void Killed()
{
IsDead = true;
}
public void Draw()
{
foreach (Shooting s in _shots)
{
s.Draw();
}
if (IsInvulnerable)
{
if ((_InvulnerableTime.Ticks / 250) % 2 == 0)
{
_Ship.Draw(X, Y, SplashKit.OptionRotateBmp(_Angle));
}
/* Point2D XY = new Point2D() {X=X,Y=Y};
_Ship.Draw(X,Y,SplashKit.OptionRotateBmp(_Angle));
SplashKit.DrawCircle(Color.AliceBlue,_Ship.BoundingCircle(XY)); */
}
else
{
_Ship.Draw(X, Y, SplashKit.OptionRotateBmp(_Angle));
}
}
private void Rotation(double change)
{
_Angle = (_Angle + change); //% 360
}
private void Move(double Speed)
{
Vector2D XY_Change = new Vector2D();
Matrix2D Rotate = SplashKit.RotationMatrix(_Angle + 270);
XY_Change.X += Speed;
XY_Change = SplashKit.MatrixMultiply(Rotate, XY_Change);
X += XY_Change.X;
Y += XY_Change.Y;
}
public void HandleInput()
{
if (_Player == "Player 1")
{ Player1Controls(); }
else if (_Player == "Player 2")
{ Player2Controls(); }
OutOfBounds();
}
private void Player1Controls()
{
double MoveSpeed = 5 * gameScale;
double RotateSpeed = 5;
if (SplashKit.KeyDown(Controls.Keylookup("P1_left"))) Rotation(-RotateSpeed);
if (SplashKit.KeyDown(Controls.Keylookup("P1_right"))) Rotation(RotateSpeed);
if (SplashKit.KeyDown(Controls.Keylookup("P1_up")) || SplashKit.KeyDown(Controls.Keylookup("P1_button2"))) Move(MoveSpeed);
if (SplashKit.KeyTyped(Controls.Keylookup("P1_button1"))) { Shoot(); }
}
private void Player2Controls()
{
double MoveSpeed = 5 * gameScale;
double RotateSpeed = 5;
if (SplashKit.KeyDown(Controls.Keylookup("P2_left"))) Rotation(-RotateSpeed);
if (SplashKit.KeyDown(Controls.Keylookup("P2_right"))) Rotation(RotateSpeed);
if (SplashKit.KeyDown(Controls.Keylookup("P2_up")) || SplashKit.KeyDown(Controls.Keylookup("P2_button2"))) Move(MoveSpeed);
if (SplashKit.KeyTyped(Controls.Keylookup("P2_button1"))) { Shoot(); }
}
private void OutOfBounds()
{
if (X < 0 - _Ship.Width) X = _gameWindow.Width;
else if (X > _gameWindow.Width) X = 0 - _Ship.Width;
else if (Y < 0 - _Ship.Height) Y = _gameWindow.Height;
else if (Y > _gameWindow.Height) Y = 0 - _Ship.Height;
}
private void Shoot()
{
shootSound.Play(0.5f);
Shooting ShotType = new PlayerShot(_Ship.Center, _Angle, this);
_shots.Add(ShotType);
}
public void Updates()
{
for (int i = 0; i < _shots.Count; i++)
{
_shots[i].Update();
if (_shots[i].IsOffscreen(_gameWindow))
{
_shots[i].freesprite();
_shots.RemoveAt(i--);
}
}
/*
foreach (Shooting s in _shots)
{
s.Update();
if (s.IsOffscreen(_gameWindow)) _KillShots.Add(s);
}
foreach (Shooting s in _KillShots) // avoiding this, to reduce comparisons for performance.
{ _shots.Remove(s); }
_KillShots.Clear();
*/
if (_InvulnerableTime.Ticks > 1500) //1500
{
IsInvulnerable = false;
_InvulnerableTime.Stop();
_InvulnerableTime.Reset();
}
}
public Bitmap HitBMP()
{
return _Ship;
}
public Tuple<String, int> HitCheck(Enemy enemy)
{
if (!IsInvulnerable && !IsDead)
{
bool hit = false;
foreach (Circle e in enemy.HitCircle())
{
if (SplashKit.BitmapCircleCollision(_Ship, X, Y, e))
{
hit = true;
PlayRandomCollisionSound();
}
}
if (enemy.HitSprite() != null)
{
if (SplashKit.SpriteBitmapCollision(enemy.HitSprite(), _Ship, X, Y)) hit = true;
}
if (hit) return enemy.HitBy(this);
}
for (int i = 0; i < _shots.Count; i++)
{
Shooting s = _shots[i];
if (s.HitCheck(enemy))
{
_shots.RemoveAt(i--);
return enemy.HitBy(s); // free sprite is called inside hitby currently
}
}
/*
foreach (Shooting s in _shots)
{
if (s.HitCheck(enemy))
{
_KillShots.Add(s);
return enemy.HitBy(s);
}
}
*/
return new Tuple<string, int>("False", 0);
}
}