Gruppe22.Backend.Map.ClosestEnemy C# (CSharp) Метод

ClosestEnemy() публичный Метод

Get coordinates for closest enemy within a specified radius
public ClosestEnemy ( Coords coords, int radius = 4, bool includePlayer = true, bool includeNPC = true, bool includeEnemy = true ) : Backend.Coords
coords Coords Center point to start checking from
radius int Number of squares to move up/left/right/down
includePlayer bool true if player should be an "enemy"
includeNPC bool true if NPCs should be "enemies"
includeEnemy bool true if monsters should be "enemies"
Результат Backend.Coords
        public Backend.Coords ClosestEnemy(Coords coords, int radius = 4, bool includePlayer = true, bool includeNPC = true, bool includeEnemy = true)
        {
            for (int distance = 0; distance <= radius; ++distance)
            {
                for (int x = Math.Max(coords.x - distance, 0); x <= Math.Min(coords.x + distance, _width); ++x)
                {
                    if ((((includePlayer && this[x, coords.y - distance].hasPlayer))
                        //                    ||((includeNPC&&this[coords.x-distance,y].hasNPC))
                    || ((includeEnemy && this[x, coords.y - distance].hasEnemy)))
                        && ((x != coords.x) || (distance != 0)))
                    {
                        //System.Diagnostics.Debug.WriteLine(coords.x + "/" + coords.y + "->" + x + "/" + (coords.y - distance).ToString() + " = " + (this[x, coords.y - distance].hasPlayer ? "Player" : "Enemy"));
                        return new Backend.Coords(x, coords.y - distance);
                    }
                    if ((((includePlayer && this[x, coords.y + distance].hasPlayer))
                        //                    ||((includeNPC&&this[coords.x-distance,y].hasNPC))
                    || ((includeEnemy && this[x, coords.y + distance].hasEnemy)))
                                            && ((x != coords.x) || (distance != 0)))
                    {
                        // System.Diagnostics.Debug.WriteLine(coords.x + "/" + coords.y + "->" + x + "/" + (coords.y + distance).ToString() + " = " + (this[x, coords.y + distance].hasPlayer ? "Player" : "Enemy"));

                        return new Backend.Coords(x, coords.y + distance);
                    }
                }
                for (int y = Math.Max(coords.y - distance, 0); y <= Math.Min(coords.y + distance, _height); ++y)
                {
                    if ((((includePlayer && this[coords.x - distance, y].hasPlayer))
                        //                    ||((includeNPC&&this[coords.x-distance,y].hasNPC))
                    || ((includeEnemy && this[coords.x - distance, y].hasEnemy)))
                        && ((y != coords.y) || (distance != 0)))
                    {
                        //   System.Diagnostics.Debug.WriteLine(coords.x + "/" + coords.y + "->" + (coords.x - distance) + "/" + y.ToString() + " = " + (this[coords.x - distance, y].hasPlayer ? "Player" : "Enemy"));

                        return new Backend.Coords(coords.x - distance, y);
                    }
                    if ((((includePlayer && this[coords.x + distance, y].hasPlayer))
                        //                    ||((includeNPC&&this[coords.x-distance,y].hasNPC))
                    || ((includeEnemy && this[coords.x + distance, y].hasEnemy)))
                        && ((y != coords.y) || (distance != 0)))
                    {
                        //  System.Diagnostics.Debug.WriteLine(coords.x + "/" + coords.y + "->" + (coords.x + distance) + "/" + y.ToString() + " = " + (this[coords.x + distance, y].hasPlayer ? "Player" : "Enemy"));

                        return new Backend.Coords(coords.x + distance, y);
                    }
                }
            }
            return new Backend.Coords(-1, -1);
        }