public override ConfirmedDirection GetDirection() { IDirectionChosen directionTowardsPlayer = this.Monster.DetermineDirectionTowardsPlayer(); if (!(directionTowardsPlayer is ConfirmedDirection)) { bool notPossibleToGoInDirectionTowardsPlayer = !this.Monster.ConfirmDirectionToMoveIn(directionTowardsPlayer, out ConfirmedDirection feasibleDirection); if (notPossibleToGoInDirectionTowardsPlayer || feasibleDirection == Direction.None) { return(feasibleDirection); } } // we know at this point that we could move in DirectionTowardsPlayer HOWEVER we don't want to actually step onto the same tile as the player TilePos potentiallyMovingTowards = this.Monster.TilePosition.GetPositionAfterOneMove(directionTowardsPlayer.Direction); if (potentiallyMovingTowards != GlobalServices.GameState.Player.TilePosition) { // it's okay. we'll be closer to the player without actually touching the player return(directionTowardsPlayer.Confirm()); } var directionAwayFromPlayer = MonsterMovement.AlterDirectionByVeeringAway(directionTowardsPlayer.Direction); this.Monster.ConfirmDirectionToMoveIn(directionAwayFromPlayer, out ConfirmedDirection result); return(result); }
/// <summary> /// Checks whether the monster can move in the specified direction. /// If it can't, it will try other directions in turn until it finds a direction that is available. /// </summary> /// <param name="m">The monster that is moving</param> /// <param name="intendedDirection">The direction the monster hopes to move in</param> /// <param name="feasibleDirection">The direction the monster can move in, or Direction.None if it's unable to move</param> /// <returns>True if the monster can move in the intendedDirection, or False otherwise</returns> public static bool ConfirmDirectionToMoveIn(this Monster m, IDirectionChosen intendedDirection, out ConfirmedDirection feasibleDirection) { if (intendedDirection.Direction == Direction.None) { feasibleDirection = ConfirmedDirection.None; return(true); } if (intendedDirection is ConfirmedDirection confirmedDirection) { feasibleDirection = confirmedDirection; return(true); } if (m.CanMoveInDirection(intendedDirection.Direction)) { feasibleDirection = intendedDirection.Confirm(); return(true); } var directionToTry = new PossibleDirection(intendedDirection.Direction); for (int i = 0; i < 3; i++) { directionToTry = GetNextDirection(directionToTry); if (m.CanMoveInDirection(directionToTry.Direction)) { feasibleDirection = directionToTry.Confirm(); return(false); } } feasibleDirection = ConfirmedDirection.None; return(false); }
protected ConfirmedDirection GetConfirmedDirection(IDirectionChosen selectedDirection) { if (selectedDirection is ConfirmedDirection || selectedDirection.Direction == Direction.None) { return(selectedDirection.Confirm()); } this.Monster.ConfirmDirectionToMoveIn(selectedDirection, out ConfirmedDirection feasibleDirection); return(feasibleDirection); }