public void UpdateArea(int startX, int startY, int endX, int endY) { for (int y = startY; y < endY; y++) { for (int x = startX; x < endX; x++) { Tile tile = floor.GetTile(x, y); TileProperties props = tile.Properties; bool trigger = props.trigger; bool passable = !props.hasCollider || trigger; int cost = 0; if (!passable) { cost = 1000; } else { cost = trigger ? 100 : 1; } grid[x, y] = new PathCellInfo(passable, trigger, cost); } } }
private void FillPathCellInfo() { pathGrid = new PathCellInfo[levelBounds.width, levelBounds.height]; for (int y = levelBounds.min.y; y < levelBounds.max.y; ++y) { for (int x = levelBounds.min.x; x < levelBounds.max.x; ++x) { Tile tile = GetTile(x, y); bool passable = TileManager.GetData(tile).passable; pathGrid[x, y] = new PathCellInfo(passable, false, passable ? 0 : int.MaxValue); } } pathfinder = new Pathfinder(this, pathGrid); }
// Sets a tile at the given world location. Computes the chunk the tile belongs // in, and creates it if it doesn't exist. public Chunk SetTile(int wX, int wY, Tile tile) { Vector2Int cP = Utils.WorldToChunkP(wX, wY); Chunk chunk = GetChunk(cP); if (chunk == null) { chunk = new Chunk(cP.x, cP.y); chunks.Add(cP, chunk); } Vector2Int rel = Utils.WorldToRelP(wX, wY); chunk.SetTile(rel.x, rel.y, tile); TileData data = TileManager.GetData(tile); if (wX >= 0 && wX < pathGrid.GetLength(0) && wY >= 0 && wY < pathGrid.GetLength(1)) { pathGrid[wX, wY] = new PathCellInfo(data.passable, false, data.passable ? 0 : int.MaxValue); } return(chunk); }
private void GetSuccessors(PathNode current, Vec2i pos) { successorCount = 0; for (int i = 0; i < 4; i++) { Vec2i next = pos + Vec2i.Directions[i]; if (floor.InBounds(next) && grid[next.x, next.y].passable) { successors[successorCount++] = GetNode(next); } } for (int i = 4; i < 8; i++) { Vec2i dir = Vec2i.Directions[i]; Vec2i next = pos + dir; if (floor.InBounds(next)) { PathCellInfo diag = grid[next.x, next.y]; if (diag.passable) { PathCellInfo adjX = grid[pos.x + dir.x, pos.y]; PathCellInfo adjY = grid[pos.x, pos.y + dir.y]; if (adjX.passable && !adjX.trigger && adjY.passable && !adjY.trigger) { successors[successorCount++] = GetNode(next); } } } } }