使用 Artemis、Visual Studio、C# 和 Monogame。
刚开始了解 Artemis,但在寻找合适的位置向实体添加可点击的矩形/区域时,屏幕上将出现多个实体。
基本简单的想法,我在 2D 游戏区域中随机显示和移动小方形精灵。我需要能够点击它们并保持简单,删除方块。
在 Artemis 中,您拥有组件、实体、系统。
我知道我会将这个矩形区域添加到 Texture2D 正方形中,猜测它应该是它自己的组件。
试图弄清楚
- 得到正方形大小的矩形,移动时与正方形保持一致。
- 如何在某些系统中检测到该方块被点击或触摸。
更新
在我的 DrawableGameComponent 实体中。DrawPosition 是一个 vector2 并在主游戏例程中设置。这是我的广场的位置。我使用它和纹理大小来计算我的矩形的大小和位置。
AreItemsIntersecting 函数将在单击屏幕时占据鼠标位置,然后我用它来创建一个小矩形,然后检查 2 是否相交。如果他们这样做,那么该对象被单击。
public override void Update(GameTime gameTime)
{
var bx = DrawPosition.X;
var by = DrawPosition.Y;
var w = _texture.Bounds.Width;
var h = _texture.Bounds.Height;
_bounds = new Rectangle((int)bx, (int)by, w+1, h+1);
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
if (_texture != null)
{
_spriteBatch.Begin();
_spriteBatch.Draw(_texture, DrawPosition, Color.White);
_spriteBatch.Draw(_texture, _bounds, Color.Transparent);
_spriteBatch.End();
base.Draw(gameTime);
}
public bool AreItemsIntersecting(int x, int y)
{
var vect = new Rectangle(x, y, 1, 1);
if (vect.Intersects(_bounds))
{
return true;
}
return false;
}