Good design for damage and death response?

Started by
2 comments, last by L. Spiro 5 years, 4 months ago

Hello,

The current questionable design:
I have lava that kills the player when it hits it.
The logic to play the death explosion is in the object the player hits.
When I did this I was influenced by what I read about Sims object design.
By placing the logic in the object it's easy to add more objects with different logic.

The downside is that responses are distributed in all the objects.

The upside is that I don't have to write code for handling damage events and responses, and mapping damage types to death animations etc.
I do feel this would be inappropriate for things like projectiles, and enemy attacks.
However because the object it hits is Static I figured it'd be ok.
 

The Ask:
So, what are your thoughts and what would you consider a good design?

Advertisement

I would separate logic from the object. So basically you have three "parts":

The object (you only have one of these, call it the "template". It contains the geometry and root data for the object)
The object's state (stores information about the current animation, transform etc, determines the number of instances of an object you have in the scene, contains a pointer to the base object and a logic handler)
The logic handler (this could be a callback, a script or whatever - doesn't really matter)

To initialize the game state you load all object data from disk, create states per instance of an object (eg for each monster), attach a logic handler to each instance and then, to update:

(eg per frame)
for(auto& o : objInstances)  o.handler->OnTick(o)

(eg when an object receives damage)
for(auto& o : objInstances)  o.handler->OnDamage(o)

 

As mentioned, objects that can be damaged should inherit from “CDamageable” and implement virtual bool CDamageable::OnDamage( Amt, Type );.

Presumably anything that can be damaged can die, so you would also implement CDamageable::OnDie( Reason );.

Your object can select how it wants to die based on the reason it is dying (too much damage, flattened, hit lava, etc.) and then each object can be damaged and die in their own unique ways.


L. Spiro


PS: The parameters I mentioned can change depending on your needs.  I omitted sending the object that deals damage or death as I usually don’t see the reason (the scene manager will tell both objects what to do) but if you have a reason then add it.

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement