riversong code showcase

This commit is contained in:
Daniele Marotta
2026-05-21 15:52:18 +02:00
commit 4c9eea1c02
462 changed files with 23406 additions and 0 deletions

View 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;
}
}
}