/// <summary> /// Changes state machine to a new state. /// </summary> /// <param name="newState">New state.</param> public void ChangeState(TowerStates newState) { if (actualState == TowerStates.Dead || newState == actualState) { return; } if (actualState != newState) { actualState = newState; switch (actualState) { case TowerStates.Idle: break; case TowerStates.Attacking: shooterCounter = shootDelay; break; case TowerStates.Dead: break; } GameController.instance.CheckEndGame(); } }
// Use this for initialization void Start() { //initialize the state machine and set it to the first state states = TowerStates.Idle; playerTrans = GameObject.FindGameObjectWithTag("Player").transform; }
private void TransitionToCharging() { targetRingRotationSpeed = 10f; State = TowerStates.Charging; TowerAnimator.SetBool("Open", true); }
private void TransitionToDead() { TowerAnimator.SetBool("Open", false); State = TowerStates.Dead; }
private void TransitionToFiring() { targetRingRotationSpeed = 1f; State = TowerStates.Firing; TowerAnimator.SetBool("Open", false); }
// Update is called once per frame void Update() { switch (states) { case TowerStates.Idle: { //Check for minions in the scene GameObject[] enemies = GameObject.FindGameObjectsWithTag(minionTag); float shortestDistance = Mathf.Infinity; GameObject nearestEnemy = null; foreach (GameObject enemy in enemies) { //convert the distance of the enemy into a float float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position); //if their distance is within range if (distanceToEnemy < shortestDistance) { //assign the first game object into the array as priority shortestDistance = distanceToEnemy; nearestEnemy = enemy; } } //if a object has been assigned if (nearestEnemy != null && shortestDistance <= range) { //change state making the same for check, and deal damage to minion target = nearestEnemy.transform; states = TowerStates.MinionAttack; } else { target = null; } var myplayer = GameObject.Find("Player"); if (myplayer != null) { playerDistance = Vector3.Distance(playerTrans.position, transform.position); if (playerDistance < ViewDistance) { states = TowerStates.PlayerAttack; } } if (myplayer == null) { Debug.Log("Player not in range"); } Debug.Log("Waiting for minion"); break; } case TowerStates.MinionAttack: { Debug.Log("Attacking Minion"); GameObject[] enemies = GameObject.FindGameObjectsWithTag(minionTag); float shortestDistance = Mathf.Infinity; GameObject nearestEnemy = null; foreach (GameObject enemy in enemies) { float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position); if (distanceToEnemy < shortestDistance) { shortestDistance = distanceToEnemy; nearestEnemy = enemy; } } if (nearestEnemy != null && shortestDistance <= range) { nearestEnemy.GetComponent <MinionHealth>().health -= 5; target = nearestEnemy.transform; } else { target = null; states = TowerStates.Idle; } break; } case TowerStates.PlayerAttack: { var myplayer = GameObject.Find("Player"); if (myplayer) { playerDistance = Vector3.Distance(playerTrans.position, transform.position); if (playerDistance < ViewDistance) { myplayer.GetComponent <PlayerHealth>().health -= 5f; Debug.Log("Attacking Player"); } } if (!myplayer) { Debug.Log("Player not in range"); } states = TowerStates.Idle; break; } } }
// Update is called once per frame void Update() { switch (states) { case TowerStates.Idle: { GameObject[] enemies = GameObject.FindGameObjectsWithTag(minionTag); float shortestDistance = Mathf.Infinity; GameObject nearestEnemy = null; foreach (GameObject enemy in enemies) { float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position); if (distanceToEnemy < shortestDistance) { shortestDistance = distanceToEnemy; nearestEnemy = enemy; } } if (nearestEnemy != null && shortestDistance <= range) { target = nearestEnemy.transform; states = TowerStates.MinionAttack; } else { target = null; } Debug.Log("Waiting for minion"); break; } case TowerStates.MinionAttack: { Debug.Log("Attacking Minion"); GameObject[] enemies = GameObject.FindGameObjectsWithTag(minionTag); float shortestDistance = Mathf.Infinity; GameObject nearestEnemy = null; foreach (GameObject enemy in enemies) { float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position); if (distanceToEnemy < shortestDistance) { shortestDistance = distanceToEnemy; nearestEnemy = enemy; } } if (nearestEnemy != null && shortestDistance <= range) { nearestEnemy.GetComponent <MinionHealth>().health -= 5; target = nearestEnemy.transform; } else { target = null; states = TowerStates.Idle; } break; } case TowerStates.PlayerAttack: { break; } } }
// Use this for initialization void Start() { //initialize the state machine and set it to the first state states = TowerStates.Idle; }
/// <summary> /// Changes state machine to a new state. /// </summary> /// <param name="newState">New state.</param> public void ChangeState(TowerStates newState) { if (actualState == TowerStates.Dead || newState == actualState) return; if (actualState != newState) { actualState = newState; switch (actualState) { case TowerStates.Idle: break; case TowerStates.Attacking: shooterCounter = shootDelay; break; case TowerStates.Dead: break; } GameController.instance.CheckEndGame(); } }