/// <summary> /// Gets the neighbors of this object within a given the world it lives in, within a given range. /// </summary> /// <param name="range">The range to find neighbors.</param> /// <param name="inhabitedWorld">The world the object lives in.</param> /// <returns>Returns an IResponse with a IEnumerable containing the neighbors if any.</returns> public virtual WorldObjectResponse GetNeighbors(double range, World inhabitedWorld) { WorldObjectResponse otherObjects = inhabitedWorld.WorldObjectsManager.GetWorldObjects(); var neighbors = from worldObject in otherObjects.WorldObjects where !ReferenceEquals(worldObject, this) && worldObject.Position.X <this.Position.X + range && worldObject.Position.X> this.Position.X - range && worldObject.Position.Y <this.Position.Y + range && worldObject.Position.Y> this.Position.Y - range select worldObject; if (neighbors.Any()) { return(new WorldObjectResponse ( $"These are the neighbors of the WorldObject: {Name}, Id: {Id}.", true, neighbors )); } return(new WorldObjectResponse( $"The WorldObject: {Name}, Id: {Id}, did not have any neighbors within the range of {range}, with the coordinates X:{Position.X},Y:{Position.Y}.", false, null )); }
public WorldObject[][] GetPlayerSurroundings(int range) { if (Player is null) { return(null); } int adjustedRange = (range * 2) + 1; WorldObject[][] Surroundings = new WorldObject[adjustedRange][]; for (int i = 0; i < Surroundings.Length; i++) { Surroundings[i] = new WorldObject[adjustedRange]; } foreach (var array in Surroundings) { for (int i = 0; i < array.Length; i++) { array[i] = null; } } Surroundings[range][range] = Player; WorldObjectResponse playerNeighbors = Player.GetNeighbors(range, WorldObject); List <WorldObject> worldObjects = playerNeighbors.SuccessValue ? playerNeighbors.WorldObjects.ToList() : null; if (worldObjects is null) { return(Surroundings); } foreach (var worldObject in worldObjects) { int differenceInX = (worldObject.Position.X - Player.Position.X) + range; int differenceInY = (worldObject.Position.Y - Player.Position.Y) + range; Surroundings[differenceInX][differenceInY] = worldObject; } return(Surroundings); }