Forum C# Targeting player & enemies of different class types

Targeting player & enemies of different class types

Postby wgfunstormcontest » November 29th, 2012, 6:37 pm

I want players to be able to use their abilities on other players and enemies. In code, I want to do something like
Code: Select all
player.useAbility(ability, target)


But I don't know how to do this since target can either be of type Player or Enemy. I'm hoping to avoid duplicating the useAbility method into a useAbilityOnFriendly and useAbilityOnEnemy that are identical except for the target type.

Any thoughts on what would be a good (clean) way to do this?
wgfunstormcontest
 
Posts: 55
Joined: September 25th, 2012, 8:26 pm

Re: Targeting player & enemies of different class types

Postby dreamora » November 29th, 2012, 9:29 pm

sounds like a job for interfaces and simple inheritance so something like

Interface IActor

class Player : IActor

class Enemy : IActor
dreamora
 
Posts: 225
Joined: March 2nd, 2012, 9:58 am


Return to C#