Skip to content

Instantly share code, notes, and snippets.

@nilpunch
Last active May 13, 2025 16:02
Show Gist options
  • Select an option

  • Save nilpunch/e655ada5c191aa061fdee89f483a8624 to your computer and use it in GitHub Desktop.

Select an option

Save nilpunch/e655ada5c191aa061fdee89f483a8624 to your computer and use it in GitHub Desktop.
WIP auto allocator syntax.
using UnityEngine;
namespace Tetris
{
public struct Block
{
public Vector2Int Offset;
}
public struct Tetromino
{
public ListHandle<Block> Shape;
public Vector2Int Position;
}
public struct CreateBoxSystem
{
private DataSet<Tetromino> _tetrominos;
private AutoListAllocator<Block> _blocks;
private World _world;
public void Initialize(World world)
{
_world = world;
_tetrominos = world.DataSet<Tetromino>();
_blocks = world.AutoListAllocator<Block>();
}
public void Update(int createAmount)
{
for (int i = 0; i < createAmount; i++)
{
var entity = _world.Create();
var shape = _blocks.AllocAutoList(entity, 4);
shape.Add(new Block() { Offset = new Vector2Int(0, 0) });
shape.Add(new Block() { Offset = new Vector2Int(1, 0) });
shape.Add(new Block() { Offset = new Vector2Int(1, 1) });
shape.Add(new Block() { Offset = new Vector2Int(0, 1) });
_tetrominos.Set(entity, new Tetromino()
{
Position = Vector2Int.zero,
Shape = shape,
});
}
}
}
public struct FallSystem
{
private DataSet<Tetromino> _tetrominos;
private World _world;
public void Initialize(World world)
{
_world = world;
_tetrominos = world.DataSet<Tetromino>();
}
public void Update()
{
foreach (var id in _tetrominos)
{
ref var tetromino = ref _tetrominos.Get(id);
var shape = tetromino.Shape.In(_world);
var isCollidedWithFloor = false;
foreach (var block in shape)
{
if ((tetromino.Position + block.Offset).y <= 0)
{
isCollidedWithFloor = true;
break;
}
}
if (!isCollidedWithFloor)
{
tetromino.Position += Vector2Int.down;
}
}
}
}
public struct DestroyTetrominosSystem
{
private DataSet<Tetromino> _tetrominos;
private World _world;
public void Initialize(World world)
{
_world = world;
_tetrominos = world.DataSet<Tetromino>();
}
public void Update()
{
foreach (var id in _tetrominos)
{
_world.Destroy(id);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment