I've got 2+ objects that come from different inheritance trees, but I'd like them to share a common set of code IMoveable
.
The IMoveable
interface is looking good, and I'm happy with where I've got with it:
public interface IMoveable
{
/// <summary>
/// The speed the object travells between start and end
/// </summary>
int Speed { get; set; }
/// <summary>
/// The current velocity of the object
/// </summary>
Vector2 Velocity { get; set; }
/// <summary>
/// How far the object has travelled
/// </summary>
int DistanceTravelled { get; set; }
/// <summary>
/// The map the object is traversing
/// </summary>
Map Map { get; set; }
/// <summary>
/// Where the object was when they started moving
/// </summary>
Rectangle StartRectangle { get; set; }
/// <summary>
/// Where the object is right now
/// </summary>
Rectangle CurrentRectangle { get; }
/// <summary>
/// Where the object will be after moving
/// </summary>
Rectangle EndRectangle { get; set; }
/// <summary>
/// What will happen if the object walks to the "EndRectangle"
/// </summary>
Map.CollisionResults CollisionResult { get; set; }
/// <summary>
/// What happens if the object triggers a battle
/// </summary>
Action OnBattle { get; set; }
/// <summary>
/// How the object determines their movement
/// </summary>
Action SetMovement { get; set; }
}
With that interface I have a method:
private static void Move(IMoveable moveableOjb)
{
moveableOjb.Speed = 4;
if (moveableOjb.DistanceTravelled > 0)
{
moveableOjb.DistanceTravelled += moveableOjb.Speed;
if (moveableOjb.DistanceTravelled > Map.TileWidth)
{
moveableOjb.DistanceTravelled = 0;
moveableOjb.Velocity = new Vector2();
}
else
{
return;
}
}
moveableOjb.SetMovement();
if (moveableOjb.Velocity != Vector2.Zero)
{
moveableOjb.StartRectangle = moveableOjb.CurrentRectangle;
moveableOjb.EndRectangle = new Rectangle(
moveableOjb.CurrentRectangle.X + ((int)moveableOjb.Velocity.X * 10),
moveableOjb.CurrentRectangle.Y + ((int)moveableOjb.Velocity.Y * 10),
moveableOjb.CurrentRectangle.Width,
moveableOjb.CurrentRectangle.Height);
moveableOjb.CollisionResult = moveableOjb.Map.GetValue(moveableOjb.EndRectangle);
switch (moveableOjb.CollisionResult)
{
case Map.CollisionResults.None:
break;
case Map.CollisionResults.Colliding:
moveableOjb.Velocity = new Vector2();
break;
case Map.CollisionResults.Battle:
moveableOjb.OnBattle();
moveableOjb.Velocity = new Vector2();
break;
case Map.CollisionResults.OffRight:
case Map.CollisionResults.OffLeft:
case Map.CollisionResults.OffTop:
case Map.CollisionResults.OffBottom:
moveableOjb.Speed = 0;
break;
default:
break;
}
}
if (moveableOjb.Velocity != Vector2.Zero)
moveableOjb.DistanceTravelled += moveableOjb.Speed;
}
The problem I'm facing is this code is just weird. I've gotten a static Move
method and I don't know where to put it, and I feel like I've gone about this the completely wrong way.
An alternative I have to this is rewrite my classes so instead of them coming from different inheritance, they are in the same tree. I could do that, but it will take some time to restructure.
I guess my main question is - am I going about this the wrong way, or am I close to following a coding practice that I can't quite figure out?
Example of implementation:
public class ClassA : Sprite, IMoveable
{
// interface implementation
public override Update(GameTime gameTime)
{
// Stuff
Move(this);
// More stuff
}
}
EDIT:
I've been informed that is C# 8 you can have default interface methods. I think that may be exactly what I need!