What is the prefered way or spot using Artemis in monogame or xna to add a mouse click rectangle

349 views Asked by At

Using Artemis, Visual Studio, C# and Monogame.

Just starting to get a grasp on Artemis, but looking fo the proper place to add a clickable rectangle/area to an entity, there will be multiple entities on the screen.

Basic simple idea, i have small square sprites randomly showing and moving in the 2D play area. I need to be able to click on them and keeping it simple, delete the square.

In Artemis you have Components, Entities, Systems.

I know i'll be adding this rectangle area to the Texture2D square, guessing it should be its own component.

Trying to figure out

  1. Get the rectangle the size of the square, and stay with the square when it moves.
  2. How in some system, detect that this square was clicked or touched.

UPDATE

In my DrawableGameComponent entity. DrawPosition is a vector2 and set in the Main Game routine. It is the location of my square. I use that and texture size to calculate the size and location of my rectangle.

AreItemsIntersecting function will take the Mouse position when the screen is clicked, then i used that to create a little rect, and then checked if the 2 intersect. If they do, then the object was clicked.

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;
    }
1

There are 1 answers

2
dan On BEST ANSWER

I would create a BoundingBox component. It will expose a Bounds property of type Rectangle.

With this component in place you can create a KillEntityOnClickSystem to handle the removal of clicked entities. You'll just need to check whether the mouse is inside the Bounds of the entity BoundingBox when the mouse button is clicked.

Hope this helps!