Implementing features in an Entity System
Posted
by
Bane
on Game Development
See other posts from Game Development
or by Bane
Published on 2012-07-21T01:33:03Z
Indexed on
2012/09/04
21:52 UTC
Read the original article
Hit count: 452
After asking two questions on Entity Systems (1, 2), and reading some articles on them, I think that I understand them much better than before.
But, I still have some uncertainties, and mainly they are about building a Particle Emitter, an Input system, and a Camera. I obviously still have some problems understanding Entity Systems, and they might apply to a whole other range of objects, but I chose these three because they are very different concepts and should cover a pretty big ground, and help me understand Entity Systems and how to handle problems like these myself, as they come along.
I am building an engine in Javascript, and I've implemented most of the core features, which include: input handling, flexible animation system, particle emitter, math classes and functions, scene handling, a camera and a render, and a whole bunch of other things that engines usually support.
Then, I read Byte56's answer that got me interested into making the engine into an Entity System one. It would still remain an HTML5 game engine with the basic Scene philosophy, but it should support dynamic creation of entities from components.
These are some of the definitions from the previous questions, updated:
An Entity is an identifier. It doesn't have any data, it's not an object, it's a simple id that represents an index in the Scene's list of all entities (which I actually plan to implement as a component matrix).
A Component is a data holder, but with methods that can operate on that data. The best example is a Vector2D
, or a "Position" component. It has data: x
and y
, but also some methods that make operating on the data a bit easier: add()
, normalize()
, and so on.
A System is something that can operate on a set of entities that meet the certain requirements, usually they (the entities) need to have a specified (by the system itself) set of components to be operated upon. The system is the "logic" part, the "algorithm" part, all the functionality supplied by components is purely for easier data management.
The problem that I have now is fitting my old engine concept into this new programming paradigm.
Lets start with the simplest one, a Camera.
The camera has a position property (Vector2D
), a rotation property and some methods for centering it around a point. Each frame, it is fed to a renderer, along with a scene, and all the objects are translated according to it's position. Then the scene is rendered.
How could I represent this kind of an object in an Entity System? Would the camera be an entity or simply a component? A combination (see my answer)?
Another issues that is bothering me is implementing a Particle Emitter. For what exactly I mean by that, you can check out my video of it: http://youtu.be/BObargIMQsE. The problem I have with this is, again, what should be what. I'm pretty sure that particles themselves shouldn't be entities, as I want to support 10k+ of them, and creating that much entities would be a heavy blow on my performance, I believe. Or maybe not? Depends on the implementation, but anyone with experience: please, do answer.
The last bit I wan't to talk about, which is also bugging me the most, is how input should be handled. In my current version of the engine, there is a class called Input
. It's a handler that subscribes to browser's events, such as keypresses, and mouse position changes, and also it maintains an internal state. Then, the player class has a react()
method, which accepts an input object as an argument. The advantage of this is that the input object could be serialized into JSON and then shared over the network, allowing for smooth multiplayer simulations. But how does this translate into an Entity System?
© Game Development or respective owner