Suppose I have a SquareObject
class, which implements IDrawable
, an interface which contains the method void Draw()
. I want to separate drawing logic itself from the game core engine.
My main idea is to create a static class which is responsible to dispatch actions to the graphic engine.
public static class DrawDispatcher<T>
{
private static Action<T> DrawAction = new Action<T>((ObjectToDraw)=>{});
public static void SetDrawAction(Action<T> action)
{
DrawAction = action;
}
public static void Dispatch(this T Obj)
{
DrawAction(Obj);
}
}
public static class Extensions
{
public static void DispatchDraw<T>(this object Obj)
{
DrawDispatcher<T>.DispatchDraw((T)Obj);
}
}
Then, on the core side:
public class SquareObject: GameObject, IDrawable
{
#region Interface
public void Draw()
{
this.DispatchDraw<SquareObject>();
}
#endregion
}
And on the graphics side:
public static class SquareRender{
//stuff here
public static void Initialize(){
DrawDispatcher<SquareObject>.SetDrawAction((Square)=>{//my square rendering logic});
}
}
Do this "pattern" follow best practices?
And a plus, I could easily change the render scheme of each object by changing the DispatchDraw parameter, as in:
public class SuperSquareObject: GameObject, IDrawable
{
#region Interface
public void Draw()
{
this.DispatchDraw<SquareObject>();
}
#endregion
}
public class RedSquareObject: GameObject, IDrawable
{
#region Interface
public void Draw()
{
this.DispatchDraw<RedSquareObject>();
}
#endregion
}
RedSquareObject
would have its own render method, but SuperSquareObject
would render as a normal SquareObject
I'm just asking because i do not want to reinvent the wheel, and there may be a design pattern similar (and better) to this that I may be not acknowledged of.
Thanks in advance!