forked from rliebz/Puddle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
220 lines (183 loc) · 6.33 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
#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;
TmxMap map;
Texture2D background;
SoundEffect song;
SoundEffectInstance instance;
bool newMapLoad;
bool paused;
float newMapTimer;
const float LOAD_SCREEN_TIME = 1.0f;
public Game1()
: base()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
string initialLevel = String.Format("Content/Level{0}.tmx", 1);
map = new TmxMap(initialLevel);
// Read Level Size From Map
graphics.PreferredBackBufferWidth = map.Width * map.TileWidth;
graphics.PreferredBackBufferHeight = map.Height * map.TileHeight;
paused = false;
// Create built-in objects
player1 = new Player(
Convert.ToInt32(map.Properties["startX"]),
Convert.ToInt32(map.Properties["startY"]),
32,
32
);
level = new Level(player1);
controls = new Controls();
newMapLoad = true;
newMapTimer = LOAD_SCREEN_TIME;
player1.newMap = initialLevel;
song = Content.Load<SoundEffect>("Sounds/InGame.wav");
instance = song.CreateInstance();
instance.IsLooped = true;
if (instance.State == SoundState.Stopped)
instance.Play();
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)
{
map = new TmxMap(name);
background = Content.Load<Texture2D>("background.png");
// Read Level Info From Map
graphics.PreferredBackBufferWidth = map.Width * map.TileWidth;
graphics.PreferredBackBufferHeight = map.Height * map.TileHeight;
player1.spriteX = Convert.ToInt32(map.Properties["startX"]);
player1.spriteY = Convert.ToInt32(map.Properties["startY"]);
player1.checkpointXPos = player1.spriteX;
player1.checkpointYPos = player1.spriteY;
// Create new level object
level = new Level(player1);
// 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);
Console.WriteLine(obj.Name);
object item = Activator.CreateInstance(t, obj);
if (item is Enemy)
level.enemies.Add((Enemy)item);
else
level.items.Add((Sprite)item);
}
}
level.items.Sort((x, y) => x.CompareTo(y));
player1.newMap = "";
LoadContent();
newMapLoad = false;
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
controls.Update();
if (controls.onPress(Keys.Escape, Buttons.Back))
Exit();
if (controls.onPress(Keys.Enter, Buttons.Start))
paused = !paused;
if (paused)
return;
player1.Update(controls, level, this.Content, gameTime);
if (!player1.newMap.Equals(""))
{
newMapLoad = true;
}
level.Update(this.Content);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
if (newMapLoad)
{
GraphicsDevice.Clear(Color.Black);
string[] fileName = player1.newMap.Split('.');
string levelNumber = fileName[0].Remove(0, 13);
string levelName = String.Format("Level {0}", levelNumber);
TextField message = new TextField(
levelName,
new Vector2(
(graphics.PreferredBackBufferWidth / 2) - 50,
(graphics.PreferredBackBufferHeight / 2) - 50
),
Color.White
);
message.loadContent(Content);
message.draw(spriteBatch);
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
newMapTimer -= elapsed;
if (newMapTimer < 0)
{
LoadMap(player1.newMap);//Timer expired, execute action
newMapTimer = LOAD_SCREEN_TIME; //Reset Timer
}
}
else
{
// Draw background
spriteBatch.Draw(
background,
new Rectangle(
0, 0,
graphics.PreferredBackBufferWidth,
graphics.PreferredBackBufferHeight
),
Color.White
);
// Draw contents of the level
level.Draw(spriteBatch);
if (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);
}
}
}