-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame1.cs
299 lines (236 loc) · 10.9 KB
/
Game1.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.IO;
using System;
using System.Collections.Generic;
using ButtonState = Microsoft.Xna.Framework.Input.ButtonState;
using Keys = Microsoft.Xna.Framework.Input.Keys;
using System.Threading;
using Microsoft.Xna.Framework.Media;
namespace MarioTest
{
public class Game1 : Game
{
private GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;
enum GameState { Menu, InGame, GameOver, Win }
GameState state;
static Tile[,] tileArray;
Texture2D[] gameStateTex;
SuperMario superMario;
Enemy enemy;
Pauline pauline;
DonkeyKong donkeyKong;
StreamReader reader;
List<string> strings = new List<string>();
List<Enemy> enemyList = new List<Enemy>();
List<Pauline> paulineList = new List<Pauline>();
List<DonkeyKong> donkeyKongList = new List<DonkeyKong>();
List<ScoreItem> scoreItemList = new List<ScoreItem>();
Random rnd = new Random();
Vector2 enemyPos;
Vector2 paulinePos;
Vector2 donkeyKongPos;
Vector2 scoreItemPos;
Vector2 heartPos;
bool hit = false;
private const int texAdjustment = 40;
private const int donkeyKongTexAdjustment = 80;
int spaceBetweenHearts;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
graphics.PreferredBackBufferWidth = 595;
graphics.PreferredBackBufferHeight = 440;
graphics.ApplyChanges();
}
protected override void Initialize()
{
state = GameState.Menu;
//state = GameState.InGame;
//state = GameState.GameOver;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
//Laddar in texturer, ljudfiler mm från TexManager klassen
TexManager.LoadTextures(Content);
//Hanterar musik
MediaPlayer.IsRepeating = true;
MediaPlayer.Play(TexManager.themeSong);
heartPos = new Vector2(10, 10);
spaceBetweenHearts = 40;
ReadFromTextFile();
//Ger bakgrunder till gamestates
TexBgForStates();
//Kartan ritas ut
Map();
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
switch(state)
{
case GameState.Menu: //Om spelaren trycker Enter --> man kommer in i spelet
{
if(Keyboard.GetState().IsKeyDown(Keys.Enter))
{
state = GameState.InGame;
}
}
break;
case GameState.InGame:
{
superMario.Update(gameTime);
foreach (Enemy enemy in enemyList)
{
enemy.Update(gameTime);
}
superMario.GettingItem(scoreItemList);
foreach (ScoreItem scoreItem in scoreItemList)
{
scoreItem.Update();
}
superMario.EnemyCollision(enemyList, donkeyKongList);
if(SuperMario.CurrentLives == 0)
{
TexManager.donkeyKongSound.Play();
state = GameState.GameOver;
}
superMario.WithPauline(paulineList);
if (SuperMario.Reunited == true)
{
TexManager.winSound.Play();
state = GameState.Win;
}
}
break;
case GameState.GameOver:
{
MediaPlayer.Stop();
}
break;
case GameState.Win:
break;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(gameStateTex[(int)state],
new Vector2(0, 0),
null, Color.White);
if(state == GameState.InGame)
{
GraphicsDevice.Clear(Color.Black);
foreach (Tile tile in tileArray)
{
tile.Draw(spriteBatch);
}
foreach (Enemy enemy in enemyList)
{
enemy.Draw(spriteBatch);
}
foreach(ScoreItem scoreItem in scoreItemList)
{
scoreItem.Draw(spriteBatch);
}
superMario.Draw(spriteBatch);
pauline.Draw(spriteBatch);
donkeyKong.Draw(spriteBatch);
for (int i = 0; i < SuperMario.CurrentLives; i++)
{
spriteBatch.Draw(TexManager.heartTex, new Vector2(heartPos.X + i * spaceBetweenHearts, heartPos.Y), Color.White);
}
}
spriteBatch.End();
base.Draw(gameTime);
}
private void ReadFromTextFile()
{
reader = new StreamReader(@"C:\Users\kimas\OneDrive\Desktop\Projects_and_Solutions\MarioTest\Content\map.txt.txt");
while (!reader.EndOfStream)
{
strings.Add(reader.ReadLine());
}
reader.Close();
}
public static bool GetTileAtPosition(Vector2 vec)
{
return tileArray[(int)vec.X / TexManager.bridgeTex.Width, (int)vec.Y / TexManager.bridgeTex.Height].emptySpace;
}
public void Map()
{
tileArray = new Tile[strings[0].Length, strings.Count];
for (int i = 0; i < tileArray.GetLength(0); i++)
{
for (int j = 0; j < tileArray.GetLength(1); j++)
{
if (strings[j][i] == 'B')
{
tileArray[i, j] = new Tile(TexManager.bridgeTex, new Vector2(TexManager.bridgeTex.Width * i, TexManager.bridgeTex.Height * j), false);
}
else if (strings[j][i] == '-')
{
tileArray[i, j] = new Tile(TexManager.emptyTex, new Vector2(TexManager.emptyTex.Width * i, TexManager.emptyTex.Height * j), true);
}
else if (strings[j][i] == 'P')
{
tileArray[i, j] = new Tile(TexManager.bridgeTex, new Vector2(TexManager.bridgeTex.Width * i, TexManager.bridgeTex.Height * j), false);
pauline = new Pauline(TexManager.paulineTex, paulinePos = new Vector2(TexManager.bridgeTex.Width * i, TexManager.bridgeTex.Height * j), new Rectangle((int)paulinePos.X, (int)paulinePos.Y - texAdjustment, TexManager.
paulineTex.Width, TexManager.paulineTex.Height));
paulineList.Add(pauline);
}
else if (strings[j][i] == 'M')
{
tileArray[i, j] = new Tile(TexManager.bridgeTex, new Vector2(TexManager.bridgeTex.Width * i, TexManager.bridgeTex.Height * j), false);
superMario = new SuperMario(TexManager.marioFrontTex, new Vector2(TexManager.bridgeTex.Width * i, TexManager.bridgeTex.Height * j), hit);
}
else if (strings[j][i] == 'L')
{
tileArray[i, j] = new Tile(TexManager.ladderTex, new Vector2(TexManager.ladderTex.Width * i, TexManager.ladderTex.Height * j), false);
}
else if (strings[j][i] == 'Y')
{
tileArray[i, j] = new Tile(TexManager.bridgeLadderTex, new Vector2(TexManager.bridgeLadderTex.Width * i, TexManager.bridgeLadderTex.Height * j), false);
}
else if (strings[j][i] == 'E')
{
tileArray[i, j] = new Tile(TexManager.bridgeTex, new Vector2(TexManager.bridgeTex.Width * i, TexManager.bridgeTex.Height * j), false);
float enemySpeed = (float)rnd.NextDouble() * 90;
enemy = new Enemy(TexManager.enemyTex, enemyPos = new Vector2(TexManager.bridgeTex.Width * i, TexManager.bridgeTex.Height * j), new Rectangle((int)enemyPos.X, (int)enemyPos.Y - texAdjustment, TexManager.
enemyTex.Width, TexManager.enemyTex.Height), enemySpeed);
enemyList.Add(enemy);
}
else if (strings[j][i] == 'D')
{
tileArray[i, j] = new Tile(TexManager.bridgeTex, new Vector2(TexManager.bridgeTex.Width * i, TexManager.bridgeTex.Height * j), false);
donkeyKong = new DonkeyKong(TexManager.donkeyKongTex, donkeyKongPos = new Vector2(TexManager.bridgeTex.Width * i, TexManager.bridgeTex.Height * j), new Rectangle((int)donkeyKongPos.X, (int)donkeyKongPos.Y - donkeyKongTexAdjustment, TexManager.
donkeyKongTex.Width, TexManager.donkeyKongTex.Height));
donkeyKongList.Add(donkeyKong);
}
else if (strings[j][i] == 'c')
{
tileArray[i, j] = new Tile(TexManager.bridgeTex, new Vector2(TexManager.bridgeTex.Width * i, TexManager.bridgeTex.Height * j), false);
scoreItemList.Add(new ScoreItem(TexManager.coinTex, scoreItemPos = new Vector2(TexManager.bridgeTex.Width * i, TexManager.bridgeTex.Height * j), new Rectangle((int)scoreItemPos.X, (int)scoreItemPos.Y - 20, TexManager.
coinTex.Width, TexManager.coinTex.Height)));
}
}
}
}
public void TexBgForStates()
{
gameStateTex = new Texture2D[4];
gameStateTex[(int)GameState.InGame] = TexManager.emptyTex;
gameStateTex[(int)GameState.Menu] = TexManager.menuScreenTex;
gameStateTex[(int)GameState.GameOver] = TexManager.gameOverScreenTex;
gameStateTex[(int)GameState.Win] = TexManager.winScreenTex;
}
}
}