Example #1
0
    public Transform Push(FigureData figureData)
    {
        GameObject figure = new GameObject("Figure");

        figure.transform.SetParent(figuresParent);
        figure.AddComponent <FigurePolygon>();
        figure.AddComponent <FigureDataComponent>();
        figure.AddComponent <Rigidbody2D>();
        figure.AddComponent <PolygonCollider2D>();
        figure.AddComponent <MeshFilter>();
        figure.AddComponent <MeshRenderer>();

        FigurePolygon polygon = figure.GetComponent <FigurePolygon>();

        polygon.SetPoints(figureData.Points, borders.Left.transform.localScale.z);

        FigureDataComponent figureDataStorage = figure.GetComponent <FigureDataComponent>();

        figureDataStorage.Data = figureData;

        Rigidbody2D rigidbody2D = figure.GetComponent <Rigidbody2D>();

        rigidbody2D.gravityScale = kGravityScale;

        PolygonCollider2D collider = figure.GetComponent <PolygonCollider2D>();

        collider.points = polygon.GetPoints();

        Mesh       mesh       = polygon.GetMesh();
        MeshFilter meshFilter = figure.GetComponent <MeshFilter>();

        meshFilter.mesh = mesh;

        MeshRenderer renderer = figure.GetComponent <MeshRenderer>();

        renderer.material.color = figureData.Color;

        SetPosition(figure.transform);

        // Check if the figure will collide with other figures at the start position. If so, we can't place it
        Collider2D[] results      = new Collider2D[100];
        int          numColliders = rigidbody2D.OverlapCollider(new ContactFilter2D(), results);

        if (numColliders > 0)
        {
            for (int i = 0; i < numColliders; i++)
            {
                if (results[i].transform != borders.Top)
                {
                    Object.Destroy(figure.gameObject);
                    return(null);
                }
            }
        }

        return(figure.transform);
    }
Example #2
0
    public Transform Push(FigureData figureData)
    {
        GameObject figure = new GameObject("Next figure");

        figure.AddComponent <FigurePolygon>();
        figure.AddComponent <FigureDataComponent>();
        figure.AddComponent <MeshFilter>();
        figure.AddComponent <MeshRenderer>();
        figure.AddComponent <FigureRotatorY>();

        FigurePolygon polygon = figure.GetComponent <FigurePolygon>();

        polygon.SetPoints(figureData.Points, nextFigureArea.Left.transform.localScale.z);

        FigureDataComponent figureDataStorage = figure.GetComponent <FigureDataComponent>();

        figureDataStorage.Data = figureData;

        Mesh       mesh       = polygon.GetMesh();
        MeshFilter meshFilter = figure.GetComponent <MeshFilter>();

        meshFilter.mesh = mesh;

        MeshRenderer renderer = figure.GetComponent <MeshRenderer>();

        renderer.material.color = figureData.Color;

        SetPosition(figure);

        FigureRotatorY rotator = figure.GetComponent <FigureRotatorY>();
        Vector2        size    = polygon.CalculateSize();
        Vector3        center  = new Vector3(size.x / 2, size.y / 2, 0);

        rotator.SetCenter(figure.transform.position + center);

        return(figure.transform);
    }