forked from thoth-tech/Asteroids
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
243 lines (192 loc) · 6.48 KB
/
Game.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
using System;
using SplashKitSDK;
using System.Collections.Generic;
using static AsteroidsGame.Program;
public class Game
{
private Window _GameWindow;
private List<Player> _Players;
private Player[] _TempPlayers;
private Level _gameLevel;
private int _playersNo;
public bool GameStarted { get; private set; }
private int _GameOverCount;
private String[] _SpritePacks = { "Shots", "Ships", "Enemies" };
private Music _ambient;
//private Players Player; temp for testing
public Game(Window gameWindow, int playersNo, string? p1Ship, string? p2Ship)
{
_GameWindow = gameWindow;
_playersNo = playersNo;
GameStarted = true;
_GameOverCount = -1;
_Players = new List<Player>();
_TempPlayers = new Player[playersNo];
_Players.Add(new Player(gameWindow, "Player 1", p1Ship, playersNo));
_Players[0].PlayerScore = new Player1Score(gameWindow, "Player 1");
if (playersNo == 2)
{
_Players.Add(new Player(gameWindow, "Player 2", p2Ship, playersNo));
_Players[1].PlayerScore = new Player2Score(gameWindow, "Player 2");
}
SplashKit.FreeAllSprites();
if (!SplashKit.HasSpritePack("Ships")) SplashKit.CreateSpritePack("Ships");
if (!SplashKit.HasSpritePack("Enemies")) SplashKit.CreateSpritePack("Enemies");
if (!SplashKit.HasSpritePack("Shots")) SplashKit.CreateSpritePack("Shots");
//_gameLevel = new Debuglvl(_GameWindow, this);
_gameLevel = new Level1(_GameWindow, this);
//_gameLevel = new Level2(_GameWindow, this);
//_gameLevel = new Jsonlvl(_GameWindow, this, "Level_Test.json");
//_gameLevel = new EndGame(_GameWindow, this);
_ambient = new Music("drone", "spaceship-ambience-with-effects-21420.mp3");
_ambient.Play();
}
public void Nextlvl(Level lvl)
{
_gameLevel = lvl;
}
public List<Player> Players
{
get { return _Players; }
}
public void Draw()
{
_gameLevel.Draw();
SplashKit.SelectSpritePack("Shots");
SplashKit.DrawAllSprites();
SplashKit.SelectSpritePack("Ships");
SplashKit.DrawAllSprites();
SplashKit.SelectSpritePack("Enemies");
SplashKit.DrawAllSprites();
// SplashKit.SelectSpritePack("Default");
// SplashKit.DrawAllSprites();
foreach (Player p in _Players)
{
if (!p.PlayerScore.IsDead)
p.Draw();
p.PlayerScore.Draw();
}
if (_GameOverCount > -1)
{
DrawGameOver();
}
}
public void GameOver()
{
Font _GameFont = new Font("pricedown_bl", "fonts/pricedown_bl.otf");
int FontSize = (int)(120 * gameScale);
_GameWindow.Clear(Color.Black);
foreach (Player p in _Players)
{
p.PlayerScore.Draw();
}
int X_GameText = _GameWindow.Width / 2 - (int)(270 * gameScale);
int Y_GameText = _GameWindow.Height / 3;
foreach (Enemy e in _gameLevel.Enemies)
{
e.freesprite();
}
SplashKit.DrawTextOnWindow(_GameWindow, "Game Over", Color.White, _GameFont, FontSize, X_GameText, Y_GameText);
_GameWindow.Refresh(60);
SplashKit.Delay(5000);
GameStarted = false;
}
public void GameEndCleanup()
{
GameStarted = false;
SplashKit.FreeSpritePack("Shots");
SplashKit.FreeSpritePack("Ships");
SplashKit.FreeSpritePack("Enemies");
}
public void DrawGameOver()
{
Font _GameFont = new Font("pricedown_bl", "fonts/pricedown_bl.otf");
int FontSize = (int)(120 * gameScale);
int X_GameText = _GameWindow.Width / 2 - (int)(270 * gameScale);
int Y_GameText = _GameWindow.Height / 3;
SplashKit.DrawTextOnWindow(_GameWindow, "Game Over", Color.White, _GameFont, FontSize, X_GameText, Y_GameText);
}
public void HandleInput()
{
foreach (Player p in _Players)
{ p.HandleInput(); }
}
public void Updates()
{
bool _GameOver = true;
bool isMusicPlaying = SplashKit.MusicPlaying();
if (!isMusicPlaying)
{
Console.WriteLine($"Backing track is not playing.");
_ambient.Play();
}
for (int i = 0; i < _Players.Count(); i++)
{
if (!_Players[i].PlayerScore.IsDead)
{
_Players[i].Updates();
HitCheck(_Players[i]);
if (_Players[i].IsDead) _Players[i].Respawn(_playersNo);
}
_GameOver = _Players[i].PlayerScore.IsDead ? _GameOver : false;
}
_gameLevel.Update();
SplashKit.SelectSpritePack("Shots");
SplashKit.UpdateAllSprites();
SplashKit.SelectSpritePack("Ships");
SplashKit.UpdateAllSprites();
SplashKit.SelectSpritePack("Enemies");
SplashKit.UpdateAllSprites();
// SplashKit.SelectSpritePack("Default");
// SplashKit.UpdateAllSprites();
foreach (Enemy e in _gameLevel.Enemies)
{
if (e.CanShoot) HitCheck(e);
}
if (_GameOver && _GameOverCount == -1)
{
_GameOverCount = 0;
//GameOver();
}
else if (_GameOverCount > -1)
{
_GameOverCount++;
if (_GameOverCount > 120)
{
GameEndCleanup();
}
}
}
public void HitCheck(Player player)
{
foreach (Enemy e in _gameLevel.Enemies)
{
Tuple<String, int> HitCheckResult = player.HitCheck(e);
if (HitCheckResult.Item1 == "Life") // Look at re-spwarning Player in centre
{
player.PlayerScore.DownLife();
player.Killed();
}
else if (HitCheckResult.Item1 == "Score")
{
player.PlayerScore.ScoreUp(HitCheckResult.Item2);
}
// if (e.CanShoot)
// {
// Tuple<String, int> PlayerHitCheckResult = player.HitCheck(e);
// }
}
}
public void HitCheck(Enemy enemy)
{
foreach (Player p in _Players)
{
Tuple<String, int> HitCheckResult = enemy.HitCheck(p);
if (HitCheckResult.Item1 == "Life") // Look at re-spwarning Player in centre
{
p.PlayerScore.DownLife();
p.Killed();
}
}
}
}