Exemple #1
0
        public HitInfo CollideWithOther(float x, float y, float otherX, float otherY, Entity other)
        {
            var hit = HitInfo.None;

            QueryTiles(x, y, otherX, otherY, other.Width, other.Height, (tile, cellX, cellY, cellWidth, cellHeight) =>
            {
                if (tile.PixelMask != null)
                {
                    if (tile.PixelMask.CollideWithOther(cellX, cellY, otherX, otherY, other))
                    {
                        hit = HitInfo.CreateHit(other, tile, tile.PixelMask);
                        return(true);
                    }
                }
                else if (Mathf.IntersectRect(cellX, cellY, cellWidth, cellHeight, otherX, otherY, other.Width, other.Height))
                {
                    hit = HitInfo.CreateHit(other, tile);
                    return(true);
                }

                return(false);
            });

            return(hit);
        }
Exemple #2
0
        public HitInfo CollideRect(float x, float y, float rectX, float rectY, int rectWidth, int rectHeight)
        {
            var hit = HitInfo.None;

            QueryTiles(x, y, rectX, rectY, rectWidth, rectHeight, (tile, cellX, cellY, cellWidth, cellHeight) =>
            {
                if (tile.PixelMask != null)
                {
                    if (tile.PixelMask.CollideRect(cellX, cellY, rectX, rectY, rectWidth, rectHeight))
                    {
                        hit = HitInfo.CreateHit(tile, tile.PixelMask);
                        return(true);
                    }
                }
                else if (Mathf.IntersectRect(cellX, cellY, cellWidth, cellHeight, rectX, rectY, rectWidth, rectHeight))
                {
                    hit = HitInfo.CreateHit(tile);
                    return(true);
                }

                return(false);
            });

            return(hit);
        }
Exemple #3
0
        public HitInfo CollideWithOtherAt(float entityX, float entityY, Entity other)
        {
            entityX -= OriginX;
            entityY -= OriginY;

            if (!Mathf.IntersectRect(entityX, entityY, Width, Height, other.Left, other.Top, other.Width, other.Height))
            {
                return(HitInfo.None);
            }

            if (Collider != null)
            {
                return(Collider.CollideWithOther(entityX, entityY, other.Left, other.Top, other));
            }

            if (other.Collider != null)
            {
                var hit = other.Collider.CollideWithOther(other.Left, other.Top, entityX, entityY, this);
                if (hit)
                {
                    hit.Other = other;
                }
                return(hit);
            }

            return(HitInfo.CreateHit(other));
        }
Exemple #4
0
        public HitInfo CollideRect(float x, float y, int width, int height)
        {
            if (!Mathf.IntersectRect(Left, Top, Width, Height, x, y, width, height))
            {
                return(HitInfo.None);
            }

            if (Collider != null)
            {
                return(Collider.CollideRect(Left, Top, x, y, width, height));
            }

            return(HitInfo.CreateHit());
        }
Exemple #5
0
        public HitInfo CollideWithOther(float x, float y, float otherX, float otherY, Entity other)
        {
            var hit = HitInfo.None;

            if (other.Collider is PixelMask)
            {
                // PixelMask to PixelMask collision.
                if (CollidePixelMaskToPixelMask(x, y, otherX, otherY, (PixelMask)other.Collider))
                {
                    hit = HitInfo.CreateHit(other);
                }
            }
            else if (other.Collider is GridCollider)
            {
                // PixelMask to GridCollider collision.
                var gridCollider = (GridCollider)other.Collider;
                gridCollider.QueryTiles(otherX, otherY, x, y, WidthPx, HeightPx, (tile, cellX, cellY, cellWidth, cellHeight) =>
                {
                    //	var tileX = otherX + tile.X * gridCollider.CellWidth;
                    //var tileY = otherY + tile.Y * gridCollider.CellHeight;

                    if (tile.PixelMask != null)
                    {
                        if (CollidePixelMaskToPixelMask(x, y, cellX, cellY, tile.PixelMask))
                        {
                            hit = HitInfo.CreateHit(other, tile);
                            return(true);
                        }
                    }
                    else if (CollideRect(x, y, cellX, cellY, cellWidth, cellHeight))
                    {
                        hit = HitInfo.CreateHit(other, tile);
                        return(true);
                    }

                    return(false);
                });
            }
            else if (CollideRect(x, y, otherX, otherY, other.Width, other.Height))
            {
                // Other collision: Use the other entity hitbox.
                hit = HitInfo.CreateHit(other);
            }

            return(hit);
        }
Exemple #6
0
        public HitInfo CollideRect(float x, float y, float rectX, float rectY, int rectWidth, int rectHeight)
        {
            var startX = (int)(rectX - x);

            if (startX < 0)
            {
                startX = 0;
            }

            var startY = (int)(rectY - y);

            if (startY < 0)
            {
                startY = 0;
            }

            var endX = (int)(rectX + rectWidth - x);

            if (endX > WidthPx)
            {
                endX = WidthPx;
            }

            var endY = (int)(rectY + rectHeight - y);

            if (endY > HeightPx)
            {
                endY = HeightPx;
            }

            for (var ix = startX; ix < endX; ix++)
            {
                for (var iy = startY; iy < endY; iy++)
                {
                    if (mask[ix, iy])
                    {
                        return(HitInfo.CreateHit());
                    }
                }
            }

            return(HitInfo.None);
        }