-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame1.cs
376 lines (308 loc) · 15.8 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
372
373
374
375
376
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
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;
/*
* Platformer Project
* By: Josh Frey
* Derek Bitterman
* Derek Ness
* */
namespace PlatformerProject
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Player player;
float playerMoveSpeed;
//This enumeration is used to keep track of the state
//it will restrict updating to the proper components of the game
//this would allow the game to appear frozen if in menu
public enum State
{
TitleState,
PlayState,
MenuState,
GOState
};
State currentState; //this is the variable that keeps track of the state
//This is the input handler for our program
KeyboardState currentKeyboardState;
KeyboardState oldKeyboardState;
GamePadState currentGamePadState;
GamePadState oldGamePadState;
//Title Screen Texture
Texture2D titleScreen, goScreen;
SevenEngine physics;
//These are 2D textures that are drawn onto the screen
ArrayList platforms;
Texture2D mainbackground; //The main backgorund
Texture2D nocol; //Testing non-collision tree
Platform platform, platform2;
Platform enemy;
//This is the menu screen used to choose options
Texture2D menuScreen;
NoCol mousePointer; //The mouse pointer is a NoCol object to make it easier to move
int mouseCounter = 0; //This is currently used to know which option in the menu is selected
//The variables necessary for jumping
float oldH ;
bool jumping = false;
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()
{
//Initialize the player and his movement speed
player = new Player();
playerMoveSpeed = 3.0f;
//Initialize the current state to title screen
currentState = State.TitleState;
physics = new SevenEngine();
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);
platforms = new ArrayList() ;
//Load Title Screen
titleScreen = Content.Load<Texture2D>("Titlescreen");
goScreen = Content.Load<Texture2D>("Gameover");
//Load the player and his animations
Animation playerAnimation = new Animation();
Texture2D playerTexture = Content.Load<Texture2D>("kidright");
playerAnimation.Initialize(playerTexture, Vector2.Zero, 33, 64, 4, 80, Color.White, 1f, false);
Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X + 20f, GraphicsDevice.Viewport.TitleSafeArea.Y
+ GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
player.Initialize(playerAnimation, playerPosition);
//all of these are our test images to be used for now
//later we will replace with the actual game textures
enemy = new Platform(Content.Load<Texture2D>("enemy"), new Vector2(600f, 365f), spriteBatch, .5f);
mainbackground = Content.Load<Texture2D>("quickSky");
nocol = Content.Load<Texture2D>("treeSmall");
menuScreen = Content.Load<Texture2D>("quickMenu");
platform = new Platform(Content.Load<Texture2D>("platform"), new Vector2(0f, 400f), spriteBatch, 5f);
platform2 = new Platform(Content.Load<Texture2D>("platform2"), new Vector2(500f, 400f), spriteBatch, 2f);
platforms.Add(platform);
platforms.Add(platform2);
//platforms.Add(enemy);
mousePointer = new NoCol(Content.Load<Texture2D>("menuArrow"), new Vector2(280f, 260f), spriteBatch);
}
/// <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
UpdatePlayer(gameTime);
base.Update(gameTime);
}
private void UpdateCollision()
{
}
/// <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);
//Starts the sprite batch
//Will be drwan in order, so things on bottom layer should be called first
spriteBatch.Begin();
//********************************
//*** FIRST LAYER OF THE SCREEN **
//********************************
spriteBatch.Draw(mainbackground, Vector2.Zero, Color.White); //The mainbackground, should always be up
//Will draw the title only if the game is currently in the TitleState
//This will actually lay on top of the main background
if (currentState == State.TitleState)
{
player.Position = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X + 20f, GraphicsDevice.Viewport.TitleSafeArea.Y
+ GraphicsDevice.Viewport.TitleSafeArea.Height / 2) ;
spriteBatch.Draw(titleScreen, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, .75f, SpriteEffects.None, 0f); //draw the titlescreen
}
//********************************
//** SECOND LAYER OF THE SCREEN **
//********************************
//This layer will only show if the current state is in play or menu state
if (currentState == State.PlayState || currentState == State.MenuState)
{
platform.drawNoCol(); //draw the platform object
platform2.drawNoCol();
enemy.drawNoCol();
player.Draw(spriteBatch); //draw the player onto the screen
//This will be drawn on top of the player and all other current object if the current state is menu
if (currentState == State.MenuState)
{
spriteBatch.Draw(menuScreen, new Vector2(200, 200), null, Color.White, 0f, Vector2.Zero, .5f, SpriteEffects.None, 0f); //draw the menu
mousePointer.drawNoCol(); //draw the mouse pointer on top of the menu
}
}
if (currentState == State.GOState)
{
spriteBatch.Draw(goScreen, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, .75f, SpriteEffects.None, 0f); //draw the titlescreen
}
//end the sprite batch and ends current Update's Drawing process
spriteBatch.End();
base.Draw(gameTime);
}
private void UpdatePlayer(GameTime gameTime)
{
player.Update(gameTime);
currentKeyboardState = Keyboard.GetState();
currentGamePadState = GamePad.GetState(PlayerIndex.One);
//*********************************************************************************************************************************************
//************************************************* CONTROL FOR PLAYSTATE *********************************************************************
//*********************************************************************************************************************************************
if (currentState == State.PlayState)
{
if ((physics.isOnTopOf(player, platforms) && !jumping))
{
if (currentKeyboardState.IsKeyDown(Keys.D))
{
player.PlayerAnimation.Looping = true;
player.Position.X += playerMoveSpeed;
}
else if (currentKeyboardState.IsKeyDown(Keys.A))
{
player.PlayerAnimation.Looping = true;
player.Position.X -= playerMoveSpeed;
}
if (currentKeyboardState.IsKeyDown(Keys.Space))
{
oldH = player.Position.Y;
jumping = true;
}
//if D is released stop moving
if (oldKeyboardState.IsKeyDown(Keys.D) && currentKeyboardState.IsKeyUp(Keys.D))
{
player.PlayerAnimation.Looping = false;
} if (oldKeyboardState.IsKeyDown(Keys.A) && currentKeyboardState.IsKeyUp(Keys.A))
{
player.PlayerAnimation.Looping = false;
}
//if m is pressed stop moving and switch to MenuState
if ((oldKeyboardState.IsKeyDown(Keys.M) && currentKeyboardState.IsKeyUp(Keys.M)) || currentGamePadState.Buttons.Y == ButtonState.Pressed)
{
player.PlayerAnimation.Looping = false;
currentState = State.MenuState;
}
}
else if (jumping)
{
player.Position.Y -= playerMoveSpeed;
if (player.Position.Y <= (oldH - 50))
jumping = false;
if (currentKeyboardState.IsKeyDown(Keys.D))
{
player.PlayerAnimation.Looping = true;
player.Position.X += playerMoveSpeed;
}
else if (currentKeyboardState.IsKeyDown(Keys.A))
{
player.PlayerAnimation.Looping = true;
player.Position.X -= playerMoveSpeed;
}
}
else
{
player.PlayerAnimation.Looping = false;
if (player.Position.Y == graphics.GraphicsDevice.Viewport.Height)
currentState = State.GOState;
if (currentKeyboardState.IsKeyDown(Keys.D) && player.Position.Y != graphics.GraphicsDevice.Viewport.Height)
{
player.PlayerAnimation.Looping = true;
player.Position.X += playerMoveSpeed;
}
else if (currentKeyboardState.IsKeyDown(Keys.A) && player.Position.Y != graphics.GraphicsDevice.Viewport.Height)
{
player.PlayerAnimation.Looping = true;
player.Position.X -= playerMoveSpeed;
}
player.Position.Y += playerMoveSpeed;
}
}
//*********************************************************************************************************************************************
//************************************************* CONTROL FOR MENUSTATE *********************************************************************
//*********************************************************************************************************************************************
else if (currentState == State.MenuState)
{
//if c is pressed go back to the PlayState
if ((oldKeyboardState.IsKeyDown(Keys.M) && currentKeyboardState.IsKeyUp(Keys.M)) || currentGamePadState.Buttons.B == ButtonState.Pressed)
{
currentState = State.PlayState;
}
//if d is pressed move menu arrow left, but only if it is currently on continue option
if (((oldKeyboardState.IsKeyDown(Keys.D) && currentKeyboardState.IsKeyUp(Keys.D)) || currentGamePadState.DPad.Right == ButtonState.Pressed) && mouseCounter == 0)
{
mousePointer.incrementPosition(100f, 0f);
mouseCounter = 1;
}
//if a is pressed move menu arrow right, but only if it is currently on exit option
if (((oldKeyboardState.IsKeyDown(Keys.A) && currentKeyboardState.IsKeyUp(Keys.A)) || currentGamePadState.DPad.Left == ButtonState.Pressed) && mouseCounter == 1)
{
mousePointer.incrementPosition(-100f, 0f);
mouseCounter = 0;
}
}
//*********************************************************************************************************************************************
//************************************************* CONTROL FOR TITLESTATE *********************************************************************
//*********************************************************************************************************************************************
else if (currentState == State.TitleState)
{
if (oldKeyboardState.IsKeyDown(Keys.Enter) && currentKeyboardState.IsKeyUp(Keys.Enter))
{
currentState = State.PlayState;
}
}
else if (currentState == State.GOState)
{
if (oldKeyboardState.IsKeyDown(Keys.Enter) && currentKeyboardState.IsKeyUp(Keys.Enter))
{
currentState = State.TitleState;
}
}
//*********************************************************************************************************************************************
//************************************************* END OF CONTROL SECTION ********************************************************************
//*********************************************************************************************************************************************
player.Position.X = MathHelper.Clamp(player.Position.X, 0, GraphicsDevice.Viewport.Width - player.Width);
player.Position.Y = MathHelper.Clamp(player.Position.Y, 0, GraphicsDevice.Viewport.Height);
oldKeyboardState = currentKeyboardState;
oldGamePadState = currentGamePadState;
}
}
}