I am trying to create a system that can easily modify objects on the fly. For example, lets say I have an Entity2D
that inherits from Entity
. Entity2D
has a Position
property. I also have a class called ModifyPosition
that inherits from Modifier
.
Here is the code:
public class Entity
{
/// <summary>Applies the modifier to this entity.</summary>
/// <param name="modifier">The modifier to apply.</param>
public void ApplyModifier(Modifier modifier)
{
modifier.Apply(this);
}
}
/// <summary>Modifies an entities position</summary>
public class ModifyPosition : Modifier
{
/// <summary>/// Initializes a new instance of the <see cref="ChangePosition"/>
class./// </summary>
/// <param name="position">The position.</param>
public ChangePosition(Vector2 position)
{
this.Position = position;
this.IsModifyingChildren = false;
}
/// <summary>Gets the position.</summary>
/// <value>The position.</value>
public Vector2 Position { get; private set; }
/// <summary>Applies this change to the specified entity.</summary>
/// <param name="entity">The entity.</param>
internal override void Apply(Entity entity)
{
((Entity2D)entity).X += this.Position.X;
((Entity2D)entity).Y += this.Position.Y;
}
}
If you are calling this multiple times per second, I would think that the casting would slow it down. Is there another way to go about this, without having to cast?