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?
public delegate T ValueModifierCallback(T baseValue, T currentValue)
– Mike Strobel Aug 10 '10 at 21:12+25
to some property) while others might be multiplicative (i.e.+25%
to some property). The former would simply returncurrentValue + 25
while the latter might returncurrentValue + (0.25 * baseValue)
. – Mike Strobel Aug 10 '10 at 21:37That makes a lot more sense thank you very much.
Oh another requirement was that I wanted to be able to create custom ones in an editor and be able to serialize them. Were you able to do that with yours?
– Chris Watts Aug 11 '10 at 00:56$currentValue + (0.25 * $baseValue)
). The language is basically just the subset of all C# expressions that are representable as LINQ expression trees, but with a few differences (system parameters prefixed with '$', user parameters prefixed with '#', expression-scoped namespace imports, etc.). The expressions are parsed at runtime and rewritten to be observable/reactive when possible (runtime value changes are automatically propagated). – Mike Strobel Aug 11 '10 at 03:01Lately, I've been using Xaml as a persistence format whenever possible. They've really improved the Xaml API in .NET 4, and it generally beats the hell out of the standard XML serialization mechanisms in .NET.
– Mike Strobel Aug 11 '10 at 04:51