Beispiel #1
0
        public void Insert(Rectangle rectangle, ICollisionEntity collisionEntity)
        {
            //check if the rectangle is in the bounds of this spatial grid - if not do not even bother adding it
            if (rectangle.X > _gridRectangle.X + _gridRectangle.Width || rectangle.Y > _gridRectangle.Y + _gridRectangle.Height)
            {
                return;
            }
            if (rectangle.X < _gridRectangle.X || rectangle.Y < _gridRectangle.Y)
            {
                return;
            }

            Vector2 topLeft     = new Vector2(rectangle.X, rectangle.Y);
            Vector2 topRight    = new Vector2(rectangle.X + rectangle.Width, rectangle.Y);
            Vector2 bottomLeft  = new Vector2(rectangle.X, rectangle.Y + _height);
            Vector2 bottomRight = new Vector2(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height);

            //GridPositions
            //top left
            var xTopLeft = Math.Floor((topLeft.X - (_gridRectangle.X)) / _size);
            var yTopLeft = Math.Floor((topLeft.Y - (_gridRectangle.Y)) / _size);

            //top right
            var xTopRight = Math.Floor((topRight.X - (_gridRectangle.X)) / _size);
            var yTopRight = Math.Floor((topRight.Y - (_gridRectangle.Y)) / _size);


            //bottom left
            var xBottomLeft = Math.Floor((bottomLeft.X - (_gridRectangle.X)) / _size);
            var yBottomLeft = Math.Floor((bottomLeft.Y - (_gridRectangle.Y)) / _size);

            //bottom right
            var xBottomRight = Math.Floor((bottomRight.X - (_gridRectangle.X)) / _size);
            var yBottomRight = Math.Floor((bottomRight.Y - (_gridRectangle.Y)) / _size);

            for (double x = xTopLeft; x <= xTopRight; x++)
            {
                for (double y = yTopLeft; y <= yTopRight; y++)
                {
                    var key = $"{x}_{y}";
                    if (_grid.ContainsKey(key) && !_grid[key].Contains(collisionEntity))
                    {
                        _grid[key].Add(collisionEntity);
                    }
                }
            }

            for (double x = xBottomLeft; x <= xBottomRight; x++)
            {
                for (double y = yBottomLeft; y <= yBottomRight; y++)
                {
                    var key = $"{x}_{y}";

                    if (_grid.ContainsKey(key) && !_grid[key].Contains(collisionEntity))
                    {
                        _grid[key].Add(collisionEntity);
                    }
                }
            }
        }
        public void UpdateCollision(Rectangle playerRec, Rectangle tileRectangle, int xOffset, int yOffset, ICollisionEntity transform)
        {
            if (collisionhelper.CollisionBottom(playerRec, tileRectangle))
            {
                playerRec.Y         = tileRectangle.Y - playerRec.Height;
                transform.Velocity  = new Vector2(transform.Velocity.X, 0f);
                transform.HasJumped = false;
            }

            if (collisionhelper.CollisionRight(playerRec, tileRectangle))
            {
                transform.Position = new Vector2(tileRectangle.X - playerRec.Width - 2, transform.Position.Y);
            }

            if (collisionhelper.CollisionLeft(playerRec, tileRectangle))
            {
                transform.Position = new Vector2(tileRectangle.X + playerRec.Width + 2, transform.Position.Y);
            }

            if (collisionhelper.CollisionTop(playerRec, tileRectangle))
            {
                transform.Velocity = new Vector2(transform.Velocity.X, 7f);
            }


            // Trap hero inside window borders
            if (transform.Position.X < 0)
            {
                transform.Position = new Vector2(0, transform.Position.Y);
            }

            if (transform.Position.X > xOffset - playerRec.Width)
            {
                transform.Position = new Vector2(xOffset - playerRec.Width, transform.Position.Y);
            }

            if (transform.Position.Y < 0)
            {
                transform.Velocity = new Vector2(transform.Velocity.X, 7.5f);
            }

            if (transform.Position.Y > yOffset - playerRec.Height)
            {
                transform.Position = new Vector2(transform.Position.X, yOffset - playerRec.Height);
            }
        }