Exemple #1
0
 public bool ShouldRender(int renderDistance)
 {
     return(Pos.DistanceTo(SharpCraft.Instance.Camera.Pos.Xz) < renderDistance * ChunkSize);
 }
Exemple #2
0
        /// <summary>
        /// Method that implements the AI movement of the minion and boss
        /// entities.
        /// </summary>
        /// <param name="board">Reference to the level's board.</param>
        /// <returns>A coord which represents where the enemy should move.
        /// </returns>
        public override Coord WhereToMove(Board board)
        {
            Entity target = null;

            Entity[,] level = board.CurrentBoard;
            // Finds player location on board
            foreach (Entity e in level)
            {
                if (e != null)
                {
                    if (e.kind == EntityKind.Player)
                    {
                        target = e;
                        continue;
                    }
                }
            }

            // Stores current distance to target
            int distanceToTarget = Pos.DistanceTo(target.Pos);

            // Creates a list with every direction
            List <Direction> PossibleDirections = new List <Direction>();

            PossibleDirections.Add(Direction.Up);
            PossibleDirections.Add(Direction.Right);
            PossibleDirections.Add(Direction.Down);
            PossibleDirections.Add(Direction.Left);

            // Creates list of shorter paths, and all possible moves
            List <Direction> legalMoves   = new List <Direction>();
            List <Direction> shorterPaths = new List <Direction>();


            // For every direction, verifies if the move is legal
            foreach (Direction d in PossibleDirections)
            {
                // Gets the destination
                Coord dest = board.GetNeighbor(Pos, d);

                // Verifies if it is on board and not obstructed
                if (board.IsOnBoard(dest) && !board.IsObstructed(dest) &&
                    !board.IsExit(dest))
                {
                    // Adds it to the legalMoves list
                    legalMoves.Add(d);

                    // Verifies if it shortens the path to player
                    int newDistanceToTarget = dest.DistanceTo(target.Pos);
                    if (newDistanceToTarget < distanceToTarget)
                    {
                        shorterPaths.Add(d);
                    }
                }
            }

            // Returns possible destiny in which the distance is shorter, if it
            // exists
            if (shorterPaths.Count > 0)
            {
                int random = Game.rand.Next(shorterPaths.Count);

                return(board.GetNeighbor(Pos, shorterPaths[random]));
            }

            // If no path shortens the distance to target, returns a random
            // possible destiny
            if (legalMoves.Count > 0)
            {
                int random = Game.rand.Next(legalMoves.Count);

                return(board.GetNeighbor(Pos, legalMoves[random]));
            }


            // If there is no possible direction to move, stays at same position
            return(Pos);
        }