The green to cyan to red to green is the temperature. Currently, the threshold for the barrels is set to 1500 fake degrees, so once it reaches 1500, it starts burning. When a barrel’s temperature is close to the threshold (about 75%), it starts radiating its heat to the nearby barrels, thus starting the chain reaction.
Hello and welcome back for the next iteration in this ECS adventure. You’ll learn about how I chose to make components, tied to the same entity, talk together. I believe this is the coolest part.
Hah. That’s it.
And now, let’s dive into a…
Let’s imagine we have a game where there are WOODEN CRATES, and WOODEN DOORS. That means we have two entities, the Crate entity, and the Door entity. Also, wood can break, and wood can burn. These are two components.
It lends itself pretty well to our system. Let’s see what are our components’ outstanding traits:
- isBurnt - bool
- isBurning - bool
- burnPoints - number (like health points, removed by burning)
This is totally BAREBONES, we could add a lot more properties and methods for the sake of making this even more interesting, but this is a bit out of the scope of this tutorial, and in time you’ll be able to do it yourself.
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.
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, this wraps up the first Entity Component System post. I hope it made sense to you, and don’t hesitate to comment!