示例#1
0
    private void SetPosition(Transform figure)
    {
        // Take the coordinates of the left, right and top walls and put the figure regarding them

        Debug.Assert(figure);

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

        Debug.Assert(polygon);

        float leftWallCenterX  = borders.Left.position.x;
        float rightWallCenterX = borders.Right.position.x;
        float leftWallX        = leftWallCenterX - borders.Left.localScale.x / 2;
        float leftWallWidth    = borders.Left.localScale.x;
        float rightWallX       = rightWallCenterX - borders.Right.localScale.x / 2;

        float areaX     = leftWallX + leftWallWidth;
        float areaWidth = rightWallX - areaX;

        Debug.Assert(areaWidth > 0);
        Vector2 polygonSize = polygon.CalculateSize();

        float figureX = areaX + (areaWidth - polygonSize.x) / 2;

        float topWallCenterY = borders.Top.position.y;
        float topWallY       = topWallCenterY - borders.Top.localScale.y / 2;

        float figureY = topWallY - polygonSize.y;

        figure.position = new Vector3(figureX, figureY, 0);
    }
示例#2
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);
    }
示例#3
0
    void Update()
    {
        if (Time.realtimeSinceStartup - lastRotateTime < actionInterval)
        {
            return;
        }

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

        if (polygon)
        {
            gameObject.transform.RotateAround(center, Vector3.up, 2.0f);
        }

        lastRotateTime = Time.realtimeSinceStartup;
    }
示例#4
0
    private void SetPosition(GameObject figure)
    {
        // Take the coordinates of the next figure's area and put the figure to the center

        Debug.Assert(figure);

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

        Debug.Assert(polygon);
        Vector2 polygonSize = polygon.CalculateSize();

        float polygonCenterX = nextFigureArea.Top.position.x;
        float polygonCenterY = nextFigureArea.Left.position.y;

        float figureX = polygonCenterX - polygonSize.x / 2;
        float figureY = polygonCenterY - polygonSize.y / 2;

        Transform figureT = figure.GetComponent <Transform>();

        figureT.position = new Vector3(figureX, figureY, 0);
    }
示例#5
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);
    }
示例#6
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);
    }