/// <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() { gameState = GameState.Menu; kbState = Keyboard.GetState(); IsMouseVisible = true; quitActive = false; reader = new MapReader(); quadTree = new QuadTreeNode(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); //Initializes player and their texture playerXState = PlayerXState.StandRight; playerYState = PlayerYState.Ground; screen = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); rand = new Random(); MapReader.rando = rand; base.Initialize(); }
/// <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) { // if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) // Exit(); previousKBState = kbState; kbState = Keyboard.GetState(); previousMState = mState; mState = Mouse.GetState(); marioQuad = quadTree.GetContainingQuad(player.O_Position); if (!quitActive) { switch (gameState) { case GameState.Menu: if (!quitActive) { mainMenu.Update(mState, previousMState, kbState, previousKBState); } break; case GameState.World: if (reader.RoomNumber >= 31) { WinGame(); } if (SingleKeyPress(Keys.P)) //Swtch to pause menu { PauseGame(); } player.Update(gameTime.ElapsedGameTime.TotalSeconds, kbState, previousKBState); for (int i = 0; i < reader.EnemyList.Count; i++) { if (player.O_Position.Intersects(reader.EnemyList[i].O_Position)) { Battle(reader.EnemyList[i]); killedEnemy = reader.EnemyList[i]; } } //Put in player collision with enemy here //Player Movement if (kbState.IsKeyDown(Keys.Left)) { player.O_X -= 5; } else if (kbState.IsKeyDown(Keys.Right)) { player.O_X += 5; } if (kbState.IsKeyDown(Keys.Up)) { player.O_Y -= 5; } else if (kbState.IsKeyDown(Keys.Down)) { player.O_Y += 5; } //Deals with players x directional movement switch (playerXState) { case PlayerXState.StandRight: if (kbState.IsKeyDown(Keys.D)) { playerXState = PlayerXState.WalkRight; player.Direction = CharDirection.right; player.State = CharacterState.o_walk; } else if (kbState.IsKeyDown(Keys.A)) { playerXState = PlayerXState.WalkLeft; player.Direction = CharDirection.left; player.State = CharacterState.o_walk; } break; case PlayerXState.WalkRight: player.Move(3); player.UpdateDetectors(); if (reader.Right && player.O_X >= GraphicsDevice.Viewport.Width - reader.CurrentRoom.Tile.Width - player.O_Width) { player.O_X = GraphicsDevice.Viewport.Width - reader.CurrentRoom.Tile.Width - player.O_Width; } for (int i = 0; i < reader.RectList.Count; i++) { if (player.Right.Intersects(reader.RectList[i])) { player.O_X = reader.RectList[i].X - player.O_Width - 1; break; } } if (kbState.IsKeyUp(Keys.D)) { playerXState = PlayerXState.StandRight; player.State = CharacterState.o_idle; player.XAcceleration = 1; } break; case PlayerXState.StandLeft: if (kbState.IsKeyDown(Keys.A)) { playerXState = PlayerXState.WalkLeft; player.Direction = CharDirection.left; player.State = CharacterState.o_walk; } else if (kbState.IsKeyDown(Keys.D)) { playerXState = PlayerXState.WalkRight; player.Direction = CharDirection.right; player.State = CharacterState.o_walk; } break; case PlayerXState.WalkLeft: player.Move(-3); player.UpdateDetectors(); //player.State = CharacterState.o_walk; if (reader.Left && player.O_X <= reader.CurrentRoom.Tile.Width) { player.O_X = reader.CurrentRoom.Tile.Width; } for (int i = 0; i < reader.RectList.Count; i++) { if (player.Left.Intersects(reader.RectList[i])) { player.O_X = reader.RectList[i].X + player.O_Width + 1; break; } } if (kbState.IsKeyUp(Keys.A)) { playerXState = PlayerXState.StandLeft; player.State = CharacterState.o_idle; player.XAcceleration = 1; } break; } //Deals with player's y directional movement switch (playerYState) { case PlayerYState.Ground: bool onPlatform = false; for (int i = 0; i < reader.RectList.Count; i++) { if (player.Below.Intersects(reader.RectList[i]) || player.O_LocY >= GraphicsDevice.Viewport.Height - player.O_Height - reader.CurrentRoom.Tile.Height) { onPlatform = true; break; } } for (int i = 0; i < reader.ItemList.Count; i++) { if (player.O_Position.Intersects(reader.ItemList[i].Rect)) { reader.ItemList[i].Collect(player); } } if (player.O_LocY >= GraphicsDevice.Viewport.Height - player.O_Height) { onPlatform = true; } if (!onPlatform) { player.JumpAcceleration = -2; playerYState = PlayerYState.Jump; player.State = CharacterState.o_jump; } if (kbState.IsKeyDown(Keys.Space)) { if (previousKBState.IsKeyUp(Keys.Space)) { playerYState = PlayerYState.Jump; player.State = CharacterState.o_jump; } } break; case PlayerYState.Jump: player.Jump(); player.UpdateDetectors(); for (int i = 0; i < reader.RectList.Count; i++) { if (player.Above.Intersects(reader.RectList[i])) { player.JumpAcceleration = -2; break; } else if (player.O_Position.Intersects(reader.RectList[i])) { player.O_Y = reader.RectList[i].Y - player.O_Height; player.JumpAcceleration = 17; playerYState = PlayerYState.Ground; if (kbState.IsKeyUp(Keys.A) && kbState.IsKeyUp(Keys.D)) { player.State = CharacterState.o_idle; } else { if (kbState.IsKeyUp(Keys.A) && kbState.IsKeyDown(Keys.D)) { player.State = CharacterState.o_walk; player.Move(5); } else if (kbState.IsKeyUp(Keys.D) && kbState.IsKeyDown(Keys.A)) { player.State = CharacterState.o_walk; player.Move(-5); } } break; } } if (reader.Up) { if (player.O_Y <= reader.CurrentRoom.Tile.Height) { player.JumpAcceleration = -2; } } if (reader.Down) { if (player.O_Y >= GraphicsDevice.Viewport.Height - player.O_Height - reader.CurrentRoom.Tile.Height) { player.O_Y = GraphicsDevice.Viewport.Height - player.O_Height - reader.CurrentRoom.Tile.Height; playerYState = PlayerYState.Ground; // IDLE HERE@@@@@@@@@@@@@@@@@@@ player.JumpAcceleration = 17; } } else if (player.O_Y >= GraphicsDevice.Viewport.Height - player.O_Height) { player.O_Y = GraphicsDevice.Viewport.Height - player.O_Height; playerYState = PlayerYState.Ground; // IDLE HERE@@@@@@@@@@@@@@@@@@@ player.JumpAcceleration = 17; } break; } break; case GameState.Pause: if (!quitActive) { pauseMenu.Update(mState, previousMState, kbState, previousKBState); if (SingleKeyPress(Keys.P)) { UnpauseGame(); } } break; case GameState.Battle: if (SingleKeyPress(Keys.P)) //Swtch to pause menu { PauseGame(); } BattleManager.Update(gameTime.ElapsedGameTime.TotalSeconds, mState, previousMState, kbState, previousKBState); break; case GameState.GameOver: gameOverMenu.Update(mState, previousMState, kbState, previousKBState); break; case GameState.Instructions: instructionsMenu.Update(mState, previousMState, kbState, previousKBState); break; case GameState.GameWon: gameWinMenu.Update(mState, previousMState, kbState, previousKBState); break; } } if (quitActive) { quitMenu.Update(mState, previousMState, kbState, previousKBState); } if (reader.SwitchRoom(player)) { reader.ReadMap("../../../Content/Rooms/room" + reader.RoomNumber + ".txt", quadTree, collectibleTexture, enemyTexture); if (reader.RoomNumber > 10 && reader.RoomNumber < 21) { int temp = rand.Next(2); if (temp == 0) { sewerBG.Tex = sewerTexture; } else { sewerBG.Tex = sewerTexture2; } } } base.Update(gameTime); }