Ejemplo n.º 1
0
    // check line horizontally
    void checkVertically()
    {
        // Vertical
        for (int i = 1; i < GridCustom.width; i++)
        {
            // get start and end line
            Line line = new Line();
            for (int j = 0; j < GridCustom.height; j++)
            {
                // if it is on open field or tower
                if (GridCustom.cells[i, j].cellContent == CellContent.OPEN_FIELD || GridCustom.cells[i, j].cellContent == CellContent.TOWER)
                {
                    // if the bottom is not open field or tower then just continue, for artistry purpose
                    if (!(GridCustom.cells[i - 1, j].cellContent == CellContent.OPEN_FIELD || GridCustom.cells[i - 1, j].cellContent == CellContent.TOWER))
                    {
                        // if there is a start line then just end it there, looks too ugly
                        if (line.isStart)
                        {
                            line.endVector = GridCustom.getWorldSpace(i, j);
                            drawLine(line.startVector, line.endVector, new Color(0, 1, 0, 100f), lineParent.transform);
                            countLine++;
                            line.reset();
                        }
                        continue;
                    }

                    // if there is no start line
                    if (!line.isStart)
                    {
                        line.startVector = GridCustom.getWorldSpace(i, j);
                        line.endVector   = GridCustom.getWorldSpace(i, j);
                        line.isStart     = true;
                    }
                    else
                    {
                        line.endVector = GridCustom.getWorldSpace(i, j);
                    }
                }
                else // If it is not on the open field or tower
                {
                    if (line.isStart)
                    {
                        line.endVector = GridCustom.getWorldSpace(i, j);
                        drawLine(line.startVector, line.endVector, new Color(0, 1, 0, 100f), lineParent.transform);
                        countLine++;
                        line.reset();
                    }
                }
            }

            // if there is a start line but end of index
            if (line.isStart)
            {
                line.endVector = GridCustom.getWorldSpace(i, GridCustom.height - 1);
                drawLine(line.startVector, line.endVector, new Color(0, 1, 0, 100f), lineParent.transform);
                countLine++;
            }
        }
    }
Ejemplo n.º 2
0
    // create gizmos on start
    public void changeGridStatus()
    {
        int x, y;

        GridCustom.getXYFromPosition(this.transform.position, out x, out y);

        x += GridCustom.offsetX;
        y += GridCustom.offsetY;

        // Draw gismoz on tower
        GridCustom.cells[x, y].cellContent = CellContent.TOWER;
        Debug.DrawLine(GridCustom.getWorldSpace(x, y), GridCustom.getWorldSpace(x + 1, y), Color.black, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x, y), GridCustom.getWorldSpace(x, y + 1), Color.black, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x + 1, y), GridCustom.getWorldSpace(x + 1, y + 1), Color.black, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x, y + 1), GridCustom.getWorldSpace(x + 1, y + 1), Color.black, 100f, false);
    }
Ejemplo n.º 3
0
    // Start is called before the first frame update
    void Start()
    {
        Vector2 position = this.transform.position;

        // get x,y of the tower in the grid
        int x, y;

        GridCustom.getXYFromPosition(position, out x, out y);

        x += GridCustom.offsetX;
        y += GridCustom.offsetY;

        // blocks the inside of tower
        GridCustom.cells[x, y].cellContent = CellContent.GENERATOR;
        Debug.DrawLine(GridCustom.getWorldSpace(x, y), GridCustom.getWorldSpace(x + 1, y), Color.yellow, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x, y), GridCustom.getWorldSpace(x, y + 1), Color.yellow, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x + 1, y), GridCustom.getWorldSpace(x + 1, y + 1), Color.yellow, 100f, false);

        GridCustom.cells[x, y - 1].cellContent = CellContent.GENERATOR;
        Debug.DrawLine(GridCustom.getWorldSpace(x, y - 1), GridCustom.getWorldSpace(x, y), Color.yellow, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x, y - 1), GridCustom.getWorldSpace(x + 1, y - 1), Color.yellow, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x + 1, y - 1), GridCustom.getWorldSpace(x + 1, y), Color.yellow, 100f, false);

        Debug.DrawLine(GridCustom.getWorldSpace(x, y + 1), GridCustom.getWorldSpace(x + 1, y + 1), Color.yellow, 100f, false);

        // blocks behind of the generator
        for (int i = x + 1; i < GridCustom.width; i++)
        {
            for (int j = 0; j < GridCustom.height; j++)
            {
                GridCustom.cells[i, j].cellContent = CellContent.KOTO_TOWER;
                Debug.DrawLine(GridCustom.getWorldSpace(i, j), GridCustom.getWorldSpace(i + 1, j), Color.yellow, 100f, false);
                Debug.DrawLine(GridCustom.getWorldSpace(i, j), GridCustom.getWorldSpace(i, j + 1), Color.yellow, 100f, false);
                Debug.DrawLine(GridCustom.getWorldSpace(i + 1, j), GridCustom.getWorldSpace(i + 1, j + 1), Color.yellow, 100f, false);
                Debug.DrawLine(GridCustom.getWorldSpace(i, j + 1), GridCustom.getWorldSpace(i + 1, j + 1), Color.yellow, 100f, false);
            }
        }
    }
