/// <summary> /// Generates DestroyableWallSprite that is ready to be placed on board. /// </summary> /// <returns></returns> private Sprite GenerateDestroyableWall() { var position = GenerateNewPosition(); while (!IsPositionValid(position) && !IsPositionEmpty(position) && Math.Abs(position.X - Player.Position.X) < 3 && Math.Abs(position.Y - Player.Position.Y) < 3) { position = GenerateNewPosition(); } var wall = new DestroyableWallSprite(_destroyableWallTexture, position, this); return(wall); }
/// <summary> /// Takes user input and moves player accordingly. /// Picks up all PowerUps and handles bomb planting. /// Decreases hunger for player. /// </summary> /// <param name="gameTime"></param> public override void Update(GameTime gameTime) { if (Delay == 0) { var newPosition = new Point(Position.X + Direction.X, Position.Y + Direction.Y); newPosition = board.AdjustToBoardSize(newPosition); if (Life < 0 || Hunger < 0) { board.GameOver(); } if (board.Fields[newPosition.X, newPosition.Y].Sprite is PowerUp) { var powerup = board.Fields[newPosition.X, newPosition.Y].Sprite as PowerUp; if (powerup is MedpackPowerUp) { if (Life < MaxLife) { Life++; } } else if (powerup is FoodPowerUp) { Hunger = Math.Min(Hunger + 20, 150); } else if (powerup is DebrisPowerUp) { if (PowerUp is DebrisPowerUp) { var playerDebris = PowerUp as DebrisPowerUp; playerDebris.IncreaseDebris(); } else { var debrisPowerUp = powerup as DebrisPowerUp; debrisPowerUp.DebrisCount = 1; PowerUp = debrisPowerUp; } } else { PowerUp = powerup; } board.RemoveObject(powerup); } Keys[] key = Keyboard.GetState().GetPressedKeys(); if (key.Contains(Keys.X) && PowerUp is DebrisPowerUp) // plant wall { var debris = PowerUp as DebrisPowerUp; if (debris.DebrisCount > 1) { var wallPos = new Point(); if (key.Contains(Keys.Left)) { wallPos = new Point(Position.X - 1, Position.Y); if (board.IsPositionValid(wallPos)) { var destroyableWall = new DestroyableWallSprite(_destroyableWallTexture, wallPos, board); board.AddObject(destroyableWall); debris.DebrisCount -= 2; if (debris.DebrisCount == 0) { PowerUp = null; } } } if (key.Contains(Keys.Right)) { wallPos = new Point(Position.X + 1, Position.Y); if (board.IsPositionValid(wallPos)) { var destroyableWall = new DestroyableWallSprite(_destroyableWallTexture, wallPos, board); board.AddObject(destroyableWall); debris.DebrisCount -= 2; if (debris.DebrisCount == 0) { PowerUp = null; } } } if (key.Contains(Keys.Up)) { wallPos = new Point(Position.X, Position.Y - 1); if (board.IsPositionValid(wallPos)) { var destroyableWall = new DestroyableWallSprite(_destroyableWallTexture, wallPos, board); board.AddObject(destroyableWall); debris.DebrisCount -= 2; if (debris.DebrisCount == 0) { PowerUp = null; } } } if (key.Contains(Keys.Down)) { wallPos = new Point(Position.X, Position.Y + 1); if (board.IsPositionValid(wallPos)) { var destroyableWall = new DestroyableWallSprite(_destroyableWallTexture, wallPos, board); board.AddObject(destroyableWall); debris.DebrisCount -= 2; if (debris.DebrisCount == 0) { PowerUp = null; } } } } } if (board.IsPositionValid(newPosition)) { Point oldPosition = Position; Position = newPosition; board.ChangePosition(oldPosition, this); } if (key.Contains(Keys.Space)) { if (PowerUp is TntDetonatorPowerUp) { var tnt = PowerUp as TntDetonatorPowerUp; if (tnt.IsPlanted) { tnt.Detonate(); PowerUp = null; } else { tnt.Plant(Position); } } else { if (board.Bombs.Count(x => x.GetType() == typeof(Bomb)) < 2) { var bomb = new Bomb(BombTexture, Position, board); board.Plant(bomb); } } } if (HungerDelay >= MaxHungerDelay) { HungerDelay = 0; Hunger--; } else { HungerDelay++; } } if (PowerUp != null) { if (PowerUp is ChargingPowerUp) { PowerUp.Life -= (int)gameTime.ElapsedGameTime.TotalMilliseconds; if (PowerUp.Life <= 0) { PowerUp = null; } } } Delay = (Delay + 1) % 5; }
/// <summary> /// Prepares new GameBoard for new game /// </summary> public void NewGame() { var normalEnemyTexture = Game.Content.Load <Texture2D>(@"Enemies/normal"); var hunterEnemyTexture = Game.Content.Load <Texture2D>(@"Enemies/hunter"); var shooterEnemyTexture = Game.Content.Load <Texture2D>(@"Enemies/shooter"); var tankEnemyTexture = Game.Content.Load <Texture2D>(@"Enemies/tank"); var bombfireTexture = Game.Content.Load <Texture2D>(@"bombfire"); var bombTexture = Game.Content.Load <Texture2D>(@"bomb"); var hungerTexture = Game.Content.Load <Texture2D>(@"hungerbar"); var foodTexture = Game.Content.Load <Texture2D>(@"food"); var medpackTexture = Game.Content.Load <Texture2D>(@"medpack"); var tntDetonatorTexture = Game.Content.Load <Texture2D>(@"tntdetonator"); var chargingTexture = Game.Content.Load <Texture2D>(@"charging"); var tntTexture = Game.Content.Load <Texture2D>(@"tnt"); var debrisTexture1 = Game.Content.Load <Texture2D>(@"debris1"); var debrisTexture2 = Game.Content.Load <Texture2D>(@"debris2"); var debrisTexture3 = Game.Content.Load <Texture2D>(@"debris3"); var debrisTexture4 = Game.Content.Load <Texture2D>(@"debris4"); var wallDestroyableTexture = Game.Content.Load <Texture2D>(@"wall_destroyable"); gameBoard = new GameBoard(boardSize, 40, 40, 80, this, gameTimeFont, bombfireTexture, normalEnemyTexture, hunterEnemyTexture, shooterEnemyTexture, tankEnemyTexture, foodTexture, medpackTexture, tntDetonatorTexture, chargingTexture, tntTexture, bombTexture, debrisTexture1, debrisTexture2, debrisTexture3, debrisTexture4, wallDestroyableTexture); //Load the player sprite player = new UserControlledSprite( Game.Content.Load <Texture2D>(@"player_transparent"), new Point(0, 0), gameBoard, Game.Content.Load <Texture2D>(@"heart"), hungerTexture, Game.Content.Load <Texture2D>(@"player_transparent_yellow"), bombTexture, wallDestroyableTexture); gameBoard.AddObject(player); var r = new Random(); var wallSprite = Game.Content.Load <Texture2D>(@"wall"); //generate random walls for (int i = 0; i < 30; i++) { int X = r.Next(boardSize.X); int Y = r.Next(boardSize.Y); var position = new Point(X, Y); if (gameBoard.IsPositionValid(position) && Math.Sqrt(position.X * position.X + position.Y * position.Y) >= 3) { gameBoard.AddObject(new WallSprite(wallSprite, new Point(X, Y), gameBoard)); } } //generate random destroyable walls for (int i = 0; i < 50; i++) { int X = r.Next(boardSize.X); int Y = r.Next(boardSize.Y); var position = new Point(X, Y); if (gameBoard.IsPositionValid(position) && Math.Sqrt(position.X * position.X + position.Y * position.Y) >= 3) { var destrWallSprite = new DestroyableWallSprite(wallDestroyableTexture, new Point(X, Y), gameBoard); gameBoard.AddObject(destrWallSprite); } } }