-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGame1.cs
368 lines (316 loc) · 10.7 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
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
#region Using Statements
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using TiledSharp;
#endregion
namespace Puddle
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Level level;
Player player1;
Controls controls;
Controls pauseControls;
TmxMap map;
Texture2D background;
Texture2D introImage;
List<Texture2D> pauseScreens;
SoundEffect song, bossSong, menuSong;
SoundEffectInstance instance, bossInstance, menuInstance;
Level levelSelect;
bool loadingMap;
bool paused;
bool intro;
string previousMap;
float newMapTimer;
float introScreenTimer;
int slideCount;
const string LEVEL_PATH = "Content/Levels/Level{0}.tmx";
const float LOAD_SCREEN_TIME = 1.5f;
const float INTRO_SCREEN_TIME = 3.0f;
public Game1()
: base()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
string initialLevel = "Menu";
map = new TmxMap(String.Format(LEVEL_PATH, initialLevel));
// Read Level Size From Map
graphics.PreferredBackBufferWidth = map.Width * map.TileWidth;
graphics.PreferredBackBufferHeight = map.Height * map.TileHeight;
paused = false;
intro = false;
introImage = Content.Load<Texture2D>("Slides/Slide1.png");
slideCount = 1;
previousMap = "";
// Create built-in objects
player1 = new Player(
Convert.ToInt32(map.Properties["startX"]),
Convert.ToInt32(map.Properties["startY"])
);
level = new Level(player1, "menu", this.Content);
levelSelect = null;
controls = new Controls();
pauseControls = new Controls();
loadingMap = true;
newMapTimer = LOAD_SCREEN_TIME;
introScreenTimer = INTRO_SCREEN_TIME;
player1.newMap = initialLevel;
song = Content.Load<SoundEffect>("Sounds/InGame.wav");
instance = song.CreateInstance();
instance.IsLooped = true;
bossSong = Content.Load<SoundEffect>("Sounds/Boss.wav");
bossInstance = bossSong.CreateInstance();
bossInstance.IsLooped = true;
menuSong = Content.Load<SoundEffect>("Sounds/Menu.wav");
menuInstance = menuSong.CreateInstance();
menuInstance.IsLooped = true;
menuInstance.Play();
pauseScreens = new List<Texture2D>();
for (int i=0; i < 4; i++)
{
pauseScreens.Add(Content.Load<Texture2D>(String.Format("Slides/pause{0}.png", i)));
}
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: Load all content in level class
player1.LoadContent(this.Content);
foreach (Sprite item in level.items)
item.LoadContent(this.Content);
foreach (Enemy enemy in level.enemies)
enemy.LoadContent(this.Content);
}
protected void LoadMap(string name)
{
player1.ResetFields(true);
// Save state of levelSelect after exiting
if (previousMap.Equals("Select"))
levelSelect = new Level(level);
// Load map and background
map = new TmxMap(String.Format(LEVEL_PATH, name));
graphics.PreferredBackBufferWidth = map.Width * map.TileWidth;
graphics.PreferredBackBufferHeight = map.Height * map.TileHeight;
background = Content.Load<Texture2D>("background.png");
// Decide if we're on the intro
intro = name.Equals("Menu");
// Choose music
if (intro || name.Equals("Select"))
{
instance.Stop();
bossInstance.Stop();
menuInstance.Play();
}
else if (name.Equals("Boss"))
{
instance.Stop();
menuInstance.Stop();
bossInstance.Play();
}
else
{
bossInstance.Stop();
menuInstance.Stop();
instance.Play();
}
// Create new level object
if (name.Equals("Select") && levelSelect != null)
{
level = levelSelect;
}
else
{
level = new Level(player1, name, this.Content);
// Create all objects from tmx and place them in level
foreach (TmxObjectGroup group in map.ObjectGroups)
{
foreach (TmxObjectGroup.TmxObject obj in group.Objects)
{
Type t = Type.GetType(obj.Type);
object item = Activator.CreateInstance(t, obj);
if (item is Enemy)
level.enemies.Add((Enemy)item);
else
level.items.Add((Sprite)item);
}
}
}
if (name.Equals("Select"))
{
// TODO: More adaptable start position handler
String startPosSelect = "";
if (previousMap.Equals("Boss"))
{
startPosSelect = "Boss";
}
else if (Char.IsNumber(previousMap[0]))
{
startPosSelect = previousMap[0].ToString();
}
else
{
startPosSelect = "0";
}
player1.worldPowerUp = null;
player1.spriteX = Convert.ToInt32(map.Properties[String.Format("startX{0}", startPosSelect)]);
player1.spriteY = Convert.ToInt32(map.Properties[String.Format("startY{0}", startPosSelect)]);
}
else
{
player1.spriteX = Convert.ToInt32(map.Properties["startX"]);
player1.spriteY = Convert.ToInt32(map.Properties["startY"]);
}
// Reset player fields
player1.checkpointXPos = player1.spriteX;
player1.checkpointYPos = player1.spriteY;
level.items.Sort((x, y) => x.CompareTo(y));
previousMap = String.Copy(player1.newMap);
player1.newMap = null;
// Load new content
foreach (Sprite item in level.items)
item.LoadContent(this.Content);
foreach (Enemy enemy in level.enemies)
enemy.LoadContent(this.Content);
}
protected override void UnloadContent()
{
Content.Unload();
}
protected override void Update(GameTime gameTime)
{
if (player1.newMap != null)
loadingMap = true;
// Do not update level while loading new map
if (loadingMap){
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
newMapTimer -= elapsed;
if (newMapTimer < 0)
{
LoadMap(player1.newMap);
newMapTimer = LOAD_SCREEN_TIME;
loadingMap = false;
}
return;
}
pauseControls.Update(level);
// Return to level select
if (pauseControls.onPress(Keys.Escape, Buttons.Back) && paused)
{
// Can only return from inside a world
if (level.name.Equals("Select") || level.name.Equals("Menu"))
return;
// Reset powerup from that world to prevent content skipping
if (player1.worldPowerUp != null)
player1.powerup[player1.worldPowerUp] = false;
// Go to new level
player1.newMap = "Select";
paused = false;
}
if (pauseControls.onPress(Keys.Enter, Buttons.Start))
paused = !paused;
if (paused)
return;
controls.Update(level);
player1.Update(controls, level, gameTime);
level.Update();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
if (loadingMap)
{
string levelDisplay = player1.newMap.Equals("Win") ? "Congratulations!" :
String.Format("Level {0}", player1.newMap);
GraphicsDevice.Clear(Color.Black);
TextField message = new TextField(
levelDisplay,
new Vector2(
(graphics.PreferredBackBufferWidth / 2) - 50,
(graphics.PreferredBackBufferHeight / 2) - 50
),
Color.White
);
message.loadContent(Content);
message.draw(spriteBatch);
}
else
{
// Draw background
spriteBatch.Draw(
background,
new Rectangle(
0, 0,
graphics.PreferredBackBufferWidth,
graphics.PreferredBackBufferHeight
),
Color.White
);
// Draw intro slides
if (intro)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
introScreenTimer -= elapsed;
spriteBatch.Draw(
introImage,
new Rectangle(
0, 0,
720,
540
),
Color.White
);
if(introScreenTimer < 0 && slideCount != 5)
{
slideCount += 1;
introImage = Content.Load<Texture2D>(String.Format("Slides/Slide{0}.png", slideCount));
introScreenTimer = INTRO_SCREEN_TIME;
}
}
// Draw contents of the level
level.Draw(spriteBatch);
if (paused && !intro)
{
spriteBatch.Draw(
pauseScreens[player1.numPowers],
new Rectangle(
0, graphics.PreferredBackBufferHeight/2 - 270,
720,
540
),
Color.White
);
}
// Display picked up messages
if (!String.IsNullOrEmpty(level.message))
{
TextField message = new TextField(
level.message,
new Vector2(16, (graphics.PreferredBackBufferHeight ) - 28),
Color.White
);
message.loadContent(Content);
message.draw(spriteBatch);
}
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}