Entity-Component-System pattern example code for PICO-8.
I'm teaching myself the Entity-Component-System (ECS) pattern for game development with PICO-8. This repository contains my learning project cartridges and example code. You can read more from my "Road to ECS" blog thread @ Lexaloffle BBS.
Start up PICO-8. Load and run the .p8
cartridges.
cd road-to-ecs
load ecs_01_draw.p8
run
Just look in the files, there's a lot of comments.
My first ECS based cartridge was MBoffin's Top-Down Adventure Game Tutorial game with ECS using selfsame's ECS library. It was too complex for learning the basics, that's why it doesn't use ECS pattern for all systems.
The cartride has a state machine (states: menu, game, gameover), selfsame's ECS lib, plenty of extras from alexr's ECS POC 2 1.0 and my constants. I also did some premature optimization (never do that, that's bad. seriously.) and changed all loops to use pairs()
instead of other iterator functions just because I read that it is faster.
Now I'm starting learning the pattern from the basics: how to draw entities on screen. This time I'm doing it with KatrinaKitten's Tiny ECS Framework. There are two entities: tables e_player
and e_thing
. Both have a few components. Finally there's two systems functions s_draw()
and s_randpos()
. Entities are created in init()
and systems are run in update()
and draw()
. I also reformatted the framework code to fit my style. It does not fit on PICO screen as well any more, sorry about that :)
A basic game feature is controlling a player avatar on screen. The controlled entity e_player
is the only one which has the c_control
component. That one is required by s_control()
. c_control
is an empty component with no data except name. Basically it's just a key or a flag to match s_control()
requirements.
Draw a map and do map cell based movement. Only the player is an entity, map is not. There's also a shortcut e_player
to avoid looping through ents
just to search for the player. pos
component represents map coordinates. MBoffin's Top-down-game tutorial inspired the map and camera functionality.
Collision detection between entities. Collision detection is not a "system", just a function call in movement system. Current version loops through all entities in ents
for every move. Collision check function credit: mboffin.itch.io/pico8-overlap.
KatrinaKitten's Tiny ECS Framework v1.1 implementation, added in carts with #include
.
I'm planning to have a go with more basic game features like the following:
- Map collision detection
- Components: c_position, c_velocity, c_collisionbox ?
- Interaction
- Turn based
- ...
- KatrinaKitten/Lexaloffle BBS: Tiny ECS Framework v1.1
- Compact ECS framework.
- selfsame/Lexaloffle BBS: Entity Component System
- ECS framework.
- alexr/Lexaloffle BBS: ECS POC 1 v. 0.5
- Utilities and extensions for selfsame's ECS.
- Klutzershy/gamedev.net: Understanding Component-Entity-Systems
- adam/T-machine.org: Designing Bomberman with an Entity System: Which Components?
- About design and principles.
- Entity Systems Wiki
- Richard Lord: Finite State Machines with Ash
- Owen Pellegrin: Simple Collision Detection
- Do systems first, components will follow.
- OOP leads to too tight coupling.
- Components are 1) only data, 2) as small as possible and 3) never share data (normalized).
- Entities are only containers for components.
- Systems are logic: manipulate component data.
- FSM does not work with ECS. States are implemented by changing entity's components: the state of an entity is encapsulated in its components.
- FSM: "define a component (one for every state machine) in which we store an integer value that represents our state"
c_movement_state = cmp("movement_state", { movement_state })
c_action_state = cmp("action_state", { action_state})
- Map tiles are not entities.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Distributed under the MIT License. See LICENSE
for details.
This repository is made possible and builds on the work of the following talented people and projects:
- KatrinaKitten @ Lexaloffle BBS
- selfsame @ Lexaloffle BBS
- alexr @ Lexaloffle BBS
- PurpleBooth/README-Template.md
- Make a README
- GitHub repository: apa64/road-to-ecs
- Twitter @apa64
- Lexaloffle BBS @apa64