Ejemplo n.º 4
0
    // Blocks the area of koto tower
    private void Start()
    {
        Vector2 position = this.transform.position;

        // get x,y of the tower in the grid
        int x, y;

        GridCustom.getXYFromPosition(position, out x, out y);

        x += GridCustom.offsetX;
        y += GridCustom.offsetY;

        // blocks the inside of tower
        GridCustom.cells[x, y].cellContent = CellContent.KOTO_TOWER;
        Debug.DrawLine(GridCustom.getWorldSpace(x, y), GridCustom.getWorldSpace(x + 1, y), Color.blue, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x, y), GridCustom.getWorldSpace(x, y + 1), Color.blue, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x + 1, y), GridCustom.getWorldSpace(x + 1, y + 1), Color.blue, 100f, false);

        GridCustom.cells[x, y - 1].cellContent = CellContent.KOTO_TOWER;
        Debug.DrawLine(GridCustom.getWorldSpace(x, y - 1), GridCustom.getWorldSpace(x, y), Color.blue, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x, y - 1), GridCustom.getWorldSpace(x + 1, y - 1), Color.blue, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x + 1, y - 1), GridCustom.getWorldSpace(x + 1, y), Color.blue, 100f, false);

        GridCustom.cells[x - 1, y].cellContent = CellContent.KOTO_TOWER;
        Debug.DrawLine(GridCustom.getWorldSpace(x - 1, y), GridCustom.getWorldSpace(x, y), Color.blue, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x - 1, y), GridCustom.getWorldSpace(x - 1, y + 1), Color.blue, 100f, false);

        GridCustom.cells[x - 1, y - 1].cellContent = CellContent.KOTO_TOWER;
        Debug.DrawLine(GridCustom.getWorldSpace(x - 1, y - 1), GridCustom.getWorldSpace(x, y - 1), Color.blue, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(x - 1, y - 1), GridCustom.getWorldSpace(x - 1, y), Color.blue, 100f, false);

        // blocks behind of the tower
        for (int i = x - 2; i >= 0; i--)
        {
            for (int j = 0; j < GridCustom.height; j++)
            {
                GridCustom.cells[i, j].cellContent = CellContent.KOTO_TOWER;
                Debug.DrawLine(GridCustom.getWorldSpace(i, j), GridCustom.getWorldSpace(i + 1, j), Color.blue, 100f, false);
                Debug.DrawLine(GridCustom.getWorldSpace(i, j), GridCustom.getWorldSpace(i, j + 1), Color.blue, 100f, false);
                Debug.DrawLine(GridCustom.getWorldSpace(i + 1, j), GridCustom.getWorldSpace(i + 1, j + 1), Color.blue, 100f, false);
                Debug.DrawLine(GridCustom.getWorldSpace(i, j + 1), GridCustom.getWorldSpace(i + 1, j + 1), Color.blue, 100f, false);
            }
        }

        // blocks top of the tower
        for (int i = x - 1, k = x - 1; i < (k + 4); i++)
        {
            for (int j = y + 1; j < GridCustom.height; j++)
            {
                GridCustom.cells[i, j].cellContent = CellContent.KOTO_TOWER;
                Debug.DrawLine(GridCustom.getWorldSpace(i, j), GridCustom.getWorldSpace(i + 1, j), Color.blue, 100f, false);
                Debug.DrawLine(GridCustom.getWorldSpace(i, j), GridCustom.getWorldSpace(i, j + 1), Color.blue, 100f, false);
                Debug.DrawLine(GridCustom.getWorldSpace(i + 1, j), GridCustom.getWorldSpace(i + 1, j + 1), Color.blue, 100f, false);
                Debug.DrawLine(GridCustom.getWorldSpace(i, j + 1), GridCustom.getWorldSpace(i + 1, j + 1), Color.blue, 100f, false);
            }
        }

        // get neighbor (usually right path)
        neighbor = this.GetComponent <Point>().getSecondIndexPoint();

        int xNeighbor, yNeighbor;

        GridCustom.getXYFromPosition(neighbor.getCurrPosition(), out xNeighbor, out yNeighbor);

        xNeighbor += GridCustom.offsetX;
        yNeighbor += GridCustom.offsetY;

        // blocks the path (usually right)
        for (int i = x + 1; i < (xNeighbor - 1); i++)
        {
            GridCustom.cells[i, y].cellContent = CellContent.PATH;
            Debug.DrawLine(GridCustom.getWorldSpace(i, y), GridCustom.getWorldSpace(i + 1, y), Color.gray, 100f, false);
            Debug.DrawLine(GridCustom.getWorldSpace(i, y), GridCustom.getWorldSpace(i, y + 1), Color.gray, 100f, false);
            Debug.DrawLine(GridCustom.getWorldSpace(i, y + 1), GridCustom.getWorldSpace(i + 1, y + 1), Color.gray, 100f, false);

            GridCustom.cells[i, y - 1].cellContent = CellContent.PATH;
            Debug.DrawLine(GridCustom.getWorldSpace(i, y - 1), GridCustom.getWorldSpace(i + 1, y - 1), Color.gray, 100f, false);
            Debug.DrawLine(GridCustom.getWorldSpace(i, y - 1), GridCustom.getWorldSpace(i, y), Color.gray, 100f, false);
        }
    }
