/// <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 KeyboardState currentKeyState = Keyboard.GetState(); GamePadState currentPadState = GamePad.GetState(PlayerIndex.One); switch (currentState) { case ScreenState.Level: { waitForKeyPress(); break; } case ScreenState.Start: { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit(); waitForKeyPress(); break; } case ScreenState.Pause: { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit(); waitForKeyPress(); break; } case ScreenState.End: { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit(); if ((GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed || currentKeyState.IsKeyDown(Keys.P)) && (lastKeyState.IsKeyUp(Keys.P) && lastpadState.IsButtonUp(Buttons.Start))) { currentState = ScreenState.Start; level = 1; plyr = new Player(new Vector3(0, 0, 50)); plyr.LoadContent(); } lastKeyState = currentKeyState; lastpadState = currentPadState; break; } case ScreenState.Play: { if ((GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed || currentKeyState.IsKeyDown(Keys.P)) && (lastKeyState.IsKeyUp(Keys.P) && lastpadState.IsButtonUp(Buttons.Start))) { currentState = ScreenState.Pause; } lastKeyState = currentKeyState; lastpadState = currentPadState; if (!plyr.isAlive()) { currentState = ScreenState.End; } if (enemy.isAlive()) { enemy.Update(gameTime); } else { if (level == 1) { v = v + plyr.health; } level++; plyr.health += 2; plyr.Score(v); enemy = new Enemy(new Vector3(0, -3f, -30)); enemy.LoadContent(); if (level == 2) { currentState = ScreenState.Level; v = 2; if (plyr.health > 0 && plyr.health <= 6) { v = plyr.health + v; } enemy.health ++; } if (level == 3) { currentState = ScreenState.Level; v = 4; if (plyr.health > 0 && plyr.health <=6) { v = plyr.health + v; } enemy.health ++; } if (level > 3) { currentState = ScreenState.End; } } // TODO: Add your update logic here radar.Update(gameTime, plyr.Look()); plyr.Update(gameTime); for (int i = 0; i < _bullets.Count; i++) { if (_bullets[i].isAlive()) { _bullets[i].Update(gameTime); } else { _bullets.RemoveAt(i); } } foreach (Obstacle obstacle in _obstacles) { obstacle.Update(gameTime); } break; } } base.Update(gameTime); }
/// <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); TitleFont = Content.Load<SpriteFont>("TitleFont"); MenuFont = Content.Load<SpriteFont>("ButtonFont"); player = new Player(); player.X = 0; player.Y = 0; player.Z = 1; player.Texture = Content.Load<Texture2D>("Character\\Character Boy"); inputDelayMax = new TimeSpan(0, 0, 0, 0, 100); inputDelayCurrent = new TimeSpan(); DataManager = new thegame.DataManager(); MapParser.Parse("maps\\demo.txt", DataManager, Content); grid = DataManager.getGrid("start"); }
/// <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 _bullets = new List<Bullet>(); _obstacles = new List<Obstacle>(); enemy = new Enemy(new Vector3(0, -3f, -30)); plyr = new Player(new Vector3(0, 0, 120)); radar = new Radar(); //obstacles spawn in random locations Random rand = new Random(); #if (!DEBUG_PATHS || !DEBUG ) for (int num = 0; num < 50; num++) { int x = rand.Next(-300, 500); int z = rand.Next(-300, 500); //draw obstacle if it does not spawn on an enemy or the player Obstacle tempObstacle = (new Obstacle(new Vector3(x, 0, z))); tempObstacle.LoadContent(); if (!tempObstacle.collidesWith(enemy.getBoundingSphere(), enemy.getWorld()) && !tempObstacle.collidesWith(plyr.getBoundingSphere(), plyr.getWorld())) { _obstacles.Add(tempObstacle); } } #else Obstacle tmp = new Obstacle(new Vector3(7,0,-65)); _obstacles.Add(tmp); #endif #if DEBUG currentState = ScreenState.Play; #else currentState = ScreenState.Start; #endif lastKeyState = Keyboard.GetState(); lastpadState = GamePad.GetState(PlayerIndex.One); base.Initialize(); }