我正在 Monogame 中准备一个小游戏引擎,在那里我想要由 DrawableObject 和 ClickHandler 组成的类似 gameObject 的东西(即:
public class GameObject : DrawableObject, ClickHandler
问题是 - C# 不支持多重继承,我需要使用接口。我已经制作了 DrawableObject 和 ClickHandler 抽象类,因此它们可以已经实现了一些功能。
public abstract class ClickHandler
{
public class NullClick : ClickHandler
{
public override void Click(Point mousePos)
{
Debug.Print("Clicked on: " + mousePos + ". NullClickHandler assigned");
}
}
private readonly byte _handlerId;
public static readonly NullClick NullClickHandler = new NullClick();
private ClickHandler() {}
public ClickHandler(ref ClickMap clickMap)
{
_handlerId = clickMap.registerNewClickHandler(this);
}
public abstract void Click(Point mousePos);
void unregisterHandler(ref ClickMap clickMap)
{
clickMap.releaseHandler(_handlerId);
}
}
class DrawableObject
{
Texture2D texture;
public Rectangle position;
public DrawableObject()
{
position = Rectangle.Empty;
}
void Load(ref GraphicsDevice graphics)
{
using (var stream = TitleContainer.OpenStream("Content/placeholder.jpg"))
{
texture = Texture2D.FromStream(graphics, stream);
position.Width = texture.Width;
position.Height = texture.Height;
}
}
void Draw(){} //here is going to be some default implementation
}
有什么提示我可以重新设计它以实现它吗?我不想将整个实现移动到我将其作为接口派生的每个类。