public IEnumerable <GameCell> GetAdjacentCells(Pos2D pos) { foreach (var dir in new[] { MoveDirection.Up, MoveDirection.Right, MoveDirection.Down, MoveDirection.Left }) { var cell = GetCell(pos.GetNeighbor(dir)); if (cell != null) { yield return(cell); } } }
public void MoveObject(GameObjectBase obj, Pos2D newPos) { var currentCell = GetCell(obj.Pos); currentCell?.RemoveObject(obj); var newCell = GetCell(newPos); newCell.AddObject(obj); ClearVisibilityCache(); }
private static Pos2D CreatePos2DFromString(string input) { if (string.IsNullOrWhiteSpace(input)) { return(new Pos2D()); } var strings = input.Split(','); var pos = new Pos2D(int.Parse(strings[0]), int.Parse(strings[1])); return(pos); }
public IEnumerable <GameObjectBase> GetTargetsAtPos(Pos2D pos) { var cell = GetCell(pos); if (cell == null) { yield break; } foreach (var obj in cell.Objects.Where(o => o.IsTargetable)) { yield return(obj); } }
public IEnumerable <GameCell> GetCellAndAdjacent(Pos2D pos) { var cell = GetCell(pos); if (cell != null) { yield return(cell); } foreach (var adjacentCell in GetAdjacentCells(pos)) { yield return(adjacentCell); } }
public bool HasSightBlocker(Pos2D pos) { // Attempt to grab from cache if (_sightBlockerCache.ContainsKey(pos)) { return(_sightBlockerCache[pos]); } // Calculate whether or not things have a sight blocker var cell = GetCell(pos); var hasSightBlocker = cell != null && cell.BlocksSight; // Cache it for next time _sightBlockerCache[pos] = hasSightBlocker; return(hasSightBlocker); }
public IEnumerable <GameCell> GetCellsInSquare(Pos2D pos, int radius) { int minX = pos.X - radius; int maxX = pos.X + radius; int minY = pos.Y - radius; int maxY = pos.Y + radius; for (int y = minY; y <= maxY; y++) { for (int x = minX; x <= maxX; x++) { var cell = GetCell(new Pos2D(x, y)); if (cell != null) { yield return(cell); } } } }
/// <summary> /// Gets the cell at the specified position, or returns null if no cell was found. /// </summary> /// <param name="pos">The position.</param> /// <returns>The cell.</returns> public GameCell GetCell(Pos2D pos) => !CellsDictionary.ContainsKey(pos) ? null : _cells[pos];
public bool IsPosExterior(Pos2D pos) => pos.X == UpperLeft.X || pos.X == LowerRight.X || pos.Y == LowerRight.Y || pos.Y == UpperLeft.Y;