-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
371 lines (286 loc) · 13.3 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
369
370
371
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;
namespace ZombieGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont spriteFont;
Texture2D playerTexture;
Texture2D mouseTexture;
Texture2D bulletTexture;
Texture2D enemyTexture;
Player player;
Enemy test;
List<Enemy> enemyList;
List<Enemy> enemyDeleteList;
List<Bullet> bulletDeleteList;
List<Rectangle> collisionList;
//TILE STUFF
Texture2D tileDirt;
Texture2D tileGrass;
Texture2D tileMud;
Texture2D tileGround;
Texture2D tileHand;
List<Texture2D> tileList = new List<Texture2D>();
List<Texture2D> tileList2 = new List<Texture2D>();
Vector2 previousPlayerPos = Vector2.Zero;
int[,] tileMap = new int[,]
{
{3,3,3,3,3,3,2,2,2,2,3,3,3,3,3,3,2,2,2,3,3,3,1,1,3,2},
{3,3,3,3,3,3,2,2,2,2,3,3,3,3,3,3,2,2,2,3,3,3,1,1,3,2},
{3,3,3,3,3,3,2,2,2,2,3,3,3,3,3,3,2,2,2,3,3,3,1,1,3,2},
{3,3,3,3,3,3,2,2,2,2,3,3,3,3,3,3,2,2,2,3,3,3,1,1,3,2},
{3,3,3,3,3,3,2,2,2,2,3,3,3,3,3,3,2,2,2,3,3,3,1,1,3,2},
{3,3,3,3,3,3,2,2,2,2,3,3,3,3,3,3,2,2,2,3,3,3,1,1,3,2},
{3,3,3,3,3,3,2,2,2,2,3,3,3,3,3,3,2,2,2,3,3,3,1,1,3,2},
{3,3,3,3,3,3,2,2,2,2,3,3,3,3,3,3,2,2,2,3,3,3,1,1,3,2},
{3,3,3,3,3,3,2,2,2,2,3,3,3,3,3,3,2,2,2,3,3,3,1,1,3,2},
{3,3,3,3,3,3,2,2,2,2,3,3,3,3,3,3,2,2,2,3,3,3,1,1,3,2},
{3,3,3,3,3,3,2,2,2,2,3,3,3,3,3,3,2,2,2,3,3,3,1,1,3,2},
};
int[,] tileMap2 = new int[,]
{
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-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,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
int tileWidth = 64;
int tileHeight = 64;
Vector2 cameraPosition = Vector2.Zero;
float cameraSpeed = 5.0f;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "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
collisionList = new List<Rectangle>();
int tileMapWidth2 = tileMap2.GetLength(1);
int tileMapHeight2 = tileMap2.GetLength(0);
for (int x = 0; x < tileMapWidth2; x++)
{
for (int y = 0; y < tileMapHeight2; y++)
{
int textIndex = tileMap2[y, x];
if (textIndex == -1)
{
continue;
}
Rectangle r = new Rectangle(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
collisionList.Add(r);
}
}
base.Initialize();
}
/// <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.
spriteFont = Content.Load<SpriteFont>("SpriteFont1");
enemyDeleteList = new List<Enemy>();
bulletDeleteList = new List<Bullet>();
playerTexture = Content.Load<Texture2D>("manwalk");
mouseTexture = Content.Load<Texture2D>("cursor");
bulletTexture = Content.Load<Texture2D>("bullet2");
enemyTexture = Content.Load<Texture2D>("ZombieGreen");
enemyList = new List<Enemy>();
enemyList.Add(new Enemy(enemyTexture, new Vector2(500, 500)));
//enemyList.Add(new Enemy(enemyTexture, new Vector2(500, 10)));
//enemyList.Add(new Enemy(enemyTexture, new Vector2(200, 200)));
//enemyList.Add(new Enemy(enemyTexture, new Vector2(100, 500)));
//enemyList.Add(new Enemy(enemyTexture, new Vector2(200, 10)));
//enemyList.Add(new Enemy(enemyTexture, new Vector2(300, 200)));
//enemyList.Add(new Enemy(enemyTexture, new Vector2(400, 500)));
//enemyList.Add(new Enemy(enemyTexture, new Vector2(500, 10)));
//enemyList.Add(new Enemy(enemyTexture, new Vector2(600, 200)));
spriteBatch = new SpriteBatch(GraphicsDevice);
// TILE STUFF
tileDirt = Content.Load<Texture2D>("Tiles/se_free_dirt_texture");
tileGrass = Content.Load<Texture2D>("Tiles/se_free_grass_texture");
tileGround = Content.Load<Texture2D>("Tiles/se_free_ground_texture");
tileMud = Content.Load<Texture2D>("Tiles/se_free_mud_texture");
tileList.Add(tileDirt);
tileList.Add(tileMud);
tileList.Add(tileGround);
tileList.Add(tileGrass);
tileHand = Content.Load<Texture2D>("crate");
tileList2.Add(tileHand);
player = new Player(playerTexture, mouseTexture, new Vector2(200, 200), bulletTexture, new AnimatedSprite(playerTexture, 1, 2, 0.5f));
}
/// <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 == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
Vector2 motion = Vector2.Zero;
KeyboardState kbs = Keyboard.GetState();
if (kbs.IsKeyDown(Keys.W))
{
motion.Y --;
}
if (kbs.IsKeyDown(Keys.S))
{
motion.Y ++;
}
if (kbs.IsKeyDown(Keys.D))
{
motion.X ++;
}
if (kbs.IsKeyDown(Keys.A))
{
motion.X --;
}
if (motion != Vector2.Zero) motion.Normalize();
cameraPosition += motion * cameraSpeed;
if (cameraPosition.Y < 0) cameraPosition.Y = 0;
if (cameraPosition.X < 0) cameraPosition.X = 0;
int screenW = GraphicsDevice.Viewport.Width;
int screenH = GraphicsDevice.Viewport.Height;
int tilemapWidth = tileMap.GetLength(1) * tileWidth;
int tilemapHeight = tileMap.GetLength(0) * tileHeight;
if (cameraPosition.Y > tilemapHeight - screenH) cameraPosition.Y = tilemapHeight - screenH;
if (cameraPosition.X > tilemapWidth - screenW) cameraPosition.X = tilemapWidth - screenW;
// COLLIDE WITH CRATES
previousPlayerPos = player._playerPos;
player.Update(gameTime);
foreach (Rectangle r in collisionList)
{
Rectangle r1 = new Rectangle(r.X - (int)cameraPosition.X, r.Y - (int)cameraPosition.Y, r.Width, r.Height);
if (r1.Intersects(player._playerRec))
{
player._playerPos = previousPlayerPos;
break;
}
}
// -------------------
foreach (Enemy e in enemyList)
{
e.Update(new Rectangle((int)player._playerPos.X, (int)player._playerPos.Y, player._playerTexture.Width/2, player._playerTexture.Height/2), gameTime);
if (e.returnIntersect(new Rectangle((int)player._playerPos.X, (int)player._playerPos.Y, player._playerTexture.Width/2, player._playerTexture.Height/2))){
if (e.canDamage) player._health -=5;
}
foreach (Bullet b in player.bulletList)
{
if (b.intersectsEnemy(e))
{
enemyDeleteList.Add(e);
bulletDeleteList.Add(b);
}
}
}
foreach (Bullet b in bulletDeleteList)
{
player.bulletList.Remove(b);
}
foreach (Enemy e in enemyDeleteList)
{
enemyList.Remove(e);
}
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);
spriteBatch.Begin();
int tileMapWidth = tileMap.GetLength(1);
int tileMapHeight = tileMap.GetLength(0);
for (int x = 0; x < tileMapWidth; x++)
{
for (int y = 0; y < tileMapHeight; y++)
{
int textIndex = tileMap[y, x];
Texture2D text = tileList[textIndex];
spriteBatch.Draw(text, new Rectangle(x * tileWidth - (int)cameraPosition.X, y * tileHeight - (int)cameraPosition.Y, tileWidth, tileHeight), Color.White);
}
}
int tileMapWidth2 = tileMap2.GetLength(1);
int tileMapHeight2 = tileMap2.GetLength(0);
for (int x = 0; x < tileMapWidth2; x++)
{
for (int y = 0; y < tileMapHeight2; y++)
{
int textIndex = tileMap2[y, x];
if (textIndex == -1)
{
continue;
}
Texture2D text = tileList2[textIndex];
spriteBatch.Draw(text, new Rectangle(x * tileWidth - (int)cameraPosition.X, y * tileHeight - (int)cameraPosition.Y, tileWidth, tileHeight), Color.White);
}
}
spriteBatch.DrawString(spriteFont, "Player Health : " + player._health.ToString(), new Vector2(10, 10), Color.White);
spriteBatch.DrawString(spriteFont, "X : " + player._playerPos.X.ToString() + " Y : " + player._playerPos.Y.ToString(), new Vector2(10, 50), Color.White);
spriteBatch.End();
foreach (Enemy e in enemyList)
{
e.Draw(spriteBatch);
}
player.Draw(spriteBatch);
base.Draw(gameTime);
}
}
}