Example #1
0
    public static PointsResult getPoints(GameObject source, int extraPoints = 0)
    {
        //get transform information
        Vector3 origScale = source.transform.localScale;

        source.transform.localScale = Vector3.one;
        Quaternion origRotation = source.transform.localRotation;

        source.transform.localRotation = Quaternion.identity;

        //get collider information
        PolygonCollider2D sourcePolyCollider = source.GetComponent <PolygonCollider2D> ();
        BoxCollider2D     sourceBoxCollider  = source.GetComponent <BoxCollider2D> ();
        Collider2D        sourceCollider     = null;
        List <Point>      points             = new List <Point> ();
        List <Vector2>    borderPoints       = new List <Vector2> ();

        if (sourcePolyCollider != null)
        {
            borderPoints   = getPoints(sourcePolyCollider);
            sourceCollider = sourcePolyCollider;
        }
        else if (sourceBoxCollider != null)
        {
            borderPoints   = getPoints(sourceBoxCollider);
            sourceCollider = sourceBoxCollider;
        }

        Rect rect = getRect(source);

        float ratio       = Mathf.Sqrt(3) / 2;
        float n           = Mathf.Sqrt(Mathf.Max(extraPoints / ratio, 100));
        int   rows        = Mathf.RoundToInt(n);
        float rowHeight   = rect.height / rows;
        int   columns     = Mathf.RoundToInt(n * ratio);
        float columnWidth = rect.width / columns;

        float x0 = rect.width / -2 + rect.center.x + columnWidth / 2;
        float y0 = rect.height / -2 + rect.center.y + rowHeight / 2;

        float   x, y, xOffset;
        float   randomVariation = 0.4f;
        Vector2 point;

        for (int i = 0; i < rows; i++)
        {
            y       = y0 + i * rowHeight;
            xOffset = i % 2 == 0 ? -columnWidth / 4 : columnWidth / 4;
            for (int j = 0; j < columns; j++)
            {
                x     = x0 + j * columnWidth + xOffset;
                point = new Vector2(
                    Random.Range(randomVariation * columnWidth / -2 + x, randomVariation * columnWidth / 2 + x),
                    Random.Range(randomVariation * rowHeight / -2 + y, randomVariation * rowHeight / 2 + y)
                    );
                if (sourceCollider != null && sourceCollider.OverlapPoint(point))
                {
                    points.Add(new Point(point, i, j));
                }
            }
        }

        //reset transform information
        source.transform.localScale    = origScale;
        source.transform.localRotation = origRotation;

        PointsResult result = new PointsResult(points, borderPoints, rows, columns);

        result.GenerateMap(rect);
        return(result);
    }
Example #2
0
    public static List <GameObject> GenerateCavePieces(GameObject source, PointsResult pointsResult, int subshatterSteps = 0, Material mat = null)
    {
        List <GameObject> pieces = new List <GameObject> ();

        if (mat == null)
        {
            mat = createFragmentMaterial(source);
        }

        //get transform information
        Vector3 origScale = source.transform.localScale;

        source.transform.localScale = Vector3.one;
        Quaternion origRotation = source.transform.localRotation;

        source.transform.localRotation = Quaternion.identity;

        //get rigidbody information
        Vector2 origVelocity = source.GetComponent <Rigidbody2D> ().velocity;

        Rect rect = getRect(source);

        List <Vector2> points = new List <Vector2> ();

        foreach (Point point in pointsResult.points)
        {
            points.Add(point.point);
        }
        List <Vector2> borderPoints = pointsResult.borderPoints;

        Voronoi voronoi = new Delaunay.Voronoi(points, null, rect);
        List <List <Vector2> > clippedRegions = new List <List <Vector2> > ();

        foreach (Point point in pointsResult.points)
        {
            bool  isWall           = pointsResult.isWall(point);
            float magnitude        = point.point.magnitude;
            bool  isCell           = magnitude > 100;
            bool  isInStartingArea = Mathf.Abs(Mathf.Abs(point.point.x) - Mathf.Abs(point.point.y)) < 10;
            if (!isInStartingArea && (isWall || isCell))
            {
                List <Vector2> region = voronoi.Region(point.point);
                clippedRegions = ClipperHelper.clip(borderPoints, region);
                foreach (List <Vector2> clippedRegion in clippedRegions)
                {
                    pieces.Add(generateVoronoiPiece(source, clippedRegion, origVelocity, origScale, origRotation, mat));
                }
            }
        }

        List <GameObject> morePieces = new List <GameObject> ();

        if (subshatterSteps > 0)
        {
            subshatterSteps--;
            foreach (GameObject piece in pieces)
            {
                morePieces.AddRange(SpriteExploder.GenerateVoronoiPieces(piece, 3, subshatterSteps));
                GameObject.DestroyImmediate(piece);
            }
        }
        else
        {
            morePieces = pieces;
        }

        //reset transform information
        source.transform.localScale    = origScale;
        source.transform.localRotation = origRotation;

        Resources.UnloadUnusedAssets();

        return(morePieces);
    }