First of all, I have no idea if the title even reflects my question. I apologize and kindly request to change it if you have a better idea.
Now, the problem I have is mainly a design problem. I am not sure if what I want is a good practice but it's d*mn convenient.
I have an Interface called IMovement.
Two classes implement IMovement: VelocityMovement, SplineMovement.
I also have a class called Sprite. A Sprite has an IMovement.
Finally, I have two sublcasses of Sprite: Player and Enemy.
I KNOW that Player will ALWAYS have VelocityMovement and
I KNOW that Enemy will ALWAYS have SplineMovement.
Is it wrong (either by design or practical) to have, aside from the superclasses reference to IMovement, an attribute to the VelocityMovement or SplineMovement from Player or Enemy respectively?
In other words:
Interface IMovement {
public Vector2 getPosition(int time);
}
class VeloctiyMovement implements IMovement {
public Vector2 velocity;
public Vector2 getPosition(int time) { ... }
}
class SplineMovement implements IMovement {
public Spline spline;
public Vector2 getPosition(int time) { ... }
}
class Sprite {
IMovement movement;
}
class Player extends Sprite {
public VelocityMovement velocityMovement;
public Player() {
velocityMovement = new VelocityMovement();
movement = velocityMovement;
}
}
class Enemy extends Sprite {
public SplineMovement splineMovement;
public Enemy() {
splineMovement = new SplineMovement();
movement = splineMovement();
}
}
The reason I want to have the reference in the subclass is convenience. Half the times I need the velocity field from Player, and half the other times I don't care. This happens in a game where the method gets called approximately 100 times per second.
The reason I am pointing out the 100 times is because I also am concerned if casting is expensive.
Example, instead of:
// When I need the velocity
Vector2 velocity = ((VelocityMovement)player.movement).velocity;
...
// When I don't care
Vector2 position = player.movement.GetPosition(time);
...
I can now do this:
Vector2 velocity = player.VelocityMovement.velocity;
...
Vector2 position = player.movement.GetPosition(time);
...
So my question is: Should Player have a VelocityMovement field and an Enemy have a SplineMovement? Or is this a bad design?
I would like your thoughts.
UPDATE
I now remembered why I chose to have a field IMovement instead of having Sprite implement IMovement (as JacobM correctly suggests).
It is because in some cases, Enemy could have SplineMovement and in others VelocityMovement.
Although I, incorrectly, said "I KNOW that Enemy will ALWAYS have SplineMovement", I should have stated that, I know an Enemy has VelocityMovement in places where I gather all enemies with VelocityMovements. I hope you understand me (otherwise I feel like an idiot).
For example:
List<Enemy> enemies = GetAllVelocityMovementEnemies();
I do this because somewhere in my game logic, I want to affect only the group of enemies that have VelocityMovements.
So, although JacobM's answer is perfectly valid as a design, this is not for my case. Otherwise I would have to subclass Enemy with EnemyVelocityMovement and EnemySplineMovement and so on and so forth.
So, sorry for the misunderstanding, I have decided that Brian Agnew's answer is the best one for my design.
Oh, and yes, waring about casting is indeed premature optimization. ;)