Ejemplo n.º 1
0
 void Awake()
 {
     if (instance == null)
     {
         instance = this;
     }
 }
Ejemplo n.º 2
0
		void _SetupFaction(){
			for(int i=0; i<factionList.Count; i++){
				if(factionList[i].isPlayerFaction){
					playerFactionIDList.Add(factionList[i].ID);
					
					for(int n=0; n<factionList[i].allUnitList.Count; n++){
						UpdateHostileUnitTriggerStatus(factionList[i].allUnitList[n]);
					}
				}
				
				if(TurnControl.GetTurnMode()!=_TurnMode.UnitPerTurn){
					if(TurnControl.GetMoveOrder()!=_MoveOrder.Random){
						factionList[i].allUnitList=ArrangeUnitListToMovePriority(factionList[i].allUnitList);
					}
				}
				
				factionList[i].NewTurn(true);	//pass true to call new turn on all unit
				//for(int n=0; n<factionList[i].allUnitList.Count; n++) factionList[i].allUnitList[n].ResetUnitTurnData();
				
				totalUnitCount+=factionList[i].allUnitList.Count;
			}
			
			if(factionList.Count==playerFactionIDList.Count) hasAIInGame=false;
			
			Debug.Log("SetupFaction  "+TurnControl.GetTurnMode());
			if(TurnControl.GetTurnMode()==_TurnMode.UnitPerTurn){
				for(int i=0; i<factionList.Count; i++){
					for(int n=0; n<factionList[i].allUnitList.Count; n++) allUnitList.Add(factionList[i].allUnitList[n]);
				}
				
				allUnitList=ArrangeUnitListToMovePriority(allUnitList);
				Debug.Log("SetupFaction  allUnitList");
			}
			
		}
Ejemplo n.º 3
0
        //when a unit completes it's move, put an unused indicator on the unit tile
        //this will only be called if the unit in question is player unit
        private void OnUnitMoveDepleted(Unit unit)
        {
            if (TurnControl.GetTurnMode() != _TurnMode.FactionPerTurn)
            {
                return;
            }

            int index = -1;

            for (int i = 0; i < movedIndicatorList.Count; i++)
            {
                if (movedIndicatorList[i].gameObject.activeInHierarchy)
                {
                    index = i;
                    break;                    //continue;
                }
            }

            if (index == -1)
            {
                Transform moveIndicatorT = (Transform)Instantiate(movedIndicatorList[0]);
                moveIndicatorT.parent = transform;
                movedIndicatorList.Add(moveIndicatorT);
                index = movedIndicatorList.Count - 1;
            }

            movedIndicatorList[index].position = unit.tile.GetPos();
            movedIndicatorList[index].gameObject.SetActive(true);
        }
Ejemplo n.º 4
0
 void OnUnitDestroyed(Unit unit)
 {
     if (TurnControl.GetTurnMode() != _TurnMode.UnitPerTurn)
     {
         return;
     }
     IterateDuration();
 }
Ejemplo n.º 5
0
 public static void OnUnitDestroyed()
 {
     if (TurnControl.GetTurnMode() != _TurnMode.UnitPerTurn)
     {
         return;
     }
     instance.IterateEndTurn();
 }
Ejemplo n.º 6
0
 void OnFactionDestroyed(int facID)
 {
     if (TurnControl.GetTurnMode() == _TurnMode.UnitPerTurn)
     {
         return;
     }
     IterateDuration();
 }
Ejemplo n.º 7
0
		public void _EndTurn_FactionUnitPerTurn(){
			selectedFactionID+=1;
			if(selectedFactionID>=factionList.Count) selectedFactionID=0;
			if(TurnControl.GetMoveOrder()==_MoveOrder.Free){
				factionList[selectedFactionID].NewTurn(true);	//enabled if there's no need to cycle through all unit
				factionList[selectedFactionID].SelectRandomAvailableUnit();
			}
			else factionList[selectedFactionID].SelectNextUnitInQueue();
		}
