Exemple #1
0
        public IList <Tile> Intersect(FloatRectangle bounds)
        {
            IList <Tile> intersection = new List <Tile>();

            // calculate possible tile bounds to improve performance.
            int colStart  = Math.Max(0, (int)bounds.Left / Tile.Width - 1);
            int colItems  = (int)bounds.Width / Tile.Width + 2;
            int colLength = Math.Min(Tiles.GetLength(0), colStart + colItems + 1);

            int rowStart  = Math.Max(0, (int)bounds.Top / Tile.Height - 1);
            int rowItems  = (int)bounds.Height / Tile.Height + 2;
            int rowLength = Math.Min(Tiles.GetLength(1), rowStart + rowItems + 1);

            for (int x = colStart; x < colLength; x++)
            {
                for (int y = rowStart; y < rowLength; y++)
                {
                    Tile tile = Tiles[x, y];

                    var intersects = bounds.Intersects(tile.Bounds);
                    if (intersects)
                    {
                        intersection.Add(tile);
                    }
                }
            }

            return(intersection);
        }
Exemple #2
0
        public List <Collision> GetCollisions(List <CollisionStats> collisionTiles)
        {
            List <Collision> collisions = new List <Collision>();

            foreach (CollisionStats first in collisionTiles)
            {
                foreach (CollisionStats second in collisionTiles)
                {
                    if (!first.entity.Equals(second.entity) && IsTargetInRange(first.entity, second.entity))
                    {
                        foreach (TimeFrame firstFrame in first.frames)
                        {
                            foreach (TimeFrame secondFrame in second.frames)
                            {
                                if (FloatRectangle.Intersects(firstFrame.entityRectangle, secondFrame.entityRectangle))
                                {
                                    if (firstFrame.time == secondFrame.time)
                                    {
                                        Collision c = new Collision();
                                        c.entity1      = first.entity;
                                        c.entity2      = second.entity;
                                        c.intersection = FloatRectangle.Intersect(firstFrame.entityRectangle, secondFrame.entityRectangle);
                                        c.frame        = firstFrame;

                                        if (!ContainsCollision(collisions, c))
                                        {
                                            collisions.Add(c);
                                            c.entity1.WasHit = true;
                                            c.entity2.WasHit = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(collisions);
        }