我有一个名为 Component 的基类。我也有 2 个接口 I2DComponent 和 I3DComponent。我目前正在研究 I2DComponent。
实施:
/// <summary>
/// Represtions a 2D object. These objects will be drawn after 3D objects,
/// and will have modifiers automatically provided in the editor.
/// </summary>
public interface I2DComponent
{
/// <summary>
/// Gets or sets the rectangle.
/// </summary>
/// <value>The rectangle.</value>
Rectangle Rectangle { get; set; }
/// <summary>
/// Gets the local position on the game screen.
/// </summary>
/// <value>The local position on the game screen.</value>
/// <remarks>The rectangle position minus the parent screen position.</remarks>
Rectangle LocalPosition { get; }
/// <summary>
/// Gets or sets the scale.
/// </summary>
/// <value>The scale.</value>
float Scale { get; set; }
}
所以问题是我需要访问全局位置或矩形来检查鼠标是否在它上面。
if (component.Rectangle.Contains(mouse.Rectangle))
{
do somthing.
}
但是当我只是访问移动它时,我只想访问本地位置,这样如果我在位置 50、50 有一个游戏屏幕,那么我可以将本地位置设置为 0、0,它将位于顶部父游戏屏幕的左上角。我想真正的问题是我不想通过说
component.Rectangle = new Rectangle(component.Rectangle.X + 5, component.Rectangle.Y + 5, component.Rectangle.Width, component.Rectangle.Height);
这或多或少是一个访问器设计问题,我想让它看起来正确,但我在这样做时遇到了麻烦。
更新:
如果我改变它,使 I2D 组件只有 Bounds 和 scale,然后在 GameScreen 中有一个函数来获取组件的当前“全局”位置,该怎么办。
更新:
这些 2D 对象都是在 3D 空间中绘制为 hud 或 gui 对象的所有对象。只是澄清一下。
更新:
我现在正在考虑递归地做它,但我不确定我将如何去做。