In my last post I presented my declarative XNA animation library called ZoomEngine.Animation. This time I want to step back and look at how all of the pieces fit together, and show how you can extend the library in your own game.
Lets start with the class diagram:
ClockManager is the object that ‘owns’ all active clocks, and is responsible for updating when it’s Update method is called (typically once per game update). This object acts as a scope for active clocks so that its easy to pause all the ‘level’ animations while keeping the animations running in the pause menu.
Clock is the base class for basically everything else. It’s called ‘Clock’ because it defines how its internal time progresses with respect to your GameTime. Properties of this type dictate when the animation starts and how long it runs. Methods on this type allow you to start, stop and pause the clock/animation.
Timer is a simple type of clock that fires an event when it completes. This is an easy way to schedule game events to happen in the future.
Animation<T> is the base type for all true animations. It adds to the notion of time (provided by Clock), the ability to compute & apply a value of type T. It is an abstract class because it doesn’t actually know how to compute the value, it simply provides an abstract method GetCurrentValue() for this purpose.
FromToAnimation<T> is the base class of all animation the define how to compute a current value using a From & To property. All of it’s derived types are there to say exactly how to interpolate between From and To based on the current progress. The Interpolate property is a delegate that lets you override the default behavior of linear interpolation without having to derive a whole new type. This can be used to add some randomness to your animation, for example.
ProgressTransforms is a static class that provides several stock ProgressTransforms which can be used with an Animation<T>.
As you can see, this library is pretty simple when it comes down to it. Some ways you might extend it would be to add a new type that derives from FromToAnimation<T>. Another way you might extend this library would be to create a new ‘kind’ of animation such as KeyFrameAnimation<T>. We’ll dive into the details of how to do this in another post.