public void TestPlacingBombCommand() { var map = new GameMap(2, 1, 0); var player1 = new PlayerEntity(); player1.BombBag = 1; var block = map.GetBlockAtLocation(1, 1); block.SetEntity(player1); var transaction = new CommandTransaction(new DummyLogger()); var command = new PlaceBombCommand(); command.PerformCommand(map, player1, transaction); transaction.ValidateCommands(map); transaction.ProcessCommands(map); transaction = new CommandTransaction(new DummyLogger()); command = new PlaceBombCommand(); Assert.Throws <InvalidCommandException>(() => command.PerformCommand(map, player1, transaction), "Cannot place bomb in same location twice"); var moveCommand = new MovementCommand(MovementCommand.Direction.Right); moveCommand.PerformCommand(map, player1, transaction); transaction.ValidateCommands(map); transaction.ProcessCommands(map); transaction = new CommandTransaction(new DummyLogger()); command = new PlaceBombCommand(); Assert.Throws <InvalidCommandException>(() => command.PerformCommand(map, player1, transaction), "Player cannot place more bombs than is in the bomb bag"); }
public void TestPlayerMovementCommand() { var gameMap = GameMapTest.GenerateTestMap(); var player = gameMap.RegisteredPlayerEntities.First(); var transaction = new CommandTransaction(new DummyLogger()); var playerLocation = player.Location; var block = gameMap.GetBlockAtLocation(player.Location.X, player.Location.Y); block.PlantBomb(2); var command = new MovementCommand(MovementCommand.Direction.Up); Assert.Throws <InvalidCommandException>(() => command.PerformCommand(gameMap, player, transaction), "Player should not be able to occupy the space of another entity"); command = new MovementCommand(MovementCommand.Direction.Left); Assert.Throws <InvalidCommandException>(() => command.PerformCommand(gameMap, player, transaction), "Player should not be able to occupy the space of another entity"); command = new MovementCommand(MovementCommand.Direction.Right); command.PerformCommand(gameMap, player, transaction); Assert.Throws <InvalidCommandException>(() => command.PerformCommand(gameMap, player, transaction), "Player can only performa one command per transaction"); transaction.ValidateCommands(gameMap); transaction.ProcessCommands(gameMap); Assert.AreEqual(playerLocation.Y, player.Location.Y, "Player moved in the wrong direction"); Assert.AreEqual(playerLocation.X + 1, player.Location.X, "Player moved in the wrond direction"); command = new MovementCommand(MovementCommand.Direction.Left); Assert.Throws <InvalidCommandException>(() => command.PerformCommand(gameMap, player, transaction), "Player should not be able to occupy the space if a bomb is in the location"); gameMap = GameMapTest.GenerateTestMap(); player = gameMap.RegisteredPlayerEntities.First(); transaction = new CommandTransaction(new DummyLogger()); transaction = new CommandTransaction(new DummyLogger()); command = new MovementCommand(MovementCommand.Direction.Down); command.PerformCommand(gameMap, player, transaction); transaction.ValidateCommands(gameMap); transaction.ProcessCommands(gameMap); Assert.AreEqual(playerLocation.Y + 1, player.Location.Y, "Player moved in the wrong direction"); Assert.AreEqual(playerLocation.X, player.Location.X, "Player moved in the wrond direction"); }
/// <summary> /// Process the player commands. Player commands are processed in a commands transaction, /// the transaction is verified, removing invalid commands, and then the commands are persisted onto the game map /// </summary> protected void ProcessPlayerCommands() { _logger.LogDebug("Processing Player Commands"); var hadCommands = _commandsToProcess.Any(); var transaction = new CommandTransaction(_logger); foreach (var command in _commandsToProcess) { if (command.Key.PlayerEntity.Killed || _entitiesToDestroy.Contains(command.Key.PlayerEntity)) { _logger.LogInfo(String.Format("Player {0} has been killed, and the command {1} will be ignored", command.Key.PlayerEntity, command.Value)); continue; } try { command.Value.PerformCommand(_gameMap, command.Key.PlayerEntity, transaction); } catch (InvalidCommandException ex) { command.Key.PlayerCommandFailed(command.Value, ex.Message); _logger.LogException(String.Format("Failed to process command {0} for player {1}", command.Value, command.Key.PlayerEntity), ex); } } transaction.ValidateCommands(_gameMap); transaction.ProcessCommands(_gameMap); _commandsToProcess.Clear(); //Do another round of marking entities for destruction, just incase a player moved into a bomb blast if (hadCommands) { MarkEntitiesForDestruction(); } }
public void TestPlayerCollidingMovementCommand() { var map = new GameMap(3, 1, 0); var player1 = new PlayerEntity(); var player2 = new PlayerEntity(); var block = map.GetBlockAtLocation(1, 1); block.SetEntity(player1); block = map.GetBlockAtLocation(3, 1); block.SetEntity(player2); var transaction = new CommandTransaction(new DummyLogger()); new MovementCommand(MovementCommand.Direction.Right).PerformCommand(map, player1, transaction); new MovementCommand(MovementCommand.Direction.Left).PerformCommand(map, player2, transaction); transaction.ValidateCommands(map); transaction.ProcessCommands(map); Assert.AreNotEqual(player1.Location.X, player2.Location.X, "Only one of the players should have moved to the location"); }