Ejemplo n.º 8
0
		public void _UnitMoveDepleted(Unit unit){
			if(TurnControl.GetTurnMode()!=_TurnMode.FactionPerTurn){
				for(int i=0; i<factionList.Count; i++){
					if(factionList[i].ID==unit.factionID){
						factionList[i].UnitMoveDepleted(unit);
					}
				}
			}
		}
Ejemplo n.º 9
0
        IEnumerator DelayEndTurn()
        {
            yield return(new WaitForSeconds(1f));

            while (!TurnControl.ClearToProceed())
            {
                yield return(null);
            }
            GameControl.EndTurn();
        }
Ejemplo n.º 10
0
		public List<Unit> _GetAllUnit(){
			if(TurnControl.GetTurnMode()==_TurnMode.UnitPerTurn) return allUnitList;
			else{
				List<Unit> list=new List<Unit>();
				for(int i=0; i<factionList.Count; i++){
					for(int n=0; n<factionList[i].allUnitList.Count; n++) list.Add(factionList[i].allUnitList[n]);
				}
				return list;
			}
		}
Ejemplo n.º 11
0
        //clear movingUnit flag so the next unit can be moved
        IEnumerator EndMoveUnitRoutine()
        {
            while (!TurnControl.ClearToProceed())
            {
                AIDebug("Waiting for all sequence to complete");
                yield return(null);
            }
            yield return(null);

            movingUnit = false;
        }
Ejemplo n.º 12
0
 private void ResetTurnCounter()
 {
     if (TurnControl.GetTurnMode() == _TurnMode.UnitPerTurn)
     {
         turnCounter = FactionManager.GetTotalUnitCount();
     }
     else
     {
         turnCounter = FactionManager.GetTotalFactionCount();
     }
 }
Ejemplo n.º 13
0
		public void _UnitTurnPriorityChanged(List<int> facIDList){
			if(TurnControl.GetTurnMode()==_TurnMode.UnitPerTurn){
				allUnitList=ArrangeUnitListToMovePriority(allUnitList);
			}
			else if(TurnControl.GetMoveOrder()!=_MoveOrder.Random){
				for(int i=0; i<factionList.Count; i++){
					if(facIDList.Contains(factionList[i].ID)){
						factionList[i].allUnitList=ArrangeUnitListToMovePriority(factionList[i].allUnitList);
					}
				}
			}
		}
Ejemplo n.º 14
0
        //end the turn, called when EndTurn button are pressed or when a unit has used up all its move(in FactionUnitPerTurn & UnitPerTurn mode)
        public static void EndTurn()
        {
            if (!TurnControl.ClearToProceed())
            {
                return;
            }

            //if(onIterateTurnE!=null) onIterateTurnE();	//listen by EffectTracker and AbilityManager to iterate effect and cd duration
            //listen by tile in for tracking forceVisible(scan)

            instance.StartCoroutine(instance._EndTurn());
        }
Ejemplo n.º 15
0
        //new turn, clear all indicator
        private void OnNewTurn()
        {
            if (TurnControl.GetTurnMode() != _TurnMode.FactionPerTurn)
            {
                return;
            }

            for (int i = 0; i < movedIndicatorList.Count; i++)
            {
                movedIndicatorList[i].gameObject.SetActive(false);
            }
        }
Ejemplo n.º 16
0
        public void OnEndTurnButton()
        {
            if (!TurnControl.ClearToProceed())
            {
                return;
            }
            OnGameInAction(true);

            endTurnButton.imgHighlight.gameObject.SetActive(false);

            //GameControl.EndTurn();
            UIMainControl.EndTurn();
        }
Ejemplo n.º 17
0
        void Awake()
        {
            instance = (GameControl)target;
            LoadDB();

            turnControl        = (TurnControl)FindObjectOfType(typeof(TurnControl));
            gridManager        = (GridManager)FindObjectOfType(typeof(GridManager));
            factionManager     = (FactionManager)FindObjectOfType(typeof(FactionManager));
            collectibleManager = (CollectibleManager)FindObjectOfType(typeof(CollectibleManager));
            settingDB          = SettingDB.LoadDB();

            InitLabel();
        }
