Entities in my game are nothing more than a collection of components, tied together with an entityID. I have (near) data-only components, and systems that work on the data.
One of these components is
HitPoints. It is one of the few components that has a little bit more than just data: it houses a
Dying event that triggers from the
HitPointsSystem when the hitpoints go below 0. One of the event listeners is my user interface, while an entity is selected, it listens to the current selection's
Dying event. On-death, the entity is dis-selected. Having entities selected draws little circles around the selection. It works great... Right up until I have an entity without
HitPoints that can still die.
I have a
Missile in my game which does not have
HitPoints, but it can be selected, and it can die. When a
Missile is selected, my user interface recognizes that it does not have
HitPoints, and therefore also does not have a
Dying event, which later causes the draw loop to attempt to draw a "selected" and dead Missile, causing a crash.
You might guess that you could just add
HitPoints to the
Missile and everything would be fine, but my AI targeting system looks for enemy
HitPoints because those are the only things it can damage. I don't want missiles to be targeted, and I don't want to add a special rule either.
So... if all entities can die (or be deleted), where do I place the
Dying event? Should I be making a new component? Either a component for all things that can die (almost everything), or a component for AI targets? Or is there a better place to put an event like this?