2015-03-29 07:50:42 +00:00
|
|
|
# tiny-ecs #
|
2015-06-17 03:13:53 +00:00
|
|
|
|
2016-06-18 17:35:26 +00:00
|
|
|
[![Build Status](https://travis-ci.org/bakpakin/tiny-ecs.png?branch=master)](https://travis-ci.org/bakpakin/tiny-ecs)[![License](http://img.shields.io/badge/Licence-MIT-brightgreen.svg)](LICENSE)
|
2015-08-27 17:47:28 +00:00
|
|
|
|
2016-06-18 17:35:26 +00:00
|
|
|
Tiny-ecs is an Entity Component System for Lua that's simple, flexible, and useful.
|
|
|
|
Because of Lua's tabular nature, Entity Component Systems are a natural choice
|
2015-03-21 08:09:39 +00:00
|
|
|
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").
|
|
|
|
|
2016-06-18 17:35:26 +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
|
2016-06-18 17:35:26 +00:00
|
|
|
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
|
2015-05-17 00:26:04 +00:00
|
|
|
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 ##
|
2015-03-29 09:07:39 +00:00
|
|
|
Tiny-ecs has four important types: Worlds, Filters, Systems, and Entities.
|
2016-06-18 17:35:26 +00:00
|
|
|
Entities, however, can be any Lua table, and Filters are just functions that
|
2015-03-29 09:07:39 +00:00
|
|
|
take an Entity as a parameter.
|
2015-03-21 08:09:39 +00:00
|
|
|
|
|
|
|
### Entities ###
|
2016-06-18 17:35:26 +00:00
|
|
|
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
|
2015-03-29 09:07:39 +00:00
|
|
|
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
|
|
|
|
2015-03-29 09:07:39 +00:00
|
|
|
### Filters ###
|
2016-06-18 17:35:26 +00:00
|
|
|
Filters are used to select Entities. Filters can be any Lua function, but
|
2015-03-29 09:07:39 +00:00
|
|
|
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()
|
2015-04-21 13:47:36 +00:00
|
|
|
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
|
2016-06-18 17:35:26 +00:00
|
|
|
print(("%s who weighs %d pounds, says %q."):format(e.name, e.mass, e.phrase)
|
2015-04-21 13:47:36 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
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,
|
2016-06-18 17:35:26 +00:00
|
|
|
please use a tagged release or use luarocks.
|
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
|
2016-06-18 17:35:26 +00:00
|
|
|
[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
|
2015-03-29 03:19:22 +00:00
|
|
|
`busted` from the command line to test.
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-03-29 12:19:31 +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/).
|
2015-06-17 00:28:21 +00:00
|
|
|
See the original forum thread [here](https://love2d.org/forums/viewtopic.php?f=5&t=79937&p=182589).
|