Ejemplo n.º 5
0
    // for touches
    void detectTouches()
    {
        // for touch
        if (Input.touchCount > 0 && GameManager.instance.isSelectTower && !isReadyToSpawn && !OtherMethod.onUiPressed(Input.GetTouch(0).position))
        {
            Touch touch = Input.GetTouch(0);
            // If it is the first time to build then make the tower
            if (!isReadyToBuild)
            {
                switch (GameManager.instance.selectedTower)
                {
                case 0:
                    currentTower     = Instantiate(towerMachineGun, this.transform);
                    currentTowerType = TowerType.MACHINE_GUN;
                    break;

                case 1:
                    currentTower     = Instantiate(towerSniper, this.transform);
                    currentTowerType = TowerType.SNIPER;
                    break;

                case 2:
                    currentTower     = Instantiate(towerElectric, this.transform);
                    currentTowerType = TowerType.ELECTRIC;
                    break;

                default:
                    currentTower     = Instantiate(towerMachineGun, this.transform);
                    currentTowerType = TowerType.MACHINE_GUN;
                    break;
                }

                currentTower.SetActive(false);
                SpriteRenderer renderer = currentTower.GetComponent <SpriteRenderer>();
                renderer.color = new Color(1, 1, 1, 150f / 256f);
                isReadyToBuild = true;
            }

            if (touch.phase == TouchPhase.Began)
            {
                Vector2 touchPosition;
                touchPosition = mainCamera.ScreenToWorldPoint(touch.position);

                // checking if the place is open field
                int x, y;
                GridCustom.getXYFromPosition(touchPosition, out x, out y);

                x += GridCustom.offsetX;
                y += GridCustom.offsetY;

                if (GridCustom.cells[x, y].cellContent == CellContent.OPEN_FIELD)
                {
                    currentTower.SetActive(true);
                    isReadyToSpawn = true;
                    canSpawn       = false;
                    currentTower.transform.position = GridCustom.getWorldSpace(x, y) + new Vector3(0.5f, 0.5f, 0);
                    currX = x;
                    currY = y;
                }
            }
        }

        if (Input.touchCount > 0 && isReadyToBuild && isReadyToSpawn && !OtherMethod.onUiPressed(Input.GetTouch(0).position))
        {
            Touch   touch = Input.GetTouch(0);
            Vector2 touchPosition;
            touchPosition = mainCamera.ScreenToWorldPoint(touch.position);

            int x, y;
            GridCustom.getXYFromPosition(touchPosition, out x, out y);

            x += GridCustom.offsetX;
            y += GridCustom.offsetY;
            // if the player touch the area again then spawn the tower
            if (touch.phase == TouchPhase.Ended && GameManager.instance.isSelectTower)
            {
                if (isReadyToSpawn && isReadyToBuild)
                {
                    canSpawn = true;
                }
                else if (canSpawn)
                {
                    // change the color
                    SpriteRenderer renderer = currentTower.GetComponent <SpriteRenderer>();
                    renderer.color = new Color(1, 1, 1, 1);

                    // activate it
                    TowerBehaviour tower = currentTower.GetComponent <TowerBehaviour>();
                    tower.activate();

                    // block the area
                    TowerGridBlocker gridBlocker = currentTower.GetComponent <TowerGridBlocker>();
                    gridBlocker.changeGridStatus();

                    // pay the tower
                    GameManager.instance.pay(currentTowerType);
                    towerButton.disableButton(GameManager.instance.selectedTower);

                    // reset variable
                    currentTower     = null;
                    currentTowerType = TowerType.MACHINE_GUN;
                    isReadyToBuild   = false;
                    isReadyToSpawn   = false;
                    canSpawn         = false;
                    currX            = -1;
                    currY            = -1;
                    desc.gameObject.SetActive(false);
                    GameEvents.current.TowerOrTrapBuild();
                    if (GameManager.instance.gameStart)
                    {
                        generator.StartCoroutine(generator.openGenerator());
                        kotoTower.StartCoroutine(kotoTower.openKotoTower());
                    }
                    return;
                }
            }
            else if (touch.phase == TouchPhase.Began && !(currX == x && currY == y))
            {
                isReadyToSpawn = false;
                canSpawn       = false;
            }
        }
    }
