private bool checkIfValidMove(IPosition currentPosition, IPosition newPosition, ILayer layer) { bool characterDoesNotExist = layer.GetCharacter(currentPosition) == null; if (characterDoesNotExist) { return(false); } bool newPositionIsNotValid = layer.PositionIsValid(newPosition) == false; if (newPositionIsNotValid) { return(false); } ICharacter characterOnNewPos = layer.GetCharacter(newPosition); bool newPositionIsNotFree = characterOnNewPos != null; if (newPositionIsNotFree) { return(false); } bool newPositionIsNotWalkable = layer.GetTile(newPosition).Walkable == false; if (newPositionIsNotWalkable) { return(false); } return(true); }
//Moves a character in the layer from oldPosition to newPosition //If move is invalid return false //If successful returns true public bool MoveCharacter(IPosition currentPosition, IPosition newPosition, ILayer layer) { if (checkIfValidMove(currentPosition, newPosition, layer)) { ICharacter characterToMove = layer.GetCharacter(currentPosition); layer.RemoveCharacter(characterToMove); characterToMove.Position = newPosition; layer.AddCharacter(characterToMove); return(true); } return(false); }
public void MovementTest_PlayerMoves_CharacterIsUpdated() { _layer.PositionIsValid(Arg.Any <IPosition>()).Returns(true); _layer.GetCharacter(Arg.Is <IPosition>(c => c == _currentPosition)).Returns(_character); _layer.GetCharacter(Arg.Is <IPosition>(c => c == _targetPosition)).Returns((ICharacter)null); _layer.GetTile(Arg.Any <IPosition>()).Walkable.Returns(true); _currentPosition = Substitute.For <IPosition>(); _currentPosition.X.Returns(3); _currentPosition.Y.Returns(3); _targetPosition = Substitute.For <IPosition>(); _targetPosition.X.Returns(4); _targetPosition.Y.Returns(3); _character.Position.X.Returns(3); _character.Position.Y.Returns(3); _uut.MoveCharacter(_currentPosition, _targetPosition, _layer); Assert.AreEqual(_targetPosition, _character.Position); }
private void MoveCharacter(ICharacter movingCharacter, IPosition targetPosition, ILayer layer) { //Attempt to move character, will return false if tile is not walkable or is already occupied if (!_movement.MoveCharacter(movingCharacter.Position, targetPosition, layer)) { //Moving didn't work, find out if failure was because it was occupied ICharacter characterOnTile = layer.GetCharacter(targetPosition); if (characterOnTile != null) { _combatHandler.Fight(movingCharacter, characterOnTile, layer); // Try to move again will succed if opponent is dead _movement.MoveCharacter(movingCharacter.Position, targetPosition, layer); } } }