public CreatureCommand Act(int x, int y) { var command = new CreatureCommand(); var diggerPosition = FindDigger(); if (diggerPosition == null) { return(command); } var distance = Math.Sqrt(Math.Pow(diggerPosition.X - x, 2) + Math.Pow(diggerPosition.Y - y, 2)); if (distance > 4) { return(command); } if (y > 0 && FindObstacle(x, y - 1) && diggerPosition.Y < y) { command.DeltaY = -1; } else if (y < Game.Height - 1 && FindObstacle(x, y + 1) && diggerPosition.Y > y) { command.DeltaY = 1; } else if (x < Game.Width - 1 && FindObstacle(x + 1, y) && diggerPosition.X > x) { command.DeltaX = 1; } else if (x > 0 && FindObstacle(x - 1, y) && diggerPosition.X < x) { command.DeltaX = -1; } return(command); }
public CreatureCommand Act(int x, int y, Game game) { var player = new CreatureCommand(); var key = game.KeyPressed; if (!HaveExit(game)) { return(player); } // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault switch (key) { case Keys.W when y - 1 >= 0 && !(game.Map[x, y - 1] is Wall) && (!(game.Map[x, y - 1] is Door) || Game.HaveKey) && (!(game.Map[x, y - 1] is Key) || !Game.HaveKey): _walk.Play(); player.DeltaY = -1; if (game.Map[x, y - 1] is Gun) { return new CreatureCommand() { DeltaY = -1, TransformTo = new PlayerWithGun() } } ; break; case Keys.D when x + 1 < game.Width && !(game.Map[x + 1, y] is Wall) && (!(game.Map[x + 1, y] is Door) || Game.HaveKey) && (!(game.Map[x + 1, y] is Key) || !Game.HaveKey): _walk.Play(); player.DeltaX = 1; if (game.Map[x + 1, y] is Gun) { return new CreatureCommand() { DeltaX = 1, TransformTo = new PlayerWithGun() } } ; break; case Keys.S when y + 1 < game.Height && !(game.Map[x, y + 1] is Wall) && (!(game.Map[x, y + 1] is Door) || Game.HaveKey) && (!(game.Map[x, y + 1] is Key) || !Game.HaveKey): _walk.Play(); player.DeltaY = 1; if (game.Map[x, y + 1] is Gun) { return new CreatureCommand() { DeltaY = 1, TransformTo = new PlayerWithGun() } } ; break; case Keys.A when x - 1 >= 0 && !(game.Map[x - 1, y] is Wall) && (!(game.Map[x - 1, y] is Door) || Game.HaveKey) && (!(game.Map[x - 1, y] is Key) || !Game.HaveKey): _walk.Play(); player.DeltaX = -1; if (game.Map[x - 1, y] is Gun) { return new CreatureCommand() { DeltaX = -1, TransformTo = new PlayerWithGun() } } ; break; } return(player); }