Summarised Problem:
I have an Animation class that I want to be able to treat as a Texture2D field when need be. I do not want to inherit from the class or its parents. I'd rather not create a parent class of both the animation class and a 'container' for a Texture2D, though that may be the only way to achieve what I want.
Code:
public class Animation : IComponent { public Texture2D texture => frames[currentFrame]; public Texture2D[] frames; public byte currentFrame { get; protected set; } /// <summary> /// Frame time in milliseconds; /// </summary> public ushort interval; public bool looping; public bool animate; protected ushort progress; /// <param name="interval">Time until frame is succeeded.</param> /// <param name="loop">Whether to loop frames.</param> /// <param name="start">Whether to start cycling through frames.</param> public Animation(Texture2D[] frames, ushort interval, bool loop, bool start) { this.frames = frames; currentFrame = 0; this.interval = interval; looping = loop; animate = start; } public void Update(ushort milliseconds) { if (looping) currentFrame = (byte)((progress += milliseconds) % interval % frames.Length); else currentFrame = (byte)((progress += milliseconds) % interval); progress %= interval; } } It's an extremely simple class that is subject to change. Its purpose is to stand in for a Texture2D for external handling of spreadsheets and whatnot. It is going to be used in an ECS but probably not as a component as I have it now.
So, for instance, I'd like to be able to put it as a stand in for a Texture2D in draw calls (can be done with the 'texture' property it has) and also be kept in the same field and be cast to treat it as the animation class again.
If anyone thinks there's a much superior implementation of this please let me know.
If there's a way I could do something like this that would be perfect:
spriteBatch.Draw(Anim as Texture2D, Vector2.Zero, Color.White);