Skip to content

Learning Entity-Component-System gamedev pattern with PICO-8.

License

Notifications You must be signed in to change notification settings

apa64/road-to-ecs

Repository files navigation

Road to ECS

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.

Getting Started

Prerequisites

Installation

Start up PICO-8. Load and run the .p8 cartridges.

cd road-to-ecs
load ecs_01_draw.p8
run

installation

Example Cartridges

Just look in the files, there's a lot of comments.

ecs_topdown1.p8

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.

ecs_01_draw.p8

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 :)

ecs_02_control.p8

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.

ecs_03_map.p8

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.

ecs_04_collision

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.

Other Files

tinyecs-1.1.lua

KatrinaKitten's Tiny ECS Framework v1.1 implementation, added in carts with #include.

Roadmap

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
  • ...

Resources

Notes and Learnings

  • 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.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

Distributed under the MIT License. See LICENSE for details.

Acknowledgments

This repository is made possible and builds on the work of the following talented people and projects:

Contact

About

Learning Entity-Component-System gamedev pattern with PICO-8.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages