riversong code showcase
This commit is contained in:
60
Source/Riversong/Game/World/Roads/RoadNetwork.cs
Normal file
60
Source/Riversong/Game/World/Roads/RoadNetwork.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Unity.Collections;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class RoadNetwork : NativeGrid<ushort>
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates that the tile is occupied by a road.
|
||||
/// </summary>
|
||||
private const int TileOccupiedBit = 1 << 15;
|
||||
|
||||
/// <summary>
|
||||
/// Mask selecting the bits indicating in which directions a road tile is connected to another road tile.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user