// 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); }
// 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); } } }
// 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); } }
// Update is called once per frame after normal update void Update() { if (GameManager.instance.isPaused) { cancel(); } // for touch if (Input.touchCount > 0 && GameManager.instance.isSelectTrap && !isReadyToSpawn && !OtherMethod.onUiPressed(Input.GetTouch(0).position)) { Touch touch = Input.GetTouch(0); // If it is the first time to build then make the trap if (!isReadyToBuild && !OtherMethod.onUiPressed(touch.position)) { switch (GameManager.instance.selectedTrap) { case 0: currentTrap = Instantiate(trapBomb, this.transform); currentTrapType = TrapType.BOMB_TRAP; break; case 1: currentTrap = Instantiate(trapTime, this.transform); currentTrapType = TrapType.TIME_TRAP; break; case 2: currentTrap = Instantiate(trapFreeze, this.transform); currentTrapType = TrapType.FREEZE_TRAP; break; default: currentTrap = Instantiate(trapBomb, this.transform); currentTrapType = TrapType.BOMB_TRAP; break; } currentTrap.SetActive(false); SpriteRenderer renderer = currentTrap.GetComponent <SpriteRenderer>(); renderer.color = new Color(1, 1, 1, 150f / 256f); isReadyToBuild = true; } if (touch.phase == TouchPhase.Began && !OtherMethod.onUiPressed(touch.position)) { 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.PATH) { currentTrap.SetActive(true); isReadyToSpawn = true; canSpawn = false; currentTrap.transform.position = new Vector3(touch.position.x, touch.position.y, 0); } } } 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 trap if (touch.phase == TouchPhase.Ended && GameManager.instance.isSelectTrap) { if (isReadyToSpawn && isReadyToBuild) { canSpawn = true; } else if (canSpawn) { // change the color SpriteRenderer renderer = currentTrap.GetComponent <SpriteRenderer>(); renderer.color = new Color(1, 1, 1, 1); // activate it TrapsBehaviour trap = currentTrap.GetComponent <TrapsBehaviour>(); trap.activate(); // pay the trap GameManager.instance.pay(currentTrapType); trapButton.disableButton(GameManager.instance.selectedTrap); // reset variable currentTrap = null; currentTrapType = TrapType.BOMB_TRAP; isReadyToBuild = false; isReadyToSpawn = false; canSpawn = false; 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 && Vector3.Distance(currentTrap.transform.position, new Vector3(touchPosition.x, touchPosition.y, 0f)) > 0.5f) { isReadyToSpawn = false; canSpawn = false; } } // for mouse (debug) if (Input.GetMouseButton(0) && GameManager.instance.isSelectTrap && !OtherMethod.onUiPressed(Input.mousePosition)) { // If it is the first time to build then make the trap if (!isReadyToBuild) { switch (GameManager.instance.selectedTrap) { case 0: currentTrap = Instantiate(trapBomb, this.transform); currentTrapType = TrapType.BOMB_TRAP; break; case 1: currentTrap = Instantiate(trapTime, this.transform); currentTrapType = TrapType.TIME_TRAP; break; case 2: currentTrap = Instantiate(trapFreeze, this.transform); currentTrapType = TrapType.FREEZE_TRAP; break; default: currentTrap = Instantiate(trapBomb, this.transform); currentTrapType = TrapType.BOMB_TRAP; break; } currentTrap.SetActive(false); SpriteRenderer renderer = currentTrap.GetComponent <SpriteRenderer>(); renderer.color = new Color(1, 1, 1, 150f / 256f); isReadyToBuild = true; } Vector2 touchPosition; touchPosition = mainCamera.ScreenToWorldPoint(Input.mousePosition); // checking if the place is open field int x, y; GridCustom.getXYFromPosition(touchPosition, out x, out y); x += GridCustom.offsetX; y += GridCustom.offsetY; if (Vector3.Distance(currentTrap.transform.position, touchPosition) > 0.5f) { isReadyToSpawn = false; } if (GridCustom.cells[x, y].cellContent == CellContent.PATH && !isReadyToSpawn) { currentTrap.SetActive(true); currentTrap.transform.position = new Vector3(touchPosition.x, touchPosition.y, -0); } } // if the player touch the area again then spawn the trap, if its the first time then just be ready to spawn if (Input.GetMouseButtonUp(0) && GameManager.instance.isSelectTrap && !OtherMethod.onUiPressed(Input.mousePosition)) { if (!isReadyToSpawn && isReadyToBuild) { isReadyToSpawn = true; } else if (isReadyToBuild) { // change the color SpriteRenderer renderer = currentTrap.GetComponent <SpriteRenderer>(); renderer.color = new Color(1, 1, 1, 1); // activate it TrapsBehaviour trap = currentTrap.GetComponent <TrapsBehaviour>(); trap.activate(); // pay the trap GameManager.instance.pay(currentTrapType); trapButton.disableButton(GameManager.instance.selectedTrap); // reset variable currentTrap = null; currentTrapType = TrapType.BOMB_TRAP; isReadyToBuild = false; isReadyToSpawn = false; desc.gameObject.SetActive(false); GameEvents.current.TowerOrTrapBuild(); if (GameManager.instance.gameStart) { generator.StartCoroutine(generator.openGenerator()); kotoTower.StartCoroutine(kotoTower.openKotoTower()); } return; } } }
// 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; } } }
// 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; } } }