Ejemplo n.º 1
0
    /// <summary>
    /// Place building from matrix.
    /// </summary>
    private void PlaceMatrixBuildings(Building.CellPosition pos)
    {
        // If building is not valid destroy all prepared instances.
        foreach (GameObject obj in preparedForBuild)
        {
            if (!buildingCollider.GetComponent <BuilderCollider>().isValid)
            {
                Destroy(obj);
            }
            else
            {
                BuilderMatrixCell     matrixPos = buildingsMatrix.Find(x => x.gameObject == obj);
                Building.CellPosition cellPos   = new Building.CellPosition()
                {
                    row = Mathf.Abs(matrixPos.y) + pos.row,
                    col = Mathf.Abs(matrixPos.x) + pos.col
                };

                FinalizeBuilding(obj, cellPos);
            }
        }

        CleanActionState();
    }
Ejemplo n.º 2
0
    public void OnDrag(PointerEventData data)
    {
        if (data.button != PointerEventData.InputButton.Left)
        {
            return;
        }

        Vector3 currentPoint           = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector3 currentVectorDirection = currentPoint - mouseInitialPoint;

        if (vectorDirection == null || vectorDirection != currentVectorDirection)
        {
            vectorDirection = new Vector3(
                Mathf.Sign(currentVectorDirection.x),
                Mathf.Sign(currentVectorDirection.y),
                Mathf.Sign(currentVectorDirection.z)
                );

            Vector3 position = building.transform.localScale;
            position.Scale(vectorDirection);

            dragInitialPoint = building.transform.position - (position / 2);
        }

        float distX = Vector3.Distance(dragInitialPoint, new Vector3(currentPoint.x, dragInitialPoint.y, dragInitialPoint.z));
        float distY = Vector3.Distance(dragInitialPoint, new Vector3(dragInitialPoint.x, currentPoint.y, dragInitialPoint.z));

        Vector2 boxSize       = building.transform.localScale;
        var     currentMatrix = new Vector2(Mathf.Ceil(distX / boxSize.x), Mathf.Ceil(distY / boxSize.y));

        if (currentMatrix == initialMatrix)
        {
            return;
        }

        Vector2 deltaSize = currentMatrix - initialMatrix;

        deltaSize.Scale(boxSize);
        buildingCollider.transform.localScale += (Vector3)deltaSize;

        Vector2 step = new Vector2(currentMatrix.x * boxSize.x / 2, currentMatrix.y * boxSize.y / 2);

        step.Scale(vectorDirection);
        buildingCollider.transform.position = (Vector2)dragInitialPoint + step;

        var items = new List <BuilderMatrixCell>();

        bool status = buildingCollider.GetComponent <BuilderCollider>().isValid;

        for (int r = 0; r < currentMatrix.y; r++)
        {
            for (int c = 0; c < currentMatrix.x; c++)
            {
                int matrixX = c * (int)vectorDirection.x;
                int matrixY = r * (int)vectorDirection.y;

                BuilderMatrixCell existingBox = buildingsMatrix.Find((item) => item.x == matrixX && item.y == matrixY);

                if (existingBox == null)
                {
                    GameObject newObj = Instantiate(
                        buildingPrefab,
                        GetBoxPosition(matrixX, matrixY),
                        Quaternion.identity,
                        buildingsContainer.transform
                        );

                    newObj.GetComponent <Building>().SetValidView(status);

                    items.Add(new BuilderMatrixCell()
                    {
                        x          = matrixX,
                        y          = matrixY,
                        gameObject = newObj
                    });
                }
                else
                {
                    items.Add(existingBox);
                }
            }
        }

        foreach (BuilderMatrixCell obj in buildingsMatrix.Except(items))
        {
            Destroy(obj.gameObject);
        }

        buildingsMatrix = items;

        preparedForBuild = items.Select(x => x.gameObject).ToList();

        initialMatrix = currentMatrix;
    }