Jump to content
  • Advertisement
mothacluckinjoe

C# OOP in game programming

This topic is 501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everybody,

So I have a situation where the player object is attacking the enemy object. My question is, in terms of best practices, should the player hit the enemy, and the enemy detect if it was hit and then deduct it's own health, or should the player deduct the health of the enemy? I know that in OOP every class should handle it's own behavior, but I'm not sure what the correct answer of this question is. Also, could anyone point me to good articles on how to take advantage of OOP in terms of game programming? I want to write the best code I can.

Thanks!

Share this post


Link to post
Share on other sites
Advertisement

Well, the other option which I tend to prefer is none of the above.  I try and encapsulate the concept of doing damage as a third object in the group.  The intention is to keep the details of how damage is calculated out of the entities so all the rules are in one place instead of split between attack and receive damage functions.  It also means that weapons can generate these objects such that you can have different rules for different weapons, or even multiple damage objects being generated by the weapon.  Additionally, this allows a better data driven design since you write a few damage type objects, parameterize them and then just fill in the details for each new weapon.

The utility of this of course depends on your type of game.  If you only have 5 weapons and they are generally just remove damage till zero, there is no reason to do this.  If you intend to have 10+ different weapons and many variations, that's when the separation becomes well worth the more indirect approach.

Share this post


Link to post
Share on other sites

However you do it in your system at the end, there are some fundamental thoughts to do for setting up such a system.

Who registers that any colision has been happen. This is important because using different weapons may result in different hit boxes, swords may trigger but a bow does not rather than its ammo.

How does a producer spread the information about a hit. Do you use directly connected objects so maybe a component on the sword or the ammo, or do they send an event to some kind of gameplay controller. I have seen both and it depends on the implementation what I would suggest, there exists good and bad implemenations of both, component driven and event driven systems.

Where will your damage been calculated. Again, is it a component or event target that will calculate the damage. The one or other way work but what happens when damage stacks for multiple buffs, e.g. for a burning sword with base 13 hit damage and +5 fire damage and how will your enemy react on that damage (for example an undead mummy could suffer incineration where a stone golem ignores the fire damage and reduces sword by half)

Share this post


Link to post
Share on other sites

Well, I would think that something like combat is not a property of either the player or the enemy, so I would not make it a member function of either.

If your programming environment does not allow things like free functions, then perhaps create a combat object that provides a function taking the two objects involved and arbitrates the results.  Such an object can also be handy for other state tracking, like driving animations or sound effects.

Share this post


Link to post
Share on other sites

Thanks for your input guys! I'm not experienced in OOP really, just getting started with it, and I want to make sure I'm really programming efficiently and taking advantage of the system. These responses helped a lot! @Bregma @Shaarigan@Hiwas

Is there any reference you guys could point me to in order to see some examples of gameplay programming and have a good picture of what to go for?

Edited by mothacluckinjoe

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!