I have made an interface called IHero i implement in my hero.as3 class.
the hero class is written so it can be inheritted in a movieclip class to handle movement etc etc. But somehow i can’t figure out how to code this with a good practice.
Maybe i am in the wrong direction.
I want to have a movieclip subclass, which will be a hero for instance.
Should i just implement the IHero in the hero class with the following methods, or is this to overkill? – I guess I am looking for an answer upon what should be in an interface and what should not. Here is the interface.
package com.interfaces
{
public interface IHero
{
//movement
function MoveLeft():void;
function MoveRight():void;
function MoveUp():void;
function MoveDown():void;
//in battle
function DoDamage(isCasting:Boolean):void;
function DoHeal():void;
function Flee():void;
function TakeDamage():void;
function IsAlive():Boolean;
function CheckDeath():void;
function Die():void;
}
}
I think you are on a right track, whether its the right one or the wrong one is always subjective. But if you do want to go down this road, I suggest you read this article by Mick West. Its a few years old, but its still very applicable.
I think you really have two distinct interfaces, but probably more
Then….
Then inject those into your subclass
MovieClipfor yourHero.Here you are using the
Heroclass as both Component Manager and Render component, which goes slightly against what West is talking about in the article, but I digress. But the idea is that your Manager (theHero) becomes more or less an orchestrator, proxying calls back through to which ever component is applicable; calling methods on_moverandfighterto do your actual work.There are several advantages using an approach like this, and some disadvantages. First, its more complex to set up; it requires you to really like about components and what each logical chunck of code is going to do. But on the other hand, it decouples your implementations from each other, makes it more testable, and reuseable. You can also swap out components at any time (compile-time or run-time for that matter), which gives you some flexability when creating new Entities.
But anyway, its just a suggestion of a slightly different paradigm that you seem to be flirting with. Maybe you’ll get some mileage out of it. But definitely give the article a read, if you haven’t already.
Also look into (like check out the API) for the Unity Engine which has a similar aggregation vs inheritance model where interfaces are key to abstraction.