implementing entity component system in current engine...

Started by
2 comments, last by SteveHatcher 9 years, 1 month ago

Hi All,

Thanks to a lot of help from this forum I have gotten my 'game' to this stage:

Its time to add some enemies and make a proper 'level'. This is where I have been stuck for a while now, because up until here it has been pretty straight forward. There seems to be only really one way to have a general game loop, and to draw some sprites to the screen (I am using directX TK).

So I have been reading about this so called entity component system and it sounds like the way to go from here. What I have absolutely no clue about is how to implement it into my current engine.

In order to test adding ECS to my engine I would like to add what I consider to be a simple example of it: Adding three different enemy types. Each type will appear on screen using a timer, each enemy will have a different color, and a different weapon. My ship too, will have different weapons, selectable with 1-3. Id like my ship and the enemy ships to have health, and weapons to deal different damage.

As it stands now all sprites are an instance of my sprite class, the particle system is its own class particleSys, collision detection is done using a rectangle class, where methods in the rectangle class generate the bounding boxes and then test them for intersection. The map draws from its MapRenderer::draw() method .Apart from that, my game does the usual

update() handles input(), then each sprite updates() itself

collisions()

render()

I am extremely confused from here:

What makes up the Entity, components and systems in this example? Are the Entitiys class PlayerShip{}, and class EnemyShip{}. Would their components be something like:

struct shipComponent

{

int life; //Everyone's health

int projectile; //Damage each projectile does

}

Do I keep my current code and then add this ECS system in, or does it require a rewrite of the game engine from scratch? Or can I mix and match sections?

I would greatly appreciate a point in the right direction of how to implement the so called ECS. I am the kind of person who learns a lot more from reading source code too, so any examples of it in place in a small scale game would be interesting to me too. I feel I have read every post on gamedev and S.E on this but really wont fully grasp whats going on until I get down and dirty in some code.

Thanks for your time.

Advertisement

Here is a really good link that explains how an ECS works: http://www.randygaul.net/2013/05/20/component-based-engine-design/

Let me use your game as an example of how things would work in an ECS.

The most basic Object is an Entity (Generally an abstract class that has abstract methods such as Update(), Draw(), Destroy(), Initialize() and an ID[int])
Basically, the ID is what differentiates one entity from another. If you believe that most entities that you create have shared logic (Position Perhaps) then you can go ahead and add that logic at this level. It would be available to every entity you generate in the game.

Components is where the differentiation occurs. The components that an Entity has is what will make it behave in a Unique way.

So for your 2 enemy spaceships you would have something like:

Heavy Enemy Spaceship

- Entity (Base Class and container for you components)

- Components:

1. Enemy Component (Basic enemy logic that all enemies share)

2. Ship Component (Holds health, projectile damage, and Shield strength) (Load up different values for change in Enemies)

3. Heavy Enemy Component (Any logic that is only specific to this specific enemy, In this case it is that every 5 seconds he throws a meteor at you)

Kamikaze Enemy Spaceship

- Entity (Base Class and container for you components)

- Components:

1. Enemy Component (Basic enemy logic that all enemies share)

2. Ship Component (Holds health, projectile damage, and Shield strength) (Load up different values for change in Enemies)

3. Kamikaze Enemy Component (When appears on screen, waits 3 seconds and then attacks you trying to explode)

See how Components are reused and you don't have to create a new class for each specific spaceship.

With regards of updating you current code. Generally an ECS does have knowledge of the Rendering Pipeline (Your DirectX code) but it doesn't have to be that way.

You can use the Components to update all the position, scaling, speed of you spaceships and have the Renderer use that knowledge to render each particular Entity.

Hope this helps!

Take a look at this topic. We cover many of these same questions there.

Good luck!

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Thanks for your replies guys. I will need a bit of time to follow these links and digest this information.

@Jonnie This is the kind of example I needed. And the link your posted does have a very clear description on some of the higher level info I was not understanding.

@BeerNutts That guy in that post sounds exactly what I was struggling with. I will have a read of that topic.

I will post back once I (hopefully) get something in place.

This topic is closed to new replies.

Advertisement