tiny-ecs/README.md

89 lines
3.5 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-05-07 11:45:28 +00:00
Tiny-ecs also works well with objected oriented programming in lua because
2015-04-25 10:30:05 +00:00
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.
For an example on how to use tiny-ecs with object-oriented lua, take a look at the
demo branch, specifically the systems and entities sub-directories.
2015-04-25 10:30:05 +00:00
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
2015-06-07 22:47:50 +00:00
can be used in an object oriented 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:24:59 +00:00
## Use It ##
2015-05-04 04:05:53 +00:00
Copy paste tiny.lua into your source folder. For stability and consistent API,
please use a tagged release or use luarocks. Tagged releases will have a version
2015-06-07 22:47:50 +00:00
number in `tiny._VERSION`, while other commits will just have the string 'scm'.
2015-04-27 06:24:59 +00:00
## Luarocks ##
2015-04-27 13:48:27 +00:00
Tiny-ecs is also on [Luarocks](https://luarocks.org/) and can be installed with
2015-04-27 06:24:59 +00:00
`luarocks install tiny-ecs`.
2015-04-27 06:20:34 +00:00
## Demo ##
2015-04-27 13:48:27 +00:00
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 Unconventional Weapon'. The demo uses
[LÖVE](https://love2d.org/), an amazing game framework for lua.
2015-04-27 06:20:34 +00:00
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/).
2015-05-07 11:45:28 +00:00
For the most up-to-date documentation, read the source code, or generate the HTML
locally with [LDoc](http://stevedonovan.github.io/ldoc/).
See the original forum thread [here](https://love2d.org/forums/viewtopic.php?f=5&t=79937&p=182589).