//AI routine to move a single unit IEnumerator _AIRoutineSingleUnit(UnitTB unit) { while (GameControlTB.IsActionInProgress()) { yield return(null); } yield return(new WaitForSeconds(1f)); unitInAction = true; instance.StartCoroutine(instance._MoveUnit(unit)); //wait a frame for the unit to start moving, set the inAction flag, etc.. yield return(null); while (unit.InAction() || GameControlTB.IsActionInProgress() || unitInAction) { yield return(null); } GameControlTB.OnEndTurn(); }
void OnEndTurnButton() { if (GameControlTB.IsActionInProgress()) { return; } if (GameControlTB.GetTurnMode() != _TurnMode.SingleUnitPerTurn) { if (GameControlTB.GetMoveOrder() == _MoveOrder.Free) { if (UnitControl.selectedUnit != null) { UnitControl.selectedUnit.moved = true; UnitControl.selectedUnit.attacked = true; UnitControl.MoveUnit(UnitControl.selectedUnit); } } } GameControlTB.OnEndTurn(); }
//code execution for when a left mouse click happen on a tile and when a touch is double tap on a tile public void OnTouchMouseDown() { #if !UNITY_IPHONE && !UNITY_ANDROID if (GameControlTB.IsCursorOnUI(Input.mousePosition)) { return; } //if(GameControlTB.IsObjectOnUI(pos)) return; #endif if (GameControlTB.IsUnitPlacementState()) { PlaceUnit(); return; } if (GameControlTB.GetTurnMode() != _TurnMode.SingleUnitPerTurn) { if (!GameControlTB.IsPlayerTurn()) { //if(GameControlTB.turnID!=GameControlTB.GetPlayerFactionTurnID()){ return; } } if (GameControlTB.IsActionInProgress()) { return; } if (!walkable && !GridManager.IsInTargetTileSelectMode()) { return; } UnitTB sUnit = UnitControl.selectedUnit; //if a friendly unit has been selected //if(sUnit!=null && sUnit.IsControllable(GameControlTB.GetPlayerFactionID())){ if (sUnit != null && sUnit.IsControllable()) { //if HexFridManager is actively looking for a target for current selectedUnit if (GridManager.IsInTargetTileSelectMode()) { ManualSelect(); } else { if (!walkableToSelected && !attackableToSelected) { ManualSelect(); } else { if (attackableToSelected && unit != null) { sUnit.Attack(unit); } else if (walkableToSelected) { sUnit.Move(this); } else { Debug.Log("error"); } } } return; } else { ManualSelect(); } }
//AI routine for faction based turn mode, AI will move all available unit for the faction //called by UnitControl in OnNextTurn IEnumerator _AIRoutine(int factionID) { while (GameControlTB.IsActionInProgress()) { yield return(null); } yield return(new WaitForSeconds(1f)); Faction faction = UnitControl.GetFactionInTurn(factionID); List <UnitTB> allUnit = faction.allUnitList; int counter = 0; if (GameControlTB.GetMoveOrder() == _MoveOrder.Free) { int count = allUnit.Count; //create a list (1, 2, 3..... ) to the size of the unit, this is for unit shuffling List <int> IDList = new List <int>(); for (int i = 0; i < count; i++) { IDList.Add(i); } for (int i = 0; i < count; i++) { //randomly select a unit to move int rand = UnityEngine.Random.Range(0, IDList.Count); /* * //if(counter%2==0) rand=0; * //else rand=IDList.Count-1; * * string label="rand: "+rand+" ("+IDList.Count+")"; * for(int nn=0; nn<IDList.Count; nn++){ * label+=" "+IDList[nn]; * } * Debug.Log(label); */ int ID = IDList[rand]; UnitTB unit = allUnit[ID]; //remove the unit from allUnit list so that it wont be selected again IDList.RemoveAt(rand); counter += 1; //move the unit unitInAction = true; StartCoroutine(_MoveUnit(unit)); //wait a frame for the unit to start moving, set the inAction flag, etc.. yield return(null); //wait while the unit is in action (making its move) //and while the game event is in progress (counter attack, death effect/animation, etc.) //and while the unit localUnitInactionFlag hasn't been cleared while (unit.InAction() || GameControlTB.IsActionInProgress() || unitInAction) { yield return(null); } //for counter-attack enabled, in case the unit is destroyed in after being countered if (unit.IsDestroyed()) { //label=""; for (int n = rand; n < IDList.Count; n++) { if (IDList[n] >= ID) { //label+=" "+IDList[n]; IDList[n] -= 1; } } //Debug.Log("unit destroyed "+label); } /* * label="rand: "+rand+" ("+IDList.Count+")"; * for(int nn=0; nn<IDList.Count; nn++){ * label+=" "+IDList[nn]; * } * Debug.Log(label); */ if (GameControlTB.battleEnded) { yield break; } } } else if (GameControlTB.GetMoveOrder() == _MoveOrder.FixedRandom || GameControlTB.GetMoveOrder() == _MoveOrder.FixedStatsBased) { for (int i = 0; i < allUnit.Count; i++) { UnitTB unit = allUnit[i]; //move the unit unitInAction = true; StartCoroutine(_MoveUnit(unit)); yield return(null); //wait while the unit is in action (making its move) //and while the game event is in progress (counter attack, death effect/animation, etc.) while (unit.InAction() || GameControlTB.IsActionInProgress() || unitInAction) { yield return(null); } if (GameControlTB.battleEnded) { yield break; } } } faction.allUnitMoved = true; yield return(new WaitForSeconds(0.5f)); EndTurn(); }
//execute move/attack for a single unit IEnumerator _MoveUnit(UnitTB unit) { //make sure no event is taking place while (GameControlTB.IsActionInProgress()) { yield return(null); } if (!unit.IsStunned()) { /* * DateTime timeS; * DateTime timeE; * TimeSpan timeSpan; * timeS=System.DateTime.Now; */ Tile destinationTile = null; if (aIStance == _AIStance.Trigger) { if (unit.triggered) { destinationTile = Analyse(unit); } } else { destinationTile = Analyse(unit); } //if there's target in current tile, just attack it if (destinationTile == unit.occupiedTile) { if (destinationTile.AIHostileList.Count != 0) { //unit.Attack(destinationTile.AIHostileList[UnityEngine.Random.Range(0, destinationTile.AIHostileList.Count)]); //unit will simply attack instead of move if (!unit.MoveAttack(unit.occupiedTile)) { NoActionForUnit(unit); } } else { unitInAction = false; } yield break; } if (destinationTile != null) { if (GameControlTB.EnableFogOfWar()) { bool visibleToPlayer = CheckEncounterOnPath(unit.occupiedTile, destinationTile); if (visibleToPlayer) { if (!unit.MoveAttack(destinationTile)) { NoActionForUnit(unit); } } else { if (destinationTile.AIHostileList.Count == 0) { MoveUnitToTileInstant(unit, destinationTile); unit.CompleteAllAction(); } else { if (!unit.MoveAttack(destinationTile)) { NoActionForUnit(unit); } } } } else { if (!unit.MoveAttack(destinationTile)) { NoActionForUnit(unit); } } } else { //no action is available for the unit, simply registred it as moved NoActionForUnit(unit); } /* * timeE=System.DateTime.Now; * timeSpan=timeE-timeS; * Debug.Log( "Time:"+timeSpan.TotalMilliseconds); */ } else { NoActionForUnit(unit); } }