This is Part 1. Here’s Part 2 of the series.
Entetey Compowhat?
So the other day I saw on invrse.co that there was interest in having an entity component system for JavaScript games, to which I self-fived myself and promptly tweeted how cool it was.
A couple of days earlier I had started looking into ECS, for a Phaser game I’m currently working on. I strongly suggest that you read invrse’s post up there, it’s super interesting and well written, and if you’re unfamiliar with ECS, I urge you to read it!
So hey, one little disclaimer before we move on: I am totally half-assing this. I’m pretty sure I’m miles away from the real way an ECS should be done, but bear with me, what’s coming is pretty cool!
Half-assing in all its glory
ALRIGHTY-O, let’s get started with a quick (half-assed) recap.
- Entities are your “objects” or “actors”, such as the player, an NPC, an orc, a sword, a crate, a textfield…
- Components are the additional properties and behaviors that you might want to add to your Entities. For example, a popular one would be a Physics component, so as to add gravity and collision to your entities.
We’re just adding behaviors and properties to an object.
With this information, let’s take a look at how components should behave:
- Components should be parameterizable (pretty sure that’s how it’s spelled)
- Components need to know on what they are applied (entity)
- Components need to be updated every frame (game loop)
- Components need to be added and removed at will (they’re optional)
- Components sometimes need to talk to each other (event, signal, or callback system)
Ok, ok, that’s cool. Let’s take a look at a basic component without any behavior at all. Like I said before, some kind of physics component is usually used during game development, so we’ll create the Body component.
[snippet id=”41″ title=”ECS – Body component example, step 1″ height=”0″ line_numbers=”true”]
As you can see, there are is a name property, and an update() method. These two things will make much more sense later, but for now let me explain quickly: the name property is used to identify which component we are dealing with. The update() method will be called by the Entity in its own update() method.
Now, the next step is to add the target, or simply put, the entity to which we are referring to, and the subject of the components behaviors.
[snippet id=”43″ title=”ECS – Body component example, step 2″ height=”0″ line_numbers=”true”]
Awesome. This is pretty much the basis for all our next components. You can copy paste and rename them to your liking. Personally, I like to give them adjectivized names, to quickly understand that they’re components, and what they do, for example Flammable, Droppable, Throwable, you get the idea…
Now here comes the meat for our Body component.
[snippet id=”26″ title=”ECS – Body component example, step 3″ height=”0″ line_numbers=”true”]
So we’ve set some default properties (self.physicsEngine), and we’re enabling the object in the physics world of your game.
Ladies and gentlemen, we’ve just created our first component, and it is absolutely simple. Don’t be fooled by the apparent number of lines, there are many lines of comments and whitespace.
FISRT!11!!
So we’ve created our first component. Now, how do we use it? Let’s imagine we have a Player class.
[snippet id=”58″ title=”ECS – Body component example, step 4″ height=”0″ line_numbers=”true”]
You need a this.components array. Storing the components you add to an entity makes them accessible, and you can then call the update method of ALL your components in easily:
[snippet id=”63″ title=”ECS – Body component example, step 5″ height=”0″ line_numbers=”true”]
Well?
Well, this wraps up the first Entity Component System post. I hope it made sense to you, and don’t hesitate to comment!
Now on to Part 2.