You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
866 B
31 lines
866 B
using System.Runtime.CompilerServices; |
|
using System.Runtime.InteropServices; |
|
|
|
[Tool] |
|
[GlobalClass] |
|
public partial class TerrainChunk |
|
: Resource |
|
{ |
|
// ChunkShift of 5 results in a ChunkSize of 32. |
|
public const int Shift = 5; |
|
public const int Mask = ~(~0 << Shift); |
|
public const int Size = Mask + 1; |
|
|
|
static readonly int SizeInBytes = Size * Size * Unsafe.SizeOf<Tile>(); |
|
|
|
[Export] public byte[] Data { get; set; } = new byte[SizeInBytes]; |
|
|
|
public ref Tile this[TilePos pos] { get { |
|
var tiles = MemoryMarshal.Cast<byte, Tile>(Data); |
|
return ref tiles[GetIndex(pos)]; |
|
} } |
|
|
|
public static Vector2I ToChunkPos(TilePos pos) |
|
=> new(pos.X >> Shift, pos.Z >> Shift); |
|
|
|
public static Vector2I ToTileOffset(Vector2I chunkPos) |
|
=> new(chunkPos.X << Shift, chunkPos.Y << Shift); |
|
|
|
static int GetIndex(TilePos pos) |
|
=> (pos.X & Mask) | ((pos.Z & Mask) << Shift); |
|
}
|
|
|