Skip to content

Instantly share code, notes, and snippets.

@d-bucur
Last active August 20, 2025 01:45
Show Gist options
  • Select an option

  • Save d-bucur/466407a3d25aa331724ad9add99de9ed to your computer and use it in GitHub Desktop.

Select an option

Save d-bucur/466407a3d25aa331724ad9add99de9ed to your computer and use it in GitHub Desktop.
ECS factory pattern
static class Prefabs {
static Entity SpawnEnemy(Vec2I pos, EnemyType enemyType) {
Entity entt = PrepEnemyCommon(pos);
switch (enemyType) {
case EnemyType.Skeleton:
entt.Add(
new EntityName { value = "Skeleton" },
new TextureWithSource(Assets.monsterTexture) {
TileIdx = new Vec2I(0, 4)
},
new Fighter(6, new Dice(1, 2), 6),
new Energy() { GainPerTick = 5 }
);
break;
//....
}
return entt;
}
static Entity PrepEnemyCommon(Vec2I pos) {
var entt = Singleton.World.CreateEntity(
new GridPosition(pos.X, pos.Y),
new Position(pos.X, 0, pos.Y),
new RotationSingle(0f),
new Scale3(1, 1, 1),
new Billboard(),
new ColorComp(),
new EnemyAI(),
new Pathfinder(Singleton.Get<Grid>()),
new PathMovement(),
new Team { Value = 2 },
Tags.Get<Enemy, Character, BlocksPathing>()
);
entt.OnTagsChanged += Movement.OnEnemyVisibilityChange;
entt.AddSignalHandler<DeathSignal>(Combat.EnemyDeath);
return entt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment