-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game1.cs
358 lines (328 loc) · 18.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
namespace gameBungee
{
static public class EnvironmentVariable
{
static public World worldPhysic;
static public GraphicsDeviceManager graphics;
static public int width;
static public int height;
static public List<ObjCercle> lCercle = new List<ObjCercle>();
static public List<ObjCercle> lCerclePol = new List<ObjCercle>();
static public List<ObjEdge> lEdge = new List<ObjEdge>();
static public List<ObjRectange> lRectangle = new List<ObjRectange>();
static public List<ObjPolygon> lPolygon = new List<ObjPolygon>();
static public ContentManager content;
}
public enum TextureImg { Null, tree1, tree2, tree3, tree4, tree5,
tree6, tree7, tree8, tree9, tree10, grass, Run, Idle
};
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
SpriteBatch spriteBatch;
private Level level;
private SamplerState _clampTextureAddressMode;
public Game1()
{
EnvironmentVariable.graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
EnvironmentVariable.content = Content;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
EnvironmentVariable.graphics.IsFullScreen = false;
EnvironmentVariable.width = 800;
EnvironmentVariable.height = 500;
EnvironmentVariable.graphics.PreferredBackBufferWidth = EnvironmentVariable.width;
EnvironmentVariable.graphics.PreferredBackBufferHeight = EnvironmentVariable.height;
EnvironmentVariable.graphics.ApplyChanges();
this.Window.Title = "Premier programme";
this.Window.AllowUserResizing = false;
level = new Level(Services, EnvironmentVariable.width, EnvironmentVariable.height/*this.Window.ClientBounds.Width, this.Window.ClientBounds.Height*/);
loadMap("C:\\Documents and Settings\\yoann\\Bureau\\level1.xml");
base.IsMouseVisible = true;
base.Initialize();
}
private void loadMap(string filename)
{
using (XmlReader reader = XmlReader.Create(filename))
{
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "Circles":
Vector2 pos = Vector2.Zero;
float radius = 0.0f;
BodyType BT = BodyType.Static;
Microsoft.Xna.Framework.Color color = Microsoft.Xna.Framework.Color.Black;
while (reader.Read() && !(reader.Name == "Circles" && !reader.IsStartElement())) // It read all the "Circle"
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "PositionInitial":
if (reader.Read())
pos = ParserXML.parseXmlToVector2(reader.Value.Trim());
//reader.Read();
break;
case "radius":
if (reader.Read())
radius = ParserXML.parseXmlToFloat(reader.Value.Trim());
//reader.Read();
break;
case "BodyType":
if (reader.Read())
BT = ParserXML.parseXmlToBodyType(reader.Value.Trim());
//reader.Read();
break;
case "Color":
if (reader.Read())
color = ParserXML.parseXmlToColor(reader.Value.Trim());
//reader.Read();
break;
}
}
else if (!reader.IsStartElement() && reader.Name == "Circle")
{
EnvironmentVariable.lCercle.Add(new ObjCercle(pos, radius, 0.0f, EnvironmentVariable.worldPhysic, BT));
EnvironmentVariable.lCercle.Last().Color = color;
EnvironmentVariable.lCercle.Last().addDisplayer();
}
}
break;
case "Rectangles":
Vector2 posR = Vector2.Zero;
Vector2 sizeR = Vector2.Zero;
float angle = 0.0f;
BodyType BTR = BodyType.Static;
Microsoft.Xna.Framework.Color colorR = Microsoft.Xna.Framework.Color.Black;
TextureImg tex = TextureImg.Null;
while (reader.Read() && !(reader.Name == "Rectangles" && !reader.IsStartElement())) // It read all the "Circle"
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "PositionInitial":
if (reader.Read())
posR = ParserXML.parseXmlToVector2(reader.Value.Trim());
//reader.Read();
break;
case "Size":
if (reader.Read())
sizeR = ParserXML.parseXmlToVector2(reader.Value.Trim());
//reader.Read();
break;
case "Angle":
if (reader.Read())
angle = ParserXML.parseXmlToFloat(reader.Value.Trim());
//reader.Read();
break;
case "BodyType":
if (reader.Read())
BTR = ParserXML.parseXmlToBodyType(reader.Value.Trim());
//reader.Read();
break;
case "Color":
if (reader.Read())
colorR = ParserXML.parseXmlToColor(reader.Value.Trim());
//reader.Read();
break;
case "Texture":
if (reader.Read())
tex = ParserXML.parseXmlToTexture(reader.Value.Trim());
//reader.Read();
break;
}
}
else if (!reader.IsStartElement() && reader.Name == "Rectangle")
{
Texture2D img = null;
EnvironmentVariable.lRectangle.Add(new ObjRectange(posR + sizeR / 2,
sizeR.X,
sizeR.Y,
0.0f,
BTR));
EnvironmentVariable.lRectangle.Last().Angle = angle;
EnvironmentVariable.lRectangle.Last().Color = colorR;
switch (tex)
{
case TextureImg.tree1:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree1");
break;
case TextureImg.tree2:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree2");
break;
case TextureImg.tree3:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree3");
break;
case TextureImg.tree4:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree4");
break;
case TextureImg.tree5:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree5");
break;
case TextureImg.tree6:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree6");
break;
case TextureImg.tree7:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree7");
break;
case TextureImg.tree8:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree8");
break;
case TextureImg.tree9:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree9");
break;
case TextureImg.tree10:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree10");
break;
case TextureImg.grass:
img = EnvironmentVariable.content.Load<Texture2D>("textures/grass");
break;
case TextureImg.Idle:
img = EnvironmentVariable.content.Load<Texture2D>("textures/Idle");
break;
case TextureImg.Run:
img = EnvironmentVariable.content.Load<Texture2D>("textures/Run");
break;
}
EnvironmentVariable.lRectangle.Last().Texture = tex;
EnvironmentVariable.lRectangle.Last().addTexture(img, 0.0f);
EnvironmentVariable.lRectangle.Last().addDisplayer();
}
}
break;
case "Polygons":
Vector2 posP = Vector2.Zero;
BodyType BTP = BodyType.Static;
Microsoft.Xna.Framework.Color colorP = Microsoft.Xna.Framework.Color.Black;
List<Vector2> vert = new List<Vector2>();
while (reader.Read() && !(reader.Name == "Polygons" && !reader.IsStartElement())) // It read all the "Circle"
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "PositionInitial":
if (reader.Read())
posP = ParserXML.parseXmlToVector2(reader.Value.Trim());
//reader.Read();
break;
case "BodyType":
if (reader.Read())
BTP = ParserXML.parseXmlToBodyType(reader.Value.Trim());
//reader.Read();
break;
case "Color":
if (reader.Read())
colorP = ParserXML.parseXmlToColor(reader.Value.Trim());
//reader.Read();
break;
case "Vertices":
while (reader.Read() && reader.Name != "Vertices")
{
if (reader.Read())
vert.Add(ParserXML.parseXmlToVector2(reader.Value.Trim()) + posP);
reader.Read();
}
break;
}
}
else if (!reader.IsStartElement() && reader.Name == "Polygon")
{
List<Vector2> vertTp = new List<Vector2>(vert);
EnvironmentVariable.lPolygon.Add(new ObjPolygon(vertTp, BTP, EnvironmentVariable.worldPhysic));
EnvironmentVariable.lPolygon.Last().addDisplayer();
EnvironmentVariable.lPolygon.Last().Color = colorP;
vert.Clear();
}
}
break;
}
}
}
}
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(EnvironmentVariable.graphics.GraphicsDevice);
EnvironmentVariable.width = GraphicsDevice.DisplayMode.Width;
EnvironmentVariable.height= GraphicsDevice.DisplayMode.Height;
//CameraController.Inialized(new Rectangle(EnvironmentVariable.width - 10, EnvironmentVariable.height / 4, 20, EnvironmentVariable.height / 2), EnvironmentVariable.graphics.GraphicsDevice);
_clampTextureAddressMode = new SamplerState
{
AddressU = TextureAddressMode.Clamp,
AddressV = TextureAddressMode.Clamp
};
level.LoadContent(EnvironmentVariable.graphics.GraphicsDevice, Content);
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
level.Update(gameTime);
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
EnvironmentVariable.graphics.GraphicsDevice.SamplerStates[0] = _clampTextureAddressMode;
level.Draw(gameTime, spriteBatch);
base.Draw(gameTime);
}
}
}