Ejemplo n.º 18
0
        public IEnumerator _EndTurn()
        {
            Debug.Log("end turn");

            ClearSelectedUnit();

            float delay = CollectibleManager.NewTurn();

            if (delay > 0)
            {
                yield return(new WaitForSeconds(delay));
            }

            TurnControl.EndTurn();
        }
Ejemplo n.º 19
0
		public void _EndTurn_FactionPerTurn(){
			GameControl.ClearSelectedUnit();
			
			selectedFactionID+=1;
			if(selectedFactionID>=factionList.Count) selectedFactionID=0;
			factionList[selectedFactionID].NewTurn(true);
			
			if(factionList[selectedFactionID].isPlayerFaction){	//if it's a player's faction, select a unit
				if(TurnControl.GetMoveOrder()==_MoveOrder.Free) _SelectNextUnitInFaction_Free();
				else _SelectNextUnitInFaction_NotFree();
			}
			else{															//if it's a AI's faction, execute AI move
				if(TurnControl.GetMoveOrder()==_MoveOrder.Free) AIManager.MoveFaction(factionList[selectedFactionID]);
				else _SelectNextUnitInFaction_NotFree();
			}
		}
Ejemplo n.º 20
0
        public void Init()
        {
            if (instance == null)
            {
                instance = this;
            }

            actionInProgress = 0;

            currentTurnID = -1;

            if (turnMode == _TurnMode.UnitPerTurn)
            {
                moveOrder = _MoveOrder.StatsBased;
            }
        }
