using Unity.Collections; using Unity.Mathematics; namespace DanieleMarotta.RiversongCodeShowcase { public class RoadNetwork : NativeGrid { /// /// Indicates that the tile is occupied by a road. /// private const int TileOccupiedBit = 1 << 15; /// /// Mask selecting the bits indicating in which directions a road tile is connected to another road tile. /// private const int ConnectivityBits = 0xFF; public RoadNetwork(int2 size) : base(size, Allocator.Persistent) { } public void AddRoadTile(int2 tile) { SetValue(tile, TileOccupiedBit); UpdateRoadTile(tile); } public void UpdateRoadTile(int2 tile) { if (!IsRoadTile(tile)) return; ushort value = TileOccupiedBit; if (tile.x > 0 && GetValue(tile + new int2(-1, 0)) != 0) value |= (ushort)DirectionsMask4.West; if (tile.x < Size.x - 1 && GetValue(tile + new int2(1, 0)) != 0) value |= (ushort)DirectionsMask4.East; if (tile.y > 0 && GetValue(tile + new int2(0, -1)) != 0) value |= (ushort)DirectionsMask4.South; if (tile.y < Size.y - 1 && GetValue(tile + new int2(0, 1)) != 0) value |= (ushort)DirectionsMask4.North; SetValue(tile, value); } public void RemoveRoadTile(int2 tile) { SetValue(tile, 0); } public DirectionsMask8 GetConnectivity(int2 tile) { return (DirectionsMask8)(GetValue(tile) & ConnectivityBits); } public bool IsRoadTile(int2 tile) { return GetValue(tile) != 0; } } }