// Update is called once per frame async void Update() { switch (battleStage) { case BattleLoopStage.Initial: if (!cutscene.inProgress) { advanceBattleStage(); } break; case BattleLoopStage.Pick: //TODO This is temp just for testing until pick phase gets built. addUnit(UnitType.Knight, level.characters[0], 0, 0); addUnit(UnitType.Knight, level.characters[0], 1, 0); addUnit(UnitType.Knight, level.characters[0], 0, 1); addUnit(UnitType.Knight, level.characters[1], 3, 7); addUnit(UnitType.Knight, level.characters[1], 4, 7); foreach (Unit unit in battlefield.charactersUnits[level.characters[1]]) { Renderer rend = unit.gameObject.GetComponent <Renderer>(); rend.material.shader = Shader.Find("_Color"); rend.material.SetColor("_Color", Color.green); rend.material.shader = Shader.Find("Specular"); rend.material.SetColor("_SpecColor", Color.green); } advanceBattleStage(); break; case BattleLoopStage.BattleLoopStart: advanceBattleStage(); break; case BattleLoopStage.TurnChange: //There's probably a less fragile way of doing this. It's just to make sure this call only happens once per turn loop. if (!turnPlayerText.enabled) { currentCharacter = (currentCharacter + 1) % level.characters.Length; turnPlayerText.text = level.characters[currentCharacter].name + "'s turn"; turnPlayerText.enabled = true; turnChangeBackground.enabled = true; Util.setTimeout(advanceBattleStage, 1000); } break; case BattleLoopStage.TurnChangeEnd: turnPlayerText.enabled = false; turnChangeBackground.enabled = false; advanceBattleStage(); break; case BattleLoopStage.UnitSelection: advanceBattleStage(); break; case BattleLoopStage.ActionSelection: //If we've already entered this we're awaiting. Don't call it again this frame. if (!battleStageChanged) { break; } battleStageChanged = false; //Character.getMove() is responsible for validation so we assume the move to be legal Move move = await level.characters[currentCharacter].getMove(); Unit ourUnit = battlefield.units[move.fromX, move.fromY]; IBattlefieldItem selectedItem = battlefield.battlefieldItemAt(move.toX, move.toY); if (selectedItem is Tile) { //We selected a tile! lets move to it moveUnit(ourUnit, move.toX, move.toY); } else if (selectedItem is Unit) { //Targeted a hostile unit! fight! Unit selectedUnit = selectedItem as Unit; bool defenderDefeated = ourUnit.doBattleWith( selectedUnit, battlefield.map[move.toX, move.toY].Peek(), battlefield); await Task.Delay(TimeSpan.FromMilliseconds(250)); if (!defenderDefeated) { //Counterattack selectedUnit.doBattleWith( ourUnit, battlefield.map[move.fromX, move.fromY].Peek(), battlefield); } await Task.Delay(TimeSpan.FromMilliseconds(250)); } else { Debug.LogWarning("Item of unrecognized type clicked on."); } checkWinAndLose(); //If all of our units have moved advance. Otherwise, go back to unit selection. ourUnit.hasMovedThisTurn = true; if (battlefield.charactersUnits[level.characters[currentCharacter]].All(unit => unit.hasMovedThisTurn)) { advanceBattleStage(); } else { setBattleLoopStage(BattleLoopStage.UnitSelection); } break; case BattleLoopStage.EndTurn: foreach (Unit unit in battlefield.charactersUnits[level.characters[currentCharacter]]) { unit.hasMovedThisTurn = false; } checkWinAndLose(); advanceBattleStage(); break; } }