Ejemplo n.º 21
0
 public static bool CanSelectUnit(Unit unit)
 {
     if (TurnControl.GetMoveOrder() != _MoveOrder.Free)
     {
         return(false);
     }
     if (TurnControl.GetTurnMode() == _TurnMode.FactionUnitPerTurn && TurnControl.HasMoved())
     {
         return(false);
     }
     if (TurnControl.GetTurnMode() == _TurnMode.UnitPerTurn)
     {
         return(false);
     }
     if (FactionManager.GetSelectedFactionID() != unit.factionID)
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 22
0
        //for FactionPerTurn mode, breakWhenExceedLimit is set to true, where selectUnitID resets when it reachs end
        //for FactionUnitPerTurn mode, breakWhenExceedLimit is set to false, where selectUnitID loops forever
        public bool SelectNextUnitInQueue(bool breakWhenExceedLimit = false)
        {
            //if(TurnControl.GetMoveOrder()==_MoveOrder.Free) return true;

            selectedUnitID += 1;

            //skipped stunned unit
            //while(allUnitList[selectedUnitID].IsStunned()){
            //	selectedUnitID+=1;
            //	if(selectedUnitID>=allUnitList.Count) break;
            //}
            if (selectedUnitID >= allUnitList.Count)
            {
                if (breakWhenExceedLimit)                       //for FactionPerTurn mode, reset the ID
                {
                    selectedUnitID = -1;
                    return(true);
                }
                selectedUnitID = 0;
            }

            if (TurnControl.GetTurnMode() != _TurnMode.FactionPerTurn)
            {
                bool isUnitActive = allUnitList[selectedUnitID].NewTurn();                      //in case unit is destroyed by damage over time effect
                if (!isUnitActive)
                {
                    return(SelectNextUnitInQueue());
                }
            }

            if (allUnitList[selectedUnitID].isAIUnit)
            {
                AIManager.MoveUnit(allUnitList[selectedUnitID]);
            }
            else
            {
                GameControl.SelectUnit(allUnitList[selectedUnitID], false);
            }

            return(false);
        }
Ejemplo n.º 23
0
		public void _InsertUnit(Unit unit, int factionID=0){ 
			unit.factionID=factionID;

			unit.InitUnitAbility();
			
			for(int i=0; i<factionList.Count; i++){
				if(factionList[i].ID==factionID){
					factionList[i].allUnitList.Add(unit);
					unit.isAIUnit=!factionList[i].isPlayerFaction;
				}
			}
			
			if(TurnControl.GetTurnMode()==_TurnMode.UnitPerTurn) allUnitList.Add(unit);
			
			_UnitTurnPriorityChanged( new List<int>{ unit.factionID} );
			
			unit.UpdateVisibility();
			
			//if(onInsertUnitE!=null) onInsertUnitE(unit);
			TBTK.OnNewUnit(unit);
		}
Ejemplo n.º 24
0
        IEnumerator DelayStartGame(float delay = 0.5f)
        {
            yield return(null);

            FactionManager.SetupFaction();
            GridManager.SetupGridForFogOfWar();
            yield return(null);

            PerkManager.InitPerkProgress();             //initiate prepurchase perk and so on

            if (delay > 0)
            {
                yield return(new WaitForSeconds(delay));
            }

            gamePhase = _GamePhase.Play;
            TurnControl.StartGame();                    //this will initiate unit selection and start the game
            yield return(null);

            //if(onGameStartE!=null) onGameStartE();
            TBTK.OnGameStart();
        }
Ejemplo n.º 25
0
		public void _OnUnitDestroyed(Unit unit){
			totalUnitCount-=1;
			
			//assume isObjectUnit is the boolean flag
			if(unit.isObjectUnit){
				GameControl.GameOver(0);	//assume 0 is the player unit faction
				return;
			}
			
			//remove unit from allUnitList so it no longers reserve a turn
			if(TurnControl.GetTurnMode()==_TurnMode.UnitPerTurn){
				int ID=allUnitList.IndexOf(unit);
				if(ID<=selectedUnitID){
					selectedUnitID-=1;
				}
				allUnitList.Remove(unit);
			}
			
			//remove the unit from the faction, and if the faction has no active unit remain, the faction itself is remove (out of the game)
			for(int i=0; i<factionList.Count; i++){
				if(factionList[i].ID==unit.factionID){
					factionList[i].RemoveUnit(unit);
					if(factionList[i].allUnitList.Count==0){
						TurnControl.OnFactionDestroyed();	//to track cd on ability and effect
						TBTK.OnFactionDestroyed(factionList[i].ID);
						
						factionList.RemoveAt(i);
						
						if(selectedFactionID>i) selectedFactionID-=1;
						else if(selectedFactionID==i) TurnControl.EndTurn();
					}
					break;
				}
			}
			
			//if there's only 1 faction remain (since faction with no active unit will be removed), then faction has won the game
			if(factionList.Count==1) GameControl.GameOver(factionList[0].ID);
		}
Ejemplo n.º 26
0
		public void _OnCursorDown(int cursorID=-1){
			//Debug.Log("_OnCursorDown");
			
			if(hoveredTile==null) return;
			
			if(TBTK.IsCursorOnUI(cursorID)) return;
			
			if(!TurnControl.ClearToProceed()) return;
			
			if(GameControl.GetGamePhase()==_GamePhase.Over) return;
			
			if(GameControl.GetGamePhase()==_GamePhase.UnitDeployment){
				if(hoveredTile.unit==null) FactionManager.DeployUnitOnTile(hoveredTile);
				else if(hoveredTile.unit!=null) FactionManager.UndeployUnit(hoveredTile.unit);
				return;
			}
			
			if(AbilityManager.InTargetMode()){
				Debug.Log("AbilityTargetSelected");
				targetModeTargetSelected(hoveredTile);
			}
			else OnTile(hoveredTile);
		}
Ejemplo n.º 27
0
        //called by FactionManager.SelectNextFaction in FactionPerTurn mode (resetSelectedID=true)
        public void NewTurn(bool processUnit = false)
        {
            selectedUnitID = -1;
            movedUnitList  = new List <Unit>();

            if (abilityInfo != null)
            {
                abilityInfo.energy = Mathf.Min(abilityInfo.energy + abilityInfo.energyGainPerTurn, abilityInfo.energyFull);
            }

            if (processUnit)
            {
                for (int i = 0; i < allUnitList.Count; i++)
                {
                    allUnitList[i].NewTurn();
                }
            }

            if (TurnControl.GetTurnMode() == _TurnMode.UnitPerTurn)
            {
                return;
            }

            if (TurnControl.GetMoveOrder() == _MoveOrder.Random)
            {
                //random order, shuffle the unit order
                List <Unit> newList = new List <Unit>();
                while (allUnitList.Count > 0)
                {
                    int rand = Random.Range(0, allUnitList.Count);
                    newList.Add(allUnitList[rand]);
                    allUnitList.RemoveAt(rand);
                }
                allUnitList = newList;
            }
        }
Ejemplo n.º 28
0
        IEnumerator ActionCamRoutine(Unit srcUnit, Vector3 tgtPos)
        {
            TurnControl.ActionCommenced();

            actionCamInProgress = true;

            Vector3    rootPosOrigin = thisT.position;
            Quaternion rootRotOrigin = thisT.rotation;

            Vector3 posOrigin  = camT.localPosition;
            float   zoomOrigin = camT.localPosition.z;

            Vector3 centerPos = srcUnit.GetTargetT().position + (tgtPos - srcUnit.GetTargetT().position) * 0.35f;

            centerPos.y = thisT.position.y;

            Quaternion wantedRot = Quaternion.LookRotation(centerPos - srcUnit.GetTargetT().position);

            wantedRot *= Quaternion.Euler(10, 15, 0);
            wantedRot  = Quaternion.Euler(wantedRot.eulerAngles.x, wantedRot.eulerAngles.y, 0);

            float dist = Vector3.Distance(centerPos, srcUnit.GetTargetT().position);

            float duration = 0;

            while (duration < 1)
            {
                thisT.rotation = Quaternion.Euler(thisT.rotation.eulerAngles.x, thisT.rotation.eulerAngles.y, 0);

                camT.localPosition = Vector3.Lerp(new Vector3(0, 0, zoomOrigin), new Vector3(0, 0, -dist - GridManager.GetTileSize() * 2), duration);
                thisT.position     = Vector3.Lerp(rootPosOrigin, centerPos, duration);
                thisT.rotation     = Quaternion.Lerp(rootRotOrigin, wantedRot, duration);

                duration += Time.deltaTime * 1f;
                yield return(null);
            }
            thisT.rotation = wantedRot;

            TurnControl.ActionCompleted();

            yield return(null);

            while (!TurnControl.ClearToProceed())
            {
                yield return(null);
            }

            TurnControl.ActionCommenced();

            duration = 0;
            while (duration < 1)
            {
                thisT.rotation = Quaternion.Euler(thisT.rotation.eulerAngles.x, thisT.rotation.eulerAngles.y, 0);

                camT.localPosition = Vector3.Lerp(new Vector3(0, 0, -dist - GridManager.GetTileSize() * 2), new Vector3(0, 0, zoomOrigin), duration);
                thisT.position     = Vector3.Lerp(centerPos, rootPosOrigin, duration);
                thisT.rotation     = Quaternion.Lerp(wantedRot, rootRotOrigin, duration);

                duration += Time.deltaTime * 1f;
                yield return(null);
            }

            thisT.position     = rootPosOrigin;
            thisT.rotation     = rootRotOrigin;
            camT.localPosition = posOrigin;

            TurnControl.ActionCompleted();

            actionCamInProgress = false;
        }
Ejemplo n.º 29
0
        public void Init()
        {
            if (init)
            {
                return;
            }
            init = true;

            thisT = transform;

            Transform deploymentIndicator = null;
            Transform moveableIndicator   = null;
            Transform hostileIndicator    = null;
            Transform abRangeIndicator    = null;
            Transform abTargetIndicator   = null;

            if (GridManager.GetTileType() == _TileType.Hex)
            {
                deploymentIndicator = hexDeployment;
                moveableIndicator   = hexMoveable;
                hostileIndicator    = hexHostile;
                abRangeIndicator    = hexAbRange;
                abTargetIndicator   = hexAbTarget;
                cursorIndicatorF    = (Transform)Instantiate(hexCursorF);
                cursorIndicatorH    = (Transform)Instantiate(hexCursorH);
                selectIndicator     = (Transform)Instantiate(hexSelected);
            }
            else if (GridManager.GetTileType() == _TileType.Square)
            {
                deploymentIndicator = sqDeployment;
                moveableIndicator   = sqMoveable;
                hostileIndicator    = sqHostile;
                abRangeIndicator    = sqAbRange;
                abTargetIndicator   = sqAbTarget;
                cursorIndicatorF    = (Transform)Instantiate(sqCursorF);
                cursorIndicatorH    = (Transform)Instantiate(sqCursorH);
                selectIndicator     = (Transform)Instantiate(sqSelected);
            }


            cursorIndicatorF.parent = thisT;
            cursorIndicatorH.parent = thisT;
            selectIndicator.parent  = thisT;


            for (int i = 0; i < 20; i++)
            {
                deploymentIndicatorList.Add((Transform)Instantiate(deploymentIndicator));
                deploymentIndicatorList[i].gameObject.SetActive(false);
                deploymentIndicatorList[i].parent = thisT;
            }
            for (int i = 0; i < 20; i++)
            {
                moveableIndicatorList.Add((Transform)Instantiate(moveableIndicator));
                moveableIndicatorList[i].gameObject.SetActive(false);
                moveableIndicatorList[i].parent = thisT;
            }
            for (int i = 0; i < 10; i++)
            {
                hostileIndicatorList.Add((Transform)Instantiate(hostileIndicator));
                hostileIndicatorList[i].gameObject.SetActive(false);
                hostileIndicatorList[i].parent = thisT;
            }

            for (int i = 0; i < 20; i++)
            {
                abRangeIndicatorList.Add((Transform)Instantiate(abRangeIndicator));
                abRangeIndicatorList[i].gameObject.SetActive(false);
                abRangeIndicatorList[i].parent = thisT;
            }
            for (int i = 0; i < 20; i++)
            {
                abTargetIndicatorList.Add((Transform)Instantiate(abTargetIndicator));
                abTargetIndicatorList[i].gameObject.SetActive(false);
                abTargetIndicatorList[i].parent = thisT;
            }


            if (TurnControl.GetTurnMode() == _TurnMode.FactionPerTurn)
            {
                //create the moved indicator
                for (int i = 0; i < 10; i++)
                {
                    movedIndicatorList.Add((Transform)Instantiate(movedIndicator));
                    movedIndicatorList[i].gameObject.SetActive(false);
                    movedIndicatorList[i].parent = thisT;
                }
            }


            if (GameControl.EnableFogOfWar())
            {
                Transform fogObj = null;

                if (GridManager.GetTileType() == _TileType.Hex)
                {
                    fogObj = hexFogObj;
                }
                else if (GridManager.GetTileType() == _TileType.Square)
                {
                    fogObj = sqFogObj;
                }

                List <Tile> tileList = GridManager.GetTileList();
                for (int i = 0; i < tileList.Count; i++)
                {
                    //if(!tileList[i].walkable) continue;
                    tileList[i].SetFogOfWarObj((Transform)Instantiate(fogObj));
                }
            }

            if (GameControl.EnableCover())
            {
                float scaleOffset = GridManager.GetTileType() == _TileType.Hex ? 0.5f : 0.8f;
                float tileSize    = GridManager.GetTileSize();

                for (int i = 0; i < 5; i++)
                {
                    coverOverlayHList.Add((Transform)Instantiate(coverOverlayH));
                    coverOverlayFList.Add((Transform)Instantiate(coverOverlayF));

                    coverOverlayHList[i].localScale *= tileSize * scaleOffset;
                    coverOverlayHList[i].parent      = thisT;
                    coverOverlayHList[i].gameObject.SetActive(false);

                    coverOverlayFList[i].localScale *= tileSize * scaleOffset;
                    coverOverlayFList[i].parent      = thisT;
                    coverOverlayFList[i].gameObject.SetActive(false);
                }
            }
        }
Ejemplo n.º 30
0
        private bool movingUnit = false;                //set to true when a unit is being moved
        IEnumerator MoveUnitRoutine(Unit unit, _AIMode activeMode)
        {
            TBTK.OnUnitSelected(unit);

            movingUnit = true;
            //Debug.Log("moving unit");
            if (activeMode != _AIMode.Aggressive && !unit.trigger)
            {
                AIDebug("unit " + unit.gameObject.name + " is not triggered");
                if (!untriggeredUnitMove)
                {
                    StartCoroutine(EndMoveUnitRoutine());
                    unit.moveRemain = 0;      unit.attackRemain = 0;
                }
                else
                {
                    if (Random.value < 0.5f)
                    {
                        StartCoroutine(EndMoveUnitRoutine());
                        unit.moveRemain = 0;      unit.attackRemain = 0;
                    }
                    else
                    {
                        AIDebug("Randomly move unit " + unit.gameObject.name + " anyway");
                        List <Tile> walkableTilesInRange = GridManager.GetTilesWithinDistance(unit.tile, Mathf.Min(1, unit.GetEffectiveMoveRange() / 2), true);
                        if (walkableTilesInRange.Count > 0)
                        {
                            unit.Move(walkableTilesInRange[Random.Range(0, walkableTilesInRange.Count)]);
                        }
                    }
                }
                AIDebug("End unit " + unit.gameObject.name + " turn");
                StartCoroutine(EndMoveUnitRoutine());
                yield break;
            }

            Tile targetTile = Analyse(unit, activeMode);


            if (CameraControl.CenterOnSelectedUnit())
            {
                bool visible = false;
                if (unit.tile.IsVisible())
                {
                    visible = true;
                }
                else if (targetTile != unit.tile)
                {
                    List <Tile> path = unit.GetPathForAI(targetTile);
                    for (int i = 0; i < path.Count; i++)
                    {
                        if (path[i].IsVisible())
                        {
                            visible = true;
                            break;
                        }
                    }
                    targetTile = path[path.Count - 1];
                    Debug.DrawLine(unit.tile.GetPos(), targetTile.GetPos(), Color.red, 2);
                }

                if (visible)
                {
                    CameraControl.OnUnitSelected(unit, false);
                    while (CameraControl.IsLerping())
                    {
                        yield return(null);
                    }
                }
            }


            //first move to the targetTile
            if (targetTile != unit.tile)
            {
                unit.Move(targetTile);
                yield return(new WaitForSeconds(.1f));                  //wait until the unit has moved into the targetTile

                while (!TurnControl.ClearToProceed())
                {
                    //AIDebug("waiting, unit "+unit.gameObject.name+" is moving");
                    AIDebug("waiting while unit is moving");
                    yield return(null);
                }
            }

            if (unit == null || unit.HP <= 0)           //in case unit is destroyed by overwatch
            {
                StartCoroutine(EndMoveUnitRoutine());
                yield break;
            }

            for (int i = 0; i < targetTile.hostileInRangeList.Count; i++)
            {
                if (targetTile.hostileInRangeList[i].unit == null || targetTile.hostileInRangeList[i].unit.factionID == unit.factionID)
                {
                    targetTile.hostileInRangeList.RemoveAt(i);              i -= 1;
                }
            }

            //if there's hostile within range, attack it
            if (targetTile.hostileInRangeList.Count > 0)
            {
                if (unit.CanAttack())
                {
                    AIDebug("waiting, unit " + unit.gameObject.name + " is attacking");

                    //~ if(targetTile!=unit.tile){	//wait until the unit has moved into the targetTile
                    //~ yield return new WaitForSeconds(.25f);
                    //~ while(!TurnControl.ClearToProceed()) yield return null;
                    //~ }

                    int rand = Random.Range(0, targetTile.hostileInRangeList.Count);
                    unit.Attack(targetTile.hostileInRangeList[rand].unit);
                }
                else
                {
                    if (unit.moveRemain > 0)
                    {
                        unit.moveRemain -= 1;
                    }
                }
            }
            else
            {
                if (unit.moveRemain <= 0)
                {
                    unit.attackRemain = 0;
                }
            }

            AIDebug("End unit " + unit.gameObject.name + " turn");
            StartCoroutine(EndMoveUnitRoutine());

            yield return(null);
        }