Пример #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);
    }
Пример #2
0
    private void CollisionFigureFigure(Transform figure1, Transform figure2)
    {
        FigureDataComponent figureDataStorage1 = figure1.GetComponent <FigureDataComponent>();
        FigureDataComponent figureDataStorage2 = figure2.GetComponent <FigureDataComponent>();

        if (!figureDataStorage1 || !figureDataStorage2)
        {
            Debug.Assert(figureDataStorage1 && figureDataStorage2);
            return;
        }

        if (figureDataStorage1.Data.Color == figureDataStorage2.Data.Color)
        {
            CollisionFigureFigureSameColor(figure1, figure2);
        }

        if (figure1 == currentFigure || figure2 == currentFigure)
        {
            NextFigure();
        }
    }
Пример #3
0
    private void NextFigure()
    {
        FigureData newFigureData;

        if (nextFigure == null)
        {
            newFigureData = figureCreator.GenerateFigure();
        }
        else
        {
            // take figure data from the next figure
            FigureDataComponent figureDataStorage = nextFigure.GetComponent <FigureDataComponent>();
            newFigureData = figureDataStorage.Data;
        }

        currentFigure = figurePusher.Push(newFigureData); // returns null if can't push figure

        if (currentFigure)
        {
            FigurePolygon polygon = currentFigure.GetComponent <FigurePolygon>();
            polygon.AddListener(this);
        }
        else
        {
            // can't push figure, so the game is over
            GameOver();
        }

        if (nextFigure)
        {
            Destroy(nextFigure.gameObject); // remove previous figure from next figure area
            nextFigure = null;
        }

        // generate next figure for displaying
        FigureData nextFigureData = figureCreator.GenerateFigure();

        nextFigure = nextFigurePusher.Push(nextFigureData);
    }
Пример #4
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);
    }