I feel like I should only have to have one instance variable reference an object. But in the code below, I have 2 instance variables "_character" and "_witch" referencing the same object. If I add a more specialized witch class, I'd have to add a 3rd instance variable.
Is this typically what people do in this situation? Or is there a way to achieve this using only one reference? Also, I really don't want to have to cast anything (unless that really is the best practice in this scenario).
On top of the WitchAnimationController extending AnimationController, the WitchState extends CharacterState.
The base class:
public class AnimationController
{
protected CharacterState _character;
public AnimationController( CharacterState character )
{
_character = character;
}
public void UpdateAnimations()
{
/* call on some CharacterState functions... */
}
}
The child class:
public class WitchAnimationController : AnimationController
{
protected WitchState _witch; //inherits from CharacterState
public AnimationController( WitchState witch ) : base( witch )
{
_witch = witch;
}
public void UpdateAnimations()
{
base.UpdateAnimations();
/* call on some WitchState functions... */
}
}
If you don't need witch-specific calls, you can just ditch the _witch
field and use the _character
field - although I'd personally make it a private field with a protected property.
If you need character-specific members, you should consider making the animation controller generic:
public class AnimationController<T> where T : CharacterState
{
protected T _character;
public AnimationController(T character)
{
_character = character;
}
public void UpdateAnimations()
{
/* call on some CharacterState functions... */
}
}
Then:
public class WitchAnimationController : AnimationController<WitchState>
{
public WitchAnimationController(WitchState witch) : base(witch)
{}
public void UpdateAnimations()
{
base.UpdateAnimations();
_character.SomeMethodOnWitchState();
}
}
The _character.SomeMethodOnWitchState()
call will be valid within WitchAnimationController
because _character
is effectively of type WitchState
.
See more on this question at Stackoverflow