Forum C# Applying effect to player and enemies

Applying effect to player and enemies

Postby wgfunstormcontest » January 12th, 2013, 12:23 pm

Some spells do things like set the target on fire (damage over time), or slow them down.

I'm finding I have to create each method twice - once in the player class and once in the enemy class.

Ideally I want 1 character class which has those methods, and then inherit player and enemy from it so they both get it.

But since player has to extend BasePlayer and c# doesn't have multiple inheritance, I can't see a way to do this.

Thoughts?
wgfunstormcontest
 
Posts: 55
Joined: September 25th, 2012, 8:26 pm

Re: Applying effect to player and enemies

Postby AndyKerrison » January 12th, 2013, 4:21 pm

Player doesn't have to extend BasePlayer directly, you can have classes in between which should give you enough flexibility.

You could have something like

BasePlayer <- Character <- Player/Enemy

Although in your position I would give the Player an 'Avatar' object. The playerIO player's in-game character is just one of their properties.

So BasePlayer <- Player
and
Character <- Avatar / Enemy (to inherit common damage functions etc)

Then you can have
Player.Avatar.AddSpellDamage() or GameState.Enemy[n].AddSpellDamage()
AndyKerrison
 
Posts: 14
Joined: November 29th, 2012, 9:24 am

Re: Applying effect to player and enemies

Postby wgfunstormcontest » January 12th, 2013, 4:25 pm

I forgot to specify - enemies are computer-controlled NPCs... not actual players. So I don't think the first suggestion would work because I don't want the enemies extending character extending baseplayer.

But your 2nd suggestion looks like a good idea, I'll give it a go. Cheers!
wgfunstormcontest
 
Posts: 55
Joined: September 25th, 2012, 8:26 pm

Re: Applying effect to player and enemies

Postby wgfunstormcontest » January 13th, 2013, 9:07 am

I tried the 2nd suggestion and am struggling.

Now I have:
- Avatar class with things I want to be shared (eg int health, dealdamage function)
- Player has an avatar class as a property
- Enemy has an avatar class as a property

But since player & enemy don't extend the same class, all my code now looks like:

Code: Select all
if (target is Player)
{
    (target as Player).avatar.dealDamage();
}
else if (target is Enemy)
{
    (target as Enemy).avatar.dealDamage();
}
else if (target is Destructible)
{
    (target as Destructible).avatar.dealDamage();
}


Note: Player & Enemy & Destructible use interface Entity so that I can pass all 3 to a function that accepts an Entity as a parameter.

So it's better than it was before because I don't have to duplicate the dealDamage function, but I would like to just be able to do target.avatar.dealDamage()!
wgfunstormcontest
 
Posts: 55
Joined: September 25th, 2012, 8:26 pm

Re: Applying effect to player and enemies

Postby AndyKerrison » January 13th, 2013, 9:46 am

How about
Code: Select all
((Entity) target).avatar.dealdamage()
Last edited by AndyKerrison on January 13th, 2013, 12:27 pm, edited 1 time in total.
AndyKerrison
 
Posts: 14
Joined: November 29th, 2012, 9:24 am

Re: Applying effect to player and enemies

Postby wgfunstormcontest » January 13th, 2013, 10:39 am

If I put dealDamage in my Entity interface, doesn't that then mean I have to go back to copy-pasting the dealDamage function into the Player Enemy and Destructible classes?

Here's my current structure.

> interface Entity

> class Player : BasePlayer, Entity
has property Avatar

> class Enemy : Entity
has property Avatar

> class Destructible : Entity
has property Avatar

> class Avatar
has method dealDamage
wgfunstormcontest
 
Posts: 55
Joined: September 25th, 2012, 8:26 pm

Re: Applying effect to player and enemies

Postby AndyKerrison » January 13th, 2013, 12:32 pm

corrected typo, avatar was missed out.

IEntity declares that both Enemy & Player implement an 'Avatar' property. Avatar has the damage method in. Cast your target to IEntity, and deal damage to the Avatar that it must contain.
AndyKerrison
 
Posts: 14
Joined: November 29th, 2012, 9:24 am

Re: Applying effect to player and enemies

Postby wgfunstormcontest » January 13th, 2013, 12:50 pm

I like that idea, I actually tried something like that, but Entity is an interface, and I don't think you can give interfaces properties. At least it gave me an error when I tried.

Sorry for being a little thick... I'm still very new to C#. How would you go about declaring an Avatar property in Entity?
wgfunstormcontest
 
Posts: 55
Joined: September 25th, 2012, 8:26 pm

Re: Applying effect to player and enemies

Postby AndyKerrison » January 13th, 2013, 1:10 pm

Code: Select all
    public interface IEntity
    {
        Avatar Avatar {get;set;}
    }
AndyKerrison
 
Posts: 14
Joined: November 29th, 2012, 9:24 am


Return to C#



cron