Example #1
0
        // based on tile type, perform logic to determine if a collision did occur with an intersecting tile or not
        private static bool HasCollidedWithMapTile(GameObject gameObject, MapTile mapTile, Direction direction)
        {
            switch (mapTile.TileType)
            {
            case TileType.PASSABLE:
                return(false);

            case TileType.NOT_PASSABLE:
                return(gameObject.Intersects(mapTile));

            case TileType.JUMP_THROUGH_PLATFORM:
                return(direction == Direction.DOWN && gameObject.Intersects(mapTile) &&
                       gameObject.GetScaledBoundsY2().Round() - 1 == mapTile.GetScaledBoundsY1().Round());

            default:
                return(false);
            }
        }
Example #2
0
        public static float GetAdjustedPositionAfterCollisionCheckY(GameObject gameObject, Map map, Direction direction)
        {
            int   numberOfTilesToCheck = Math.Max(gameObject.GetScaledBounds().Width / map.GetTileset().GetScaledSpriteWidth(), 1);
            float edgeBoundY           = direction == Direction.UP ? gameObject.GetScaledBounds().GetY1() : gameObject.GetScaledBounds().GetY2();
            Point tileIndex            = map.GetTileIndexByPosition(gameObject.GetScaledBounds().GetX1(), edgeBoundY);

            for (int j = -1; j <= numberOfTilesToCheck + 1; j++)
            {
                MapTile mapTile = map.GetMapTile(tileIndex.X.Round() + j, tileIndex.Y.Round());
                if (mapTile != null && HasCollidedWithMapTile(gameObject, mapTile, direction))
                {
                    if (direction == Direction.DOWN)
                    {
                        float boundsDifference = gameObject.GetScaledY2() - gameObject.GetScaledBoundsY2();
                        return(mapTile.GetScaledBoundsY1() - gameObject.GetScaledHeight() + boundsDifference);
                    }
                    else if (direction == Direction.UP)
                    {
                        float boundsDifference = gameObject.GetScaledBoundsY1() - gameObject.GetY();
                        return(mapTile.GetScaledBoundsY2() - boundsDifference);
                    }
                }
            }
            foreach (EnhancedMapTile enhancedMapTile in map.GetActiveEnhancedMapTiles())
            {
                if (HasCollidedWithMapTile(gameObject, enhancedMapTile, direction))
                {
                    if (direction == Direction.DOWN)
                    {
                        float boundsDifference = gameObject.GetScaledY2() - gameObject.GetScaledBoundsY2();
                        return(enhancedMapTile.GetScaledBoundsY1() - gameObject.GetScaledHeight() + boundsDifference);
                    }
                    else if (direction == Direction.UP)
                    {
                        float boundsDifference = gameObject.GetScaledBoundsY1() - gameObject.GetY();
                        return(enhancedMapTile.GetScaledBoundsY2() - boundsDifference);
                    }
                }
            }
            return(0);
        }