I have a basic core interface called IComponent.
public interface IComponent
{
void Initialize();
void Update();
void Draw(Color tint);
BoundingBox BoundingBox { get; }
bool Initialized { get; }
}
It is used by any class that will drawable (button, text, texture, …)
I want to develop a set of effects that can be applied to any IComponent, such as:
Shake(IComponent component, TimeSpan duration)
-> Causes the Component to vibrate for a given duration. It works by initially storing the center position of the passed component and offsetting it at each Update till duration ends.
Translate(IComponent component, Vector2 destination, TimeSpan timeToReach)
-> Causes the Component to move to the given destination after a certain time. It works by incrementally offsetting the component at each Update.
You can imagine more…
So assume I want a certain class (Texture : IComponent) to shake while also moving to a certain point. I thought of possibly using the decorator pattern like so:
Create a Texture => Wrap it in a Shake (5 sec duration) => Wrap it in a Translate (10 sec duration)
but there are some problems with that.
Primarily, The Shake wrapper would work fine alone with a static stationary texture, but combined with a Translate it would fail since it would keep returning the texture to its original position and it wont translate properly.
Secondly, while the translation will take 10 seconds to finish, the vibrating will take only 5 seconds, so afterwards I don’t know how to automatically remove the Shake wrapper from within the chain, and also finally remove the Translate when its done, leaving behind the original texture.
Thirdly, the decorator pattern hides any specific functions of the wrapped object, so after wrapping a Texture with a Shake, I wont be able to invoke the setPixelColor() of Texture, unless I also create a second direct reference to Texture before wrapping it.
Any suggestions of how to tackle such a challenge elegantly are welcome. Thanks.
Note: In practice I am likely to apply those effects to probably 2% of all created objects that are IComponent.
Maybe another approach along the following lines.
In your example both shake and translate are Animation effects. All animations need some form of duration and are applied to a component. Which could lead to the following first take:
Duration of an animation may seem like a good initial approach, however, when you need to combine animations, each animation needs to manipulate the BoundingRect of its encapsulated IComponent over the same period of time as other animations. In order to be able to stack animations like this, the draw methods for the animation effects need to be instructed to draw a specific frame of the animation.
An improved version of the IAnimationEffect interface would be:
Whichever class is responsible for drawing your IComponents (Lets call it
DrawingEnginefor now), is now also responsible for holding an internal List of all applicable animation effects. It also needs to have some sort of timeline and rendering logic in frames per second, so that calculations for a specific animation frame can be performed.As proposed above then your AnimationEffects classes do not have any responsibility for drawing IComponents, that remains the responsibility of the DrawingEngine.
What you would need to introduce directly before your DrawingLoop, is a loop for running all animation effects, all this would do is update the bounds of the objects that happen to have an animation effect associated with them. Then you carry on drawing the object as usual.
Suppose that you have something like this:
All that RunAnimationCalculations would do is loop through through your animationEffects collection and run Calculate passing in the current frame so that the bounds of the IComponent are updated, before the frame is subsequently drawn.
Good luck!