feat: add block model with IBlock flyweight and registry
This commit is contained in:
26
World/BlockRegistry.cs
Normal file
26
World/BlockRegistry.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using VoxelEngine.World.Blocks;
|
||||
|
||||
namespace VoxelEngine.World;
|
||||
|
||||
public sealed class BlockRegistry
|
||||
{
|
||||
private readonly IBlock[] _byId;
|
||||
|
||||
public AirBlock Air { get; } = new();
|
||||
public StoneBlock Stone { get; } = new();
|
||||
public DirtBlock Dirt { get; } = new();
|
||||
public GrassBlock Grass { get; } = new();
|
||||
|
||||
public BlockRegistry()
|
||||
{
|
||||
// Index by id. Order does not matter; slot = block.Id.
|
||||
var blocks = new IBlock[] { Air, Stone, Dirt, Grass };
|
||||
_byId = new IBlock[blocks.Length];
|
||||
foreach (IBlock block in blocks)
|
||||
{
|
||||
_byId[block.Id] = block;
|
||||
}
|
||||
}
|
||||
|
||||
public IBlock Get(ushort id) => _byId[id];
|
||||
}
|
||||
Reference in New Issue
Block a user