Ejemplo n.º 6
0
    // create blockers for path
    private void Start()
    {
        Vector3 currPosition = this.transform.position;
        // block the area of move point
        int xPoint, yPoint;

        GridCustom.getXYFromPosition(currPosition, out xPoint, out yPoint);

        xPoint += GridCustom.offsetX;
        yPoint += GridCustom.offsetY;

        GridCustom.cells[xPoint, yPoint].cellContent = (isBlocker ? CellContent.DISABLED : CellContent.PATH);
        Debug.DrawLine(GridCustom.getWorldSpace(xPoint, yPoint), GridCustom.getWorldSpace(xPoint + 1, yPoint), Color.gray, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(xPoint, yPoint), GridCustom.getWorldSpace(xPoint, yPoint + 1), Color.gray, 100f, false);

        GridCustom.cells[xPoint - 1, yPoint].cellContent = (isBlocker ? CellContent.DISABLED : CellContent.PATH);
        Debug.DrawLine(GridCustom.getWorldSpace(xPoint - 1, yPoint), GridCustom.getWorldSpace(xPoint, yPoint), Color.gray, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(xPoint - 1, yPoint), GridCustom.getWorldSpace(xPoint - 1, yPoint + 1), Color.gray, 100f, false);

        GridCustom.cells[xPoint, yPoint - 1].cellContent = (isBlocker ? CellContent.DISABLED : CellContent.PATH);
        Debug.DrawLine(GridCustom.getWorldSpace(xPoint, yPoint - 1), GridCustom.getWorldSpace(xPoint, yPoint), Color.gray, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(xPoint, yPoint - 1), GridCustom.getWorldSpace(xPoint + 1, yPoint - 1), Color.gray, 100f, false);

        GridCustom.cells[xPoint - 1, yPoint - 1].cellContent = (isBlocker ? CellContent.DISABLED : CellContent.PATH);
        Debug.DrawLine(GridCustom.getWorldSpace(xPoint - 1, yPoint - 1), GridCustom.getWorldSpace(xPoint - 1, yPoint), Color.gray, 100f, false);
        Debug.DrawLine(GridCustom.getWorldSpace(xPoint - 1, yPoint - 1), GridCustom.getWorldSpace(xPoint, yPoint - 1), Color.gray, 100f, false);

        // Block the path to neighbor
        neighbors = new List <Point>(this.GetComponent <Point>().getAllNeighbor());

        foreach (Point neighbor in neighbors)
        {
            Vector3 neighborPosition = neighbor.getCurrPosition();

            int xNeighbor, yNeighbor;
            GridCustom.getXYFromPosition(neighborPosition, out xNeighbor, out yNeighbor);

            xNeighbor += GridCustom.offsetX;
            yNeighbor += GridCustom.offsetY;

            direction = checkDirection(xPoint, xNeighbor, yPoint, yNeighbor);

            // only draw a right and up path
            switch (direction)
            {
            case Direction.RIGHT:
                // draw right path, if it is neighbor then the path iteration is different since its smaller
                for (int i = xPoint + 1, k = (neighbor.getIsGenerator() ? xNeighbor : xNeighbor - 1); i < k; i++)
                {
                    GridCustom.cells[i, yPoint].cellContent = (isBlocker ? CellContent.DISABLED : CellContent.PATH);
                    Debug.DrawLine(GridCustom.getWorldSpace(i, yPoint), GridCustom.getWorldSpace(i + 1, yPoint), Color.gray, 100f, false);
                    Debug.DrawLine(GridCustom.getWorldSpace(i, yPoint), GridCustom.getWorldSpace(i, yPoint + 1), Color.gray, 100f, false);
                    Debug.DrawLine(GridCustom.getWorldSpace(i, yPoint + 1), GridCustom.getWorldSpace(i + 1, yPoint + 1), Color.gray, 100f, false);

                    GridCustom.cells[i, yPoint - 1].cellContent = (isBlocker ? CellContent.DISABLED : CellContent.PATH);
                    Debug.DrawLine(GridCustom.getWorldSpace(i, yPoint - 1), GridCustom.getWorldSpace(i + 1, yPoint - 1), Color.gray, 100f, false);
                    Debug.DrawLine(GridCustom.getWorldSpace(i, yPoint - 1), GridCustom.getWorldSpace(i, yPoint), Color.gray, 100f, false);
                }
                break;

            case Direction.UP:
                // draw upward path, if it is neighbor then the path iteration is different since its smaller
                for (int i = yPoint + 1, k = (neighbor.getIsGenerator() ? yNeighbor : yNeighbor - 1); i < k; i++)
                {
                    GridCustom.cells[xPoint, i].cellContent = (isBlocker ? CellContent.DISABLED : CellContent.PATH);
                    Debug.DrawLine(GridCustom.getWorldSpace(xPoint, i), GridCustom.getWorldSpace(xPoint + 1, i), Color.gray, 100f, false);
                    Debug.DrawLine(GridCustom.getWorldSpace(xPoint, i), GridCustom.getWorldSpace(xPoint, i + 1), Color.gray, 100f, false);
                    Debug.DrawLine(GridCustom.getWorldSpace(xPoint, i + 1), GridCustom.getWorldSpace(xPoint + 1, i + 1), Color.gray, 100f, false);

                    GridCustom.cells[xPoint - 1, i].cellContent = (isBlocker ? CellContent.DISABLED : CellContent.PATH);
                    Debug.DrawLine(GridCustom.getWorldSpace(xPoint - 1, i), GridCustom.getWorldSpace(xPoint - 1, i + 1), Color.gray, 100f, false);
                    Debug.DrawLine(GridCustom.getWorldSpace(xPoint - 1, i), GridCustom.getWorldSpace(xPoint, i), Color.gray, 100f, false);
                }
                break;

            case Direction.DOWN:
                // draw downward path
                for (int i = yPoint - 1; i > yNeighbor; i--)
                {
                    GridCustom.cells[xPoint, i].cellContent = (isBlocker ? CellContent.DISABLED : CellContent.PATH);
                    Debug.DrawLine(GridCustom.getWorldSpace(xPoint, i), GridCustom.getWorldSpace(xPoint + 1, i), Color.gray, 100f, false);
                    Debug.DrawLine(GridCustom.getWorldSpace(xPoint, i), GridCustom.getWorldSpace(xPoint, i - 1), Color.gray, 100f, false);
                    Debug.DrawLine(GridCustom.getWorldSpace(xPoint + 1, i), GridCustom.getWorldSpace(xPoint + 1, i - 1), Color.gray, 100f, false);

                    GridCustom.cells[xPoint - 1, i].cellContent = (isBlocker ? CellContent.DISABLED : CellContent.PATH);
                    Debug.DrawLine(GridCustom.getWorldSpace(xPoint - 1, i), GridCustom.getWorldSpace(xPoint, i), Color.gray, 100f, false);
                    Debug.DrawLine(GridCustom.getWorldSpace(xPoint - 1, i), GridCustom.getWorldSpace(xPoint - 1, i - 1), Color.gray, 100f, false);
                }
                break;

            default:
                break;
            }
        }
    }