/
Game1.cs
309 lines (229 loc) · 8.11 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
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
#endregion
namespace TankWar
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
#region Fields
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//set screen
int winWidth=GameConstants.WIN_WIDTH;
int winHeight=GameConstants.WIN_HEIGHT;
bool isFullScreen=false;
//Game Objects
Tank player1;
//tank enemies
AI ai;
//All tanks with ai
List<AI> tanks = new List<AI> ();
List<MediumTank> mediumTanks = new List<MediumTank> ();
List<ArmoredCar> armoredCars=new List<ArmoredCar>();
//Game back ground
Texture2D backSprite;
public static Map map1;
//fonts
SpriteFont font;
int score;
string scoresString;
//explosions
Texture2D spriteExp;
List<Explosion> explosions=new List<Explosion>();
#endregion
// #region Properties
// public Map Map1{get{return map1;} }
//
// #endregion
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = isFullScreen;
graphics.PreferredBackBufferWidth=winWidth ;
graphics.PreferredBackBufferHeight=winHeight;
}
/// <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
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.
spriteBatch = new SpriteBatch(GraphicsDevice);
//load back
backSprite = Content.Load<Texture2D> ("back");
//font load
font = Content.Load<SpriteFont> ("Arial");
score = 0;
scoresString = "Scores: ";
//load map
map1 = new Map (Content);
//Creat new objects;
player1 = new Tank (Content, "Tanks4", GameConstants.PLAYER1_X, winHeight,GameConstants.PLAYER1,map1);
//create explosion
spriteExp = Content.Load<Texture2D> ("explosion");
//tanks.Add (new AI(Content,"MediumTanks4",GameConstants.LEFT_BOARDER,0,map1));
mediumTanks.Add(new MediumTank(Content,"MediumTanks4",GameConstants.LEFT_BOARDER,0,map1));
mediumTanks.Add(new MediumTank(Content,"MediumTanks4",GameConstants.CENTER_V,0,map1));
armoredCars.Add(new ArmoredCar(Content,"MediumTanks4",GameConstants.RIGHT_BOARDER,0,map1));
//add all in tanks
tanks.AddRange (mediumTanks);
tanks.AddRange (armoredCars);
}
/// <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)
{
// For Mobile devices, this logic will close the Game when the Back button is pressed
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
Exit();
}
// Use Keyboard
KeyboardState keyboard = Keyboard.GetState ();
player1.Update (gameTime, keyboard);
//player1 shoot in map
CheckMapProjectiles (player1);
//tankAIs Update (gameTime, keyboard);
for (int i=tanks.Count-1; i>=0; i--) {
// ai tanks shoot in map
CheckMapProjectiles (tanks [i]);
//Checnk the collisiond between ai and player's projectiles Jul 11
foreach (Projectile projectile in player1.Projectiles) {
if (tanks[i].Active&&tanks [i].CollisionRectangle.Intersects (projectile.CollisionRectangle)) {
tanks [i].Active = false;
//(check the ai tank type)
score += 1;
projectile.Active = false;
Explode (gameTime, tanks [i].CollisionRectangle.X, tanks [i].CollisionRectangle.Y);
}
}
//check the projectiles from ais
foreach (Projectile projectile in tanks[i].Projectiles) {
//deactive the projectiles from player
if (tanks [i].Active) {
foreach (Projectile playerProjectile in player1.Projectiles) {
if (projectile.CollisionRectangle.Intersects (playerProjectile.CollisionRectangle)) {
projectile.Active = false;
playerProjectile.Active = false;
}
}
}
//deactive the projectiles from ai
for (int j=i+1; j<tanks.Count; j++) {
foreach (Projectile nextProjectile in tanks[j].Projectiles) {
if (projectile.CollisionRectangle.Intersects(nextProjectile.CollisionRectangle)){
//projectiles in tanks will removed when it was deactive
projectile.Active=false;
nextProjectile.Active=false;
}
}
}
if (player1.Active&&player1.CollisionRectangle.Intersects(projectile.CollisionRectangle)){
projectile.Active = false;
Explode (gameTime, player1.CollisionRectangle.X, player1.CollisionRectangle.Y);
player1.Active=false;
}
}
//update tanks
//to avoid projectiles
tanks [i].Update (gameTime);
}
//remove the dead tanks
for (int i=tanks.Count-1; i>=0; i--) {
if (!tanks [i].Active) {
tanks [i].Update (gameTime);
tanks.RemoveAt (i);
}
}
//update explosions and remove the finished one
for (int i= explosions.Count-1; i>=0; i--) {
if (explosions [i].Finished) {
explosions.RemoveAt (i);
} else {
explosions[i].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)
{
graphics.GraphicsDevice.Clear(Color.DarkGray);
//TODO: Add your drawing code here
spriteBatch.Begin ();
//draw background &map
spriteBatch.Draw (backSprite, new Vector2 (0, 0),Color.White);
spriteBatch.DrawString (font,scoresString+ score, new Vector2 (560, 40), Color.Red);
map1.Draw (spriteBatch);
player1.Draw (spriteBatch);
//draw tank enemies
foreach (Tank tank in tanks) {
tank.Draw (spriteBatch);
}
//draw explosions
foreach (Explosion explosion in explosions) {
explosion.Draw (spriteBatch);
}
spriteBatch.End ();
base.Draw(gameTime);
}
public void Explode(GameTime gameTime,int x, int y){
explosions.Add(new Explosion(spriteExp, x,y));
}
/// <summary>
/// Checks the map projectiles. Remove the cubes in map which was shooted
/// </summary>
/// <param name="tank">Tank.</param>
public void CheckMapProjectiles( Tank tank){
//this nested loop maybe decrease the efficiency
for (int i=map1.Cubes.Count-1; i>=0; i--) {
for (int j=tank.Projectiles.Count-1; j>=0; j--) {
if (map1.Cubes [i].CollisionRectangle.Intersects (tank.Projectiles[j].CollisionRectangle)) {
//wall cubes gone
if (map1.Cubes [i].GetType () == typeof(WallCube)) {
map1.Cubes.RemoveAt (i);
tank.Projectiles [j].Active = false; //when projectile do not active tank update will remove it automatically
}
//iron still there
if (map1.Cubes [i].GetType () == typeof(IronCube)) {
tank.Projectiles [j].Active = false;
}
//check whether the home is conquered
if (map1.Cubes [i].GetType () == typeof(EagleCube)) {
map1.Cubes.RemoveAt (i);
// add a ruin area
tank.Projectiles [j].Active = false;
}
}
}
}
}
}
}