Example #1
0
        public List <CollisionTile> GetCollidingTiles(Rectangle rectangle, TileInfo.SideType direction)
        {
            TileUnit firstPrimary = Units.GameToTile(rectangle.Side(TileInfo.OppositeSide(direction)));
            TileUnit lastPrimary  = Units.GameToTile(rectangle.Side(direction));
            int      primaryIncr  = TileInfo.IsMax(direction) ? 1 : -1;

            bool     horizontal = TileInfo.Horizontal(direction);
            TileUnit sMin       = Units.GameToTile(horizontal ? rectangle.Top : rectangle.Left);
            TileUnit sMid       = Units.GameToTile(horizontal ? rectangle.Center.Y : rectangle.Center.X);
            TileUnit sMax       = Units.GameToTile(horizontal ? rectangle.Bottom : rectangle.Right);

            bool                 sPositive      = sMid - sMin < sMax - sMid;
            int                  secondaryIncr  = sPositive ? 1 : -1;
            TileUnit             firstSecondary = sPositive ? sMin : sMax;
            TileUnit             lastSecondary  = sPositive ? sMin : sMax;
            List <CollisionTile> collisionTiles = new List <CollisionTile>();

            for (TileUnit primary = firstPrimary; primary != lastPrimary + primaryIncr; primary = Convert.ToUInt32(Convert.ToInt32(primary) + primaryIncr))
            {
                for (TileUnit secondary = firstSecondary; secondary != lastSecondary + secondaryIncr; secondary = Convert.ToUInt32(Convert.ToInt32(secondary) + secondaryIncr))
                {
                    TileUnit row = !horizontal ? primary : secondary;
                    TileUnit col = horizontal ? primary : secondary;
                    collisionTiles.Add(new CollisionTile(row, col, tiles[Convert.ToInt32(row)][Convert.ToInt32(col)].tileType));
                }
            }
            return(collisionTiles);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="side">The side of the tile that is being collided with</param>
        /// <param name="perpendicularPosition">The position on the tile on the opposite axis of side</param>
        /// <param name="leadingPosition">Position of the leading edge of the colliding entity</param>
        /// <param name="shouldTestSlopes">whether slopes should be considered for collision</param>
        /// <returns>
        /// isColliding is true if there was a collision
        /// Returns the position of the collision on the same axis of side
        /// </returns>
        public TestCollisionInfo TestCollision(TileInfo.SideType side, GameUnit perpendicularPosition,
                                               GameUnit leadingPosition, bool shouldTestSlopes)
        {
            TestCollisionInfo info = new TestCollisionInfo(false, leadingPosition);

            if (tileType[(int)TileInfo.TileFlag.Wall])
            {
                info.isColliding = true;
                if (side == TileInfo.SideType.TopSide)
                {
                    info.position = Units.TileToGame(row);
                }
                else if (side == TileInfo.SideType.BottomSide)
                {
                    info.position = Units.TileToGame(row + 1);
                }
                else if (side == TileInfo.SideType.LeftSide)
                {
                    info.position = Units.TileToGame(col);
                }
                else
                {
                    info.position = Units.TileToGame(col + 1);
                }
            }
            else if (shouldTestSlopes && tileType[(int)TileInfo.TileFlag.Slope] &&
                     !tileType[(int)TileInfo.SlopeFlagFromSide(side)])
            {
                GameUnit row                = Units.TileToGame(this.row);
                GameUnit col                = Units.TileToGame(this.col);
                float    slope              = GetSlope(tileType);
                GameUnit offset             = GetOffset(tileType);
                GameUnit calculatedPosition = TileInfo.Vertical(side) ?
                                              slope * (perpendicularPosition - col) + offset + row :
                                              (perpendicularPosition - row - offset) / slope + col;

                bool isColliding = TileInfo.IsMax(side) ?
                                   leadingPosition <= calculatedPosition :
                                   leadingPosition >= calculatedPosition;

                info.isColliding = isColliding;
                info.position    = calculatedPosition;
            }
            return(info);
        }