Entity Component System, entity relations

Started by
5 comments, last by aardvarklord 3 years, 5 months ago

Hi,

Trying to wrap my head around basic Entity Component System design principles and how to model a simple weapon / combat system within ECS. Consider a simple case containing the following entities: Human, Goblin, NormalSword, MagicSword where the two different swords have unique specialized implementations. Humans can carry one of the sword types if he has found any, and choose to attack a Goblin if encountered.

Some questions:
1. How would you represent the weapon being wielded by the Human? By a “Wielded” component on the Human containing the ID of the NormalSword or MagicSword entity he is carrying?
2. How would you handle combat? My first thought was to add a “Combat” component to the Human when combat starts and then have separate systems for each weapon type and have those weapon systems traverse all entities that currently have a “Combat” component AND a “weapon of the type associated with the system”. But I'm stuck on “weapon of the type associated with the system”… what would that really mean? The weapons are entities, not components so that would not work…. the Humans carrying weapons would all have a “Wielded” component containing a reference to the weapon which would make it really ugly to iterate through and does not seem like the ECS way to do it?

Any feedback on the questions or suggestions for completely different ways to do it would be appreciated

Cheers

None

Advertisement

aardvarklord said:
The weapons are entities, not components so that would not work

Why do the weapons as wielded need to be entities? If it's to allow the weapon to be dropped on the ground (or otherwise be separated physically from the player), then couldn't you model this with a “weapon” component that switches to a “dropped weapon entity” when dropped, and is on the player the rest of the time?

@Oberon_Command Interesting, thanks, I'll definitely consider such a conversion between entity and component. Not quite obvious how it would work though, I imagine my items that could be picked up would be entities with one or more components so a NormalSword entity may for example have only a MeleeAttackComponent while a MagicSword may have a MeleeAttackComponent and a MagicAttackBoosterComponent, so converting those item entities to components on a Human entity may be a bit tricky.

Another idea I have is to add a CombatComponent (containing info about who to attack) to the weapon entity when combat starts. That way, a system associated with for example a MeleeAttackComponent could iterate over all entities that have a MeleeAttackComponent AND a CombatComponent and execute the attack functionality.

None

aardvarklord said:
@Oberon_Command Interesting, thanks, I'll definitely consider such a conversion between entity and component. Not quite obvious how it would work though,

To be clear, what I'm suggesting is that:

  • the “weapon” is its own component representing the weapon and its capabilities
  • initially this lives on the player entity
  • but when the player entity drops the weapon, it is removed from the player and a new entity representing the weapon on the ground is created, with a weapon component representing the weapon

Either by moving/copying the component between entities, or by having a separate component that represents a description of a weapon as it can be used when a player picks it up.

I would not necessarily make 2 different components for the 2 swords. The magic sword could also have some physical attack component, so it would make sense to just have one weapon component containing physical attack and magical attack. Then have one system calculate the compound damage depending on the weapons, def, hit rate, … all influencing it (possibly having other systems calculate intermediate values like how much complete physical attack or defence a character got with all equipped items storing these into other components, as there may be attack boosting rings or whatever).

Thanks both, good input. This may very well work for my game, need to think it through some more.

(What I meant I thought could be tricky was for the generic case where some items are composed from multiple components and the Human could carry multiple such items. If I understand correctly part of the ECS philosophy is to when suitable have multiple small reusable components. For example the MagicAttackBoosterComponent on the MagicSword in my example above could also be attached to a MagicRangedWeapon entity in order to give it the same magic capabilities as the MagicSword.)

None

This topic is closed to new replies.

Advertisement