private bool handleAttack(bool updateState) { // If both tanks have been clicked, orchestrate the attack if (tankClicked != null && tankClicked2 != null) { // Create coordinates CoordinateSet currentPlayerTankCoordinates = new CoordinateSet((int)tankClicked.transform.position.x, (int)tankClicked.transform.position.z); CoordinateSet targetPlayerTankCoordinates = new CoordinateSet((int)tankClicked2.transform.position.x, (int)tankClicked2.transform.position.z); PlayerColors opposingPlayer; if (playerTurn == PlayerColors.Red) { opposingPlayer = PlayerColors.Blue; } else { opposingPlayer = PlayerColors.Red; } if (gs.getPlayerTank(playerTurn, currentPlayerTankCoordinates).getHealth() <= 0 || gs.getPlayerTank(opposingPlayer, targetPlayerTankCoordinates).getHealth() <= 0) { return(false); } // Check if the attack is valid if (gs.checkValidAttack(playerTurn, currentPlayerTankCoordinates, targetPlayerTankCoordinates, updateState)) { if (!updateState) { return(true); } // Do knockback handleKnockback(currentPlayerTankCoordinates, targetPlayerTankCoordinates); } else { if (!updateState) { return(false); } } // Update player powerup state gs.updatePlayerPowerupState(playerTurn); // Update the state information round = Rounds.Gamble; TileHighlighter.resetTiles(); gs.toggleTankRings(playerTurn == PlayerColors.Red ? redTanks : blueTanks, new CoordinateSet(-1, -1), true); gs.toggleTankRings(playerTurn == PlayerColors.Red ? blueTanks : redTanks, new CoordinateSet(-1, -1), false); printTurn(); tankClicked = null; tankClicked2 = null; updateTooltips("Gamble"); } return(true); }
private void assignTanksClicked(RaycastHit hit) { // Assign tanks clicked Tank currentTank; if (hit.transform.gameObject.tag == "Red Tank") { currentTank = gs.getPlayerTank(PlayerColors.Red, new CoordinateSet((int)hit.transform.position.x, (int)hit.transform.position.z)); if (playerTurn == PlayerColors.Red && !currentTank.isDead()) { tankClicked = hit.transform.gameObject; TileHighlighter.resetTiles(); highlightAttackTiles(new CoordinateSet((int)hit.transform.position.x, (int)hit.transform.position.z)); if (gs.playerHasValidAttack(playerTurn, new CoordinateSet((int)hit.transform.position.x, (int)hit.transform.position.z))) { updateTooltips("Attack2"); } else { updateTooltips("Attack3"); } } else if (!currentTank.isDead()) { tankClicked2 = hit.transform.gameObject; TileHighlighter.resetTiles(); } } else if ((hit.transform.gameObject.tag == "Blue Tank")) { currentTank = gs.getPlayerTank(PlayerColors.Blue, new CoordinateSet((int)hit.transform.position.x, (int)hit.transform.position.z)); if (playerTurn == PlayerColors.Blue && !currentTank.isDead()) { tankClicked = hit.transform.gameObject; TileHighlighter.resetTiles(); highlightAttackTiles(new CoordinateSet((int)hit.transform.position.x, (int)hit.transform.position.z)); if (gs.playerHasValidAttack(playerTurn, new CoordinateSet((int)hit.transform.position.x, (int)hit.transform.position.z))) { updateTooltips("Attack2"); } else { updateTooltips("Attack3"); } } else if (!currentTank.isDead()) { tankClicked2 = hit.transform.gameObject; TileHighlighter.resetTiles(); } } }
private void handleMove(RaycastHit hit) { if ((hit.transform.gameObject.tag == "Red Tank" && playerTurn == PlayerColors.Red) || (hit.transform.gameObject.tag == "Blue Tank" && playerTurn == PlayerColors.Blue)) { tankClicked = hit.transform.gameObject; CoordinateSet tankCoordinates = new CoordinateSet((int)hit.transform.position.x, (int)hit.transform.position.z); Tank currentTank = gs.getPlayerTank(playerTurn, tankCoordinates); // If the player didn't choose one of their own tanks, or if that tank is dead, just ignore if (currentTank.isDead() || currentTank.getPlayer().getPlayerColor() != playerTurn) { tankClicked = null; return; } TileHighlighter.resetTiles(); TileHighlighter.highlightValidTiles(currentTank.getValidMovements(gs.getGrid(), tankCoordinates), round); gs.toggleTankRings(playerTurn == PlayerColors.Red ? redTanks : blueTanks, tankCoordinates, true); gs.toggleTankRings(playerTurn == PlayerColors.Red ? blueTanks : redTanks, new CoordinateSet(-1, -1), false); updateTooltips("Move2"); } else if (tankClicked != null) { tileClicked = hit.transform.gameObject; CoordinateSet tankCoordinates = new CoordinateSet((int)tankClicked.transform.position.x, (int)tankClicked.transform.position.z); CoordinateSet tileCoordinates = new CoordinateSet((int)tileClicked.transform.position.x, (int)tileClicked.transform.position.z); if (gs.checkValidMove(playerTurn, tankCoordinates, tileCoordinates, true)) { TileHighlighter.resetTiles(); Tank currentTank = gs.getPlayerTank(playerTurn, tileCoordinates); // Tank moving animation StartCoroutine(moveTank(tileClicked, tankClicked, currentTank)); tileClicked = null; tankClicked = null; round = Rounds.Attack; TileHighlighter.resetTiles(); printTurn(); updateTooltips("Attack1"); gs.toggleTankRings(redTanks, new CoordinateSet(-1, -1), false); gs.toggleTankRings(blueTanks, new CoordinateSet(-1, -1), false); } } }
private void changeTurns() { if (playerTurn == PlayerColors.Red) { playerTurn = PlayerColors.Blue; if (aiON == false) { camera.translateToPlayer(PlayerColors.Blue); } } else { playerTurn = PlayerColors.Red; if (aiON == false) { camera.translateToPlayer(PlayerColors.Red); } } TileHighlighter.resetTiles(); gs.toggleTankRings(redTanks, new CoordinateSet(-1, -1), false); gs.toggleTankRings(blueTanks, new CoordinateSet(-1, -1), false); roundCounter++; }
// Update is called once per frame void Update() { if (aiON && aiMove) { return; } if (numGambles == 4) { updateTooltips("GLHF"); } // Check if gameover gameOver(); // Run if ai's turn if (playerTurn == PlayerColors.Blue && aiON) { gs.toggleTankRings(redTanks, new CoordinateSet(-1, -1), false); gs.toggleTankRings(blueTanks, new CoordinateSet(-1, -1), false); //handleGreedyAi(); if (aiMove == false) { aiMove = true; if (currentLevel == Levels.Level1) { StartCoroutine(handleGreedyAi()); } else { StartCoroutine(handleMinMaxAi()); } } } // Skip turn if spacebar is pressed if (Input.GetKeyDown(KeyCode.Space) && !aiMove) { round++; TileHighlighter.resetTiles(); if (round == Rounds.Gamble) { gs.toggleTankRings(playerTurn == PlayerColors.Red ? redTanks : blueTanks, new CoordinateSet(-1, -1), true); gs.toggleTankRings(playerTurn == PlayerColors.Red ? blueTanks : redTanks, new CoordinateSet(-1, -1), false); updateTooltips("Gamble"); gs.updatePlayerPowerupState(playerTurn); } if (round == Rounds.Attack) { gs.toggleTankRings(redTanks, new CoordinateSet(-1, -1), false); gs.toggleTankRings(blueTanks, new CoordinateSet(-1, -1), false); updateTooltips("Attack1"); } if (round > Rounds.Gamble) { round = Rounds.Move; updateTooltips("Move1"); numGambles++; changeTurns(); } printTurn(); } // If nothing has been clicked, return if (oc.objectClicked == null) { return; } // Assign variables objectClicked = oc.objectClicked; oc.objectClicked = null; ClickItems items = getItemClicked(); RaycastHit hit = items.getRaycastHit(); if (!items.isValid()) { return; } switch (round) { case Rounds.Move: handleMove(hit); break; case Rounds.Attack: assignTanksClicked(hit); handleAttack(true); break; case Rounds.Gamble: handleGamble(hit); break; } gs.updatePlayerHealthBars(hpController); }