As an example of the idea that you add components to entities, here's some code I've been fiddling with lately:
Code: Select all
local Colored = Component {
_color = {0, 0, 0, 255}
}
function Colored:color(r, g, b, a)
if r==nil and g==nil and b==nil and a==nil then
return self._color[1], self._color[2], self._color[3], self._color[4]
else
local old = self._color
self._color = { r or old[1], g or old[2], b or old[3], a or old[4] }
end
return self
end
In this piece of code, I establish a component, Colored, that carries this one function that lets me define or access the color for an entity.
Code: Select all
local ColoredDraw = Component {
}
function ColoredDraw:init()
self:import(Colored):import(Rectangular)
self:on('draw', self.drawColoredRect)
end
function ColoredDraw:drawColoredRect(offset)
local ox, oy = offset and offset.x or 0, offset and offset.y or 0
local x, y, w, h = self:rectangle()
Graphics:setColor(self:color())
love.graphics.rectangle("fill", x+ox, y+oy, w, h)
return self
end
This next piece is a component that depends on Colored and Rectangular, that will make use of those two components to draw a colored rectangle on the screen.
The definition of the abstract "Component" is trivial enough that you don't need to see it, it's basically just a way to copy everything from one table to another, sans the key "init" which has the special meaning that it'll be called when an entity imports the component the first time, and a __call metamethod to make it easier to use. Also, the "on" function is that Observer thing I mentioned in my earlier post, which I've actually open sourced that code
here.
What this gets me is that I can declare things like so:
Code: Select all
local things = ColoredDraw(Entity())
:rectangle(200, 100, 64, 64)
:color(255, 0, 0)
And thats it in a nutshell. Instead of complex declarations, I create tables that are copied into the final entity table.
I'm still learning this stuff myself, but I'm please with what I've been experimenting with. To get a better feel for how to do this stuff, I'd say you should cross the void and look at
http://craftyjs.com/ to see how they explain Entity systems. It's very interesting stuff.