public IEnumerator updateOwnUnits() { // We loop through the unit 1 by 1! foreach (GameObject enemyUnitGO in m_enemyGOList) { // Wait for the some time before the AI make its move yield return(timeForDelayAI); // Activate the unit range ring! CharacterScript charStat = enemyUnitGO.GetComponent <CharacterScript>(); charStat.unitRangeRingGO.SetActive(true); // Get the state machine! UnitFSM enemyUnitFSM = enemyUnitGO.GetComponent <UnitFSM>(); // Interact with the state to inform it to move towards the player fortress! enemyUnitFSM.GetGenericState("MoveState").interactWithState("PLAYERFORTRESS"); enemyUnitFSM.ChangeCurrentState("MoveState"); // Wait till it finishes updating! while (enemyUnitFSM.updateStateCoroutine != null) { yield return(null); } charStat.unitRangeRingGO.SetActive(false); } // When everything is done, then switch the turn at GameManager! GameManager.Instance.isItPlayerTurn = true; yield break; }
void Idle() { StateTime += Time.deltaTime; if (StateTime > idleDelay) { StateTime = 0.0f; if (target && !target.GetComponent<ObjectStatus>().IsDead()) { LookEnemy(); var dist = Vector2.Distance(target.transform.position, transform.position); if (dist < objInfo.AttackRange) { StateTime = -backDelayTime; State = UnitFSM.ATTACK; StartCoroutine("AttackDelay", backDelayTime); } else { State = UnitFSM.MOVE; anim.SetTrigger("move"); } } else { SearchEnemy(); } } }
protected override bool OnStartState() { UnitFSM fSM = currentUnit.fSM as UnitFSM; fSM.ForceChangeState(UnitFSM.UnitStates.MOVE); return(true); }
//----------------------------------------------------------------------------------- // handler functions void OnDisable() { StopAllCoroutines(); State = UnitFSM.IDLE; backDelayTime = 0.0f; target = null; GameManager.instance.playerObjList.Remove(gameObject); }
public UnitController(Unit u, GameplayManager gm) { controlledUnit = u; this.gm = gm; u.Controller = this; fsm = new UnitFSM(this, gm); SetSteering(new StandStill(gm, u)); }
protected override bool OnEndState() { ForceQuit = false; isInit = false; FSM.GetUnitOwner().SetCurrentNode(null); FSM.GetUnitOwner().Die(); FSM = null; return(true); }
private bool Move(Unit u, UnitFSM fsm) { int steps = u.Stats.MoveRange;// Random.Range(1, u.Stats.MoveRange); if (steps == 0) { return(false); } u.CurrentPath = RecalculatePathToSteps(u.CurrentPath, u); fsm.ForceChangeState(UnitFSM.UnitStates.MOVE); isMoving = true; return(true); }
private bool Attack(Unit u, UnitFSM fsm) { Unit enemy = null; if (CheckEnemyInRange(u, out enemy)) { u.CurrentEnemy = enemy; fsm.ForceChangeState(UnitFSM.UnitStates.ATTACK); isAttacking = true; Debug.Log("Enemy attacked"); return(true); } return(false); }
public override bool ExecuteState(FSM fsm) { FSM = fsm as UnitFSM; if (!isInit) { isInit = OnStartState(); } if (OnExecuteState() || ForceQuit) { return(OnEndState()); } return(false); }
public void changeState(UnitFSM new_state) { state = new_state; switch (new_state) { case UnitFSM.Idle: if(gameObject.GetComponent<idle_script>() == null) { idle = gameObject.AddComponent<idle_script>(); } DestroyImmediate(seek); DestroyImmediate(attack); break; case UnitFSM.Seek: if (gameObject.GetComponent<seek_script>() == null) { seek = gameObject.AddComponent<seek_script>(); } DestroyImmediate(idle); DestroyImmediate(attack); break; case UnitFSM.Attack: if (gameObject.GetComponent<attack_script>() == null) { attack = gameObject.AddComponent<attack_script>(); } DestroyImmediate(seek); DestroyImmediate(idle); break; } }
public override bool ExecuteState(FSM fsm) { if (!isInit) { FSM = fsm as UnitFSM; isInit = OnStartState(); } if (OnExecuteState() || ForceQuit) { if (ForceQuit) { ((UnitFSM)fsm).ChangeState(((UnitFSM)fsm).ForcedState); } return(OnEndState()); } return(false); }
public override bool ExecuteState(FSM fsm) { if (!isInit) { fSM = fsm as UnitFSM; unit = ((UnitFSM)fsm).GetUnitOwner(); isInit = OnStartState(); } path = unit.CurrentPath; if (path != null) { if (OnExecuteState() == true) { return(OnEndState()); } } return(false); }
private bool ManageUnitTurn(Unit u) { UnitFSM fsm = u.fSM as UnitFSM; if (!isAttacking && !isMoving) { if (u.CurrentPath != null) { Move(u, fsm); } if (!Attack(u, fsm)) { if (!isPathRequested) { isPathRequested = RequestPathForUnit(u); } } } else { if (u.HasAttacked) { isAttacking = false; return(true); } if (u.HasMoved && !isAttacking) { isMoving = false; u.CurrentPath = null; if (!Attack(u, fsm)) { return(true); } } } return(false); }
public override bool ExecuteState(FSM fsm) { FSM = fsm as UnitFSM; target = FSM.GetUnitOwner().CurrentEnemy; if (!isInit) { UnitFSM targetFSM = target.fSM as UnitFSM; targetFSM.ForceChangeState(UnitFSM.UnitStates.ATTACKED); isInit = OnStartState(); } if (OnExecuteState() || ForceQuit) { return(OnEndState()); } return(false); }
public void Attacked() { StopCoroutine("AttackDelay"); body.velocity = Vector2.zero; StateTime = 0.0f; State = UnitFSM.HIT; anim.SetTrigger("hit"); }
public IEnumerator updateOwnUnits() { // Clone the list! m_playerNotInteractGOList = new List <GameObject>(m_playerGOList); yield return(new WaitForSeconds(1.0f)); // If there is a previous unit, move the camera towards there! if (m_lastActionUnitTile) { CameraMovement.Instance.MoveTowardsPosition(m_lastActionUnitTile.transform.position); } else if (m_playerGOList.Count > 0) { // Randomly move the camera to any of the objects! CameraMovement.Instance.MoveTowardsPosition(m_playerGOList[0].transform.position); } while (m_playerNotInteractGOList.Count > 0) { CameraMovement.Instance.BeginCamFreeMovement(); playerMouseInput.enabled = true; // Will wait every frame for the player to click on the tile which belongs to the player! Also making sure that the player has not interact with the tile before! while (playerMouseInput.playerClickedTile == null || playerMouseInput.playerClickedTile.tag != "Player" || (!m_playerNotInteractGOList.Contains(playerMouseInput.playerClickedTile.gameObject) && playerMouseInput.playerClickedTile.tag == "Player")) { yield return(null); } CameraMovement.Instance.StopCamUpdateMovement(); TileScript firstTileClicked = playerMouseInput.playerClickedTile; // then set the green indicator! playerMouseInput.SetUnitIndicatorPermanent(firstTileClicked.transform); // Set it to null and prevent player from pressing! playerMouseInput.playerClickedTile = null; playerMouseInput.enabled = false; CharacterScript playerUnitStat = firstTileClicked.GetComponent <CharacterScript>(); UnitStatWindowManager.gameObject.SetActive(true); UnitStatWindowManager.DisplayUI(playerUnitStat); m_hasFinishedConversing = false; // set the motivation to be true then wait for it to be inactive! //displayMotivationScreen.SetActive(true); //while (displayMotivationScreen.activeSelf) // yield return null; //string talkToWhatChar = playerUnitStat.m_AttackType.ToString() + "|" + playerUnitStat.m_characterCharis.ToString(); // Try to talk to the character //theConversationChart.SendFungusMessage(talkToWhatChar); theConversationChart.SetIntegerVariable(m_MotivationVarStr, playerUnitStat.m_Motivation); theConversationChart.SetStringVariable(m_AttckTypeVarStr, playerUnitStat.m_AttackType.ToString()); theConversationChart.SendFungusMessage(m_MotivateDialogueStr); while (!m_hasFinishedConversing) { yield return(null); } UnitStatWindowManager.gameObject.SetActive(false); // Then we wait till the next tile that the player clicked on or maybe there is none! UnitFSM playerFSM = firstTileClicked.GetComponent <UnitFSM>(); yield return(null); playerUnitStat.m_Motivation = theConversationChart.GetIntegerVariable(m_MotivationVarStr); // Wait till the player clicked on a tile and it turns out to be the enemy or player clicked on the background and nothing is selected forsure! m_clickedFlag = false; //if (playerFSM.GetGenericState("DemoralizeState").interactWithState(m_PlayerChoseChar)) if (playerFSM.GetGenericState("DemoralizeState").interactWithState(null)) { // Only Successful interaction will mean being able to move the unit! m_endPlayerTurnScreen.SetActive(true); playerUnitStat.unitRangeRingGO.SetActive(true); } playerMouseInput.enabled = true; // Wait for next frame yield return(null); playerMouseInput.playerClickedTile = null; #region UNIT_ACTION while (m_endPlayerTurnScreen.activeSelf) { CameraMovement.Instance.BeginCamFreeMovement(); // Wait for PlayerBattleMouse to send the event trigger! while (!m_clickedFlag && m_endPlayerTurnScreen.activeSelf) { if (playerMouseInput.playerClickedTile && (playerMouseInput.playerClickedTile.tag == "Enemy" || playerMouseInput.playerClickedTile.tag == "EnemyFortress") && playerUnitStat.m_AttackType != CharacterScript.TYPE_ATTACK.HEAL) { // check whether the player is close to the enemy! if (playerUnitStat.m_Range >= Vector3.Distance(playerUnitStat.transform.position, playerMouseInput.playerClickedTile.transform.position)) { CharacterScript otherCharStat = playerMouseInput.playerClickedTile.GetComponent <CharacterScript>(); playerFSM.GetGenericState("AttackState").interactWithState(otherCharStat); playerFSM.ChangeCurrentState("AttackState"); // If Player attack, this means the unit turn has ended m_endPlayerTurnScreen.SetActive(false); break; } else { playerMouseInput.playerClickedTile = null; } } else if (playerMouseInput.playerClickedTile && playerMouseInput.playerClickedTile.tag == "Player" && playerUnitStat.m_AttackType == CharacterScript.TYPE_ATTACK.HEAL) { // check whether the player is close to allies! if (playerUnitStat.m_Range >= Vector3.Distance(playerUnitStat.transform.position, playerMouseInput.playerClickedTile.transform.position)) { CharacterScript otherCharStat = playerMouseInput.playerClickedTile.GetComponent <CharacterScript>(); playerFSM.GetGenericState("AttackState").interactWithState(otherCharStat); playerFSM.ChangeCurrentState("AttackState"); // If Player attack, this means the unit turn has ended m_endPlayerTurnScreen.SetActive(false); break; } else { playerMouseInput.playerClickedTile = null; } } yield return(null); } // Making sure the player clicked on an empty tile and their available movement tiles are more than 0. if (!hasUIBlockRaycast && !playerMouseInput.playerClickedTile && playerUnitStat.m_leftOverMoveSpeed > 0) { // If successful interaction, then will move! // Player pressed nothing, so move towards there! if (playerFSM.GetGenericState("MoveState").interactWithState(playerMouseInput.playerMouseLastClickedPos)) { playerFSM.ChangeCurrentState("MoveState"); // Then wait for the FSM to be finished updating! yield return(playerFSM.updateStateCoroutine); } } //else { // Reset the tile! playerMouseInput.playerClickedTile = null; } CameraMovement.Instance.StopCamUpdateMovement(); m_clickedFlag = false; yield return(null); } #endregion // Unblock the raycast! hasUIBlockRaycast = false; playerMouseInput.SetUnitIndicatorPermanent(null); if (firstTileClicked) { playerUnitStat.resetMoveSpeed(); // Removed the already interacted gameobject! m_playerNotInteractGOList.Remove(firstTileClicked.gameObject); m_lastActionUnitTile = firstTileClicked; firstTileClicked.GetComponent <SpriteRenderer>().color = colorOfUsedUnit; playerUnitStat.unitRangeRingGO.SetActive(false); } else { RemoveDestroyedUnit(); } } CameraMovement.Instance.StopCamUpdateMovement(); // Set it to be false! GameManager.Instance.isItPlayerTurn = false; // Then change all of the unit color back to normal foreach (GameObject unitGO in m_playerGOList) { SpriteRenderer unitSprRender = unitGO.GetComponent <SpriteRenderer>(); unitSprRender.color = Color.white; } yield break; }
void Move() { if (target && !target.GetComponent<ObjectStatus>().IsDead()) { StateTime += Time.deltaTime; if (StateTime > moveDelay) { StateTime = 0.0f; SearchEnemy(); LookEnemy(); var dist = Vector2.Distance(target.transform.position, transform.position); if (dist < objInfo.AttackRange) { StateTime = -backDelayTime; State = UnitFSM.ATTACK; StartCoroutine("AttackDelay", backDelayTime); return; } if (anim.GetCurrentAnimatorStateInfo(0).IsName("Base Layer.walk") == false) { anim.SetTrigger("move"); } float speed = objInfo.MoveSpeed * (float)objInfo.Dir; body.velocity = new Vector2(speed, body.velocity.y); } } else { StateTime = UnityEngine.Random.Range(-0.1f, 0.1f); State = UnitFSM.IDLE; anim.SetTrigger("idle"); target = null; } }
void Attack() { if (target && !target.GetComponent<ObjectStatus>().IsDead()) { var dist = Vector2.Distance(target.transform.position, transform.position); if (dist > objInfo.AttackRange) { StopCoroutine("AttackDelay"); StateTime = 0.0f; State = UnitFSM.IDLE; anim.SetTrigger("idle"); return; } StateTime += Time.deltaTime; if (StateTime > objInfo.AttackFrontDelay) { LookEnemy(); AttackProcess(); backDelayTime = objInfo.AttackBackDelay + UnityEngine.Random.Range(0.0f, 0.2f); StateTime = -(backDelayTime); StartCoroutine("AttackDelay", backDelayTime); } } else { StateTime = UnityEngine.Random.Range(-0.1f, 0.1f); State = UnitFSM.IDLE; anim.SetTrigger("idle"); target = null; } }
void Hit() { StateTime += Time.deltaTime; if (StateTime > hitDelay) { StateTime = UnityEngine.Random.Range(-0.1f, 0.1f); State = UnitFSM.IDLE; } }
public void Death() { StopCoroutine("AttackDelay"); StateTime = 0.0f; State = UnitFSM.DEAD; anim.SetTrigger("death"); }