Player.attack(Enemy), how should I do it?

If I want to use a function to modify Enemy.HP, but have it written as above, how would I do that? I want to keep Enemy.HP private because I feel like that's the 'right' way to do it, but the only way I can come up to work that out is doing it backwards (i.e Enemy.takeDMG(Player.dmg)-type thing).

The attack function itself would just subtract the Player.dmg int from Enemy.HP. What's the easiest way to accomplish this?
Well, your "backwards idea" is the right one. Just have a function takeDamage() that reduces the HP.
Last edited on
Yep, agreed. I would have a TakeDamage(int amount) function in my enemy class, which deducts the amount passed in from the private HP variable.
Yes. Make a TakeDamage() function. If the player receiving the damage has defense bonuses, you can calculate them inside this function, making the thing more transparent.
I always liked working on programs like this when I was learning, this is a great chance to work on your inheritance/polymorphism knowledge. Meaning something like TakeDamage seems like it would be good for players as well, having their own implementation (or same) as an Enemy object. Sounds like the workings for a base class interface.
Great point. If you have any knowledge of inheritance, now would be a good time to think about using it. A game object class can be a great superclass for players/enemies/static objects as they'll all probably contain similar Draw() and Update() functions.

Couple that with a collection class and drawing objects becomes as simple as a loop with a single line in it. Nice stuff.
Last edited on
Topic archived. No new replies allowed.