tiny-ecs/README.md

86 lines
3.0 KiB
Markdown
Raw Normal View History

2015-03-29 07:50:42 +00:00
# tiny-ecs #
Tiny-ecs is an Entity Component System for lua that's simple, flexible, and useful.
2015-03-21 08:09:39 +00:00
Because of lua's tabular nature, Entity Component Systems are a natural choice
for simulating large and complex systems. For more explanation on Entity
Component Systems, here is some
[basic info](http://en.wikipedia.org/wiki/Entity_component_system "Wikipedia").
2015-04-25 10:30:05 +00:00
Tiny-ecs also works well with Objected Oriented programming in lua because
Systems and Entities do not use metatables. This means you can subclass your
Systems and Entities, and use existing lua class frameworks with tiny-ecs, no problem.
2015-03-21 08:09:39 +00:00
## Use It ##
2015-03-29 07:50:42 +00:00
Copy paste tiny.lua into your source folder.
2015-03-21 08:09:39 +00:00
## Overview ##
Tiny-ecs has four important types: Worlds, Filters, Systems, and Entities.
Entities, however, can be any lua table, and Filters are just functions that
take an Entity as a parameter.
2015-03-21 08:09:39 +00:00
### Entities ###
Entities are simply lua tables of data that gets processed by Systems. Entities
2015-04-25 10:30:05 +00:00
should contain primarily data rather than code, as it is the Systems's job to
2015-03-21 08:09:39 +00:00
do logic on data. Henceforth, a key-value pair in an Entity will
be referred to as a Component.
### Worlds ###
2015-03-29 07:50:42 +00:00
Worlds are the outermost containers in tiny-ecs that contain both Systems
2015-03-21 08:09:39 +00:00
and Entities. In typical use, only one World is used at a time.
### Systems ###
2015-03-29 07:50:42 +00:00
Systems in tiny-ecs describe how to update Entities. Systems select certain Entities
using a Filter, and then only update those select Entities. Some Systems don't
update Entities, and instead just act as function callbacks every update. Tiny-ecs
2015-04-25 10:30:05 +00:00
provides functions for creating Systems easily, as well as creating Systems that
can be used in an Object Orientented fashion.
2015-03-21 08:09:39 +00:00
### Filters ###
Filters are used to select Entities. Filters can be any lua function, but
tiny-ecs provides some functions for generating common ones, like selecting
only Entities that have all required components.
2015-03-21 08:09:39 +00:00
## Example ##
```lua
2015-03-29 07:50:42 +00:00
local tiny = require("tiny")
2015-03-21 08:09:39 +00:00
2015-04-25 10:36:59 +00:00
local talkingSystem = tiny.processingSystem()
talkingSystem.filter = tiny.requireAll("name", "mass", "phrase")
2015-04-22 09:23:45 +00:00
function talkingSystem:process(e, dt)
e.mass = e.mass + dt * 3
print(e.name .. ", who weighs " .. e.mass .. " pounds, says, \"" .. e.phrase .. "\"")
end
2015-03-21 08:09:39 +00:00
local joe = {
name = "Joe",
phrase = "I'm a plumber.",
2015-03-22 04:35:08 +00:00
mass = 150,
2015-03-21 08:09:39 +00:00
hairColor = "brown"
}
local world = tiny.world(talkingSystem, joe)
2015-03-21 08:09:39 +00:00
2015-03-22 04:35:08 +00:00
for i = 1, 20 do
world:update(1)
end
2015-03-21 08:09:39 +00:00
```
2015-04-27 06:20:34 +00:00
## Demo ##
Check out the [demo](https://github.com/bakpakin/tiny-ecs/tree/demo-commandokibbles), a game
originally written for Ludum Dare 32 with the theme 'An Unconventioanl Weapon'. The demo uses
[LOVE](https://love2d.org/), an amazing game framework for lua.
2015-03-21 08:09:39 +00:00
## Testing ##
2015-03-29 07:50:42 +00:00
Tiny-ecs uses [busted](http://olivinelabs.com/busted/) for testing. Install and run
`busted` from the command line to test.
2015-03-21 08:09:39 +00:00
## Documentation ##
2015-03-29 12:39:59 +00:00
See API [here](http://bakpakin.github.io/tiny-ecs/doc/).
Documentation can be generated locally with [LDoc](http://stevedonovan.github.io/ldoc/).
2015-03-21 08:09:39 +00:00
## TODO ##
* More testing
* Performance testing / optimization
2015-04-27 06:20:34 +00:00
* Add more System types
* Improve Documentation