
Go + ECS + Roguelike
This was a real fun project to work on. Implementing an entity component system in golang from a basic tutorial. Next steps would be to experiment utilizing SQLite instead of in-memory storage.
- https://www.roguebasin.com/index.php?title=How_to_Write_a_Roguelike_in_15_Steps
- https://www.fatoldyeti.com/
ECS: The Game Server
An Entity-Component-System (ECS) is a software architecture pattern commonly used in game development to organize and manage game objects and their behavior. It separates objects into three main parts: Entities, Components, and Systems, which helps improve flexibility, scalability, and performance. Here’s a high-level explanation of each part:
Entities
- What They Are: Entities are unique identifiers (often just an integer or a lightweight object) that represent individual game objects.
- What They Do: By themselves, entities don’t have any data or behavior. They are essentially placeholders or tags to which components can be attached.
Example:
- Entity 1 represents a player.
- Entity 2 represents an enemy.
Components
- What They Are: Components are modular pieces of data that define the attributes or properties of an entity.
- What They Do: Components don’t have behavior; they only store data. An entity becomes meaningful by attaching a collection of components to it.
Example:
- A player (Entity 1) might have:
- A
Positioncomponent (x: 10, y: 20). - A
Healthcomponent (value: 100). - A
Spritecomponent (image: player.png).
- A
- An enemy (Entity 2) might have:
- A
Positioncomponent (x: 50, y: 30). - A
Healthcomponent (value: 50). - A
Behaviorcomponent (type: aggressive).
- A
Systems
- What They Are: Systems are functions or processes that operate on entities with specific components.
- What They Do: Systems contain the logic of the game. They query entities that have the components the system cares about and perform operations on them.
Example:
- A Render System might:
- Query all entities with
PositionandSpritecomponents. - Draw the sprites at their respective positions.
- Query all entities with
- A Movement System might:
- Query all entities with
PositionandVelocitycomponents. - Update their positions based on velocity.
- Query all entities with
Why ECS is Useful
- Decoupling:
- Data (components) is separated from logic (systems), making code easier to maintain and extend.
- Flexibility:
- You can dynamically add, remove, or modify components for an entity, allowing objects to change behavior at runtime.
- Performance:
- Systems often operate on flat, contiguous arrays of data (all components of a specific type), making ECS cache-friendly and efficient for large-scale simulations.
- Scalability:
- It’s easier to manage thousands of entities since ECS focuses on efficient processing of data.
Example in Practice
Imagine a 2D game with a player and some enemies:
- Entities: Player, Enemy1, Enemy2.
- Components:
Position:(x, y)for spatial data.Velocity:(vx, vy)for movement.Health: For tracking hit points.Sprite: For graphical representation.
- Systems:
MovementSystem: Updates position based on velocity.CollisionSystem: Detects collisions between entities.RenderSystem: Draws entities with aSpriteat theirPosition.
Game Loop Workflow:
MovementSystemupdates allPositioncomponents.CollisionSystemchecks for collisions between entities and adjusts theirHealth.RenderSystemdraws all entities with aSpriteat their updatedPosition.
Summary
- Entities: Unique IDs representing objects.
- Components: Modular data that defines object properties.
- Systems: Logic that processes entities with specific components.