// Update is called once per frame void Update() { #if UNITY_EDITOR if (currentTile == null) { Debug.Log(this.actorName + " no current tile"); } #endif //Updating the UI if (barScript != null) { if (fear < fearSpookedAmount) { fearStatus = "Normal"; } else if (fear >= fearSpookedAmount && fear < fearPanicAmount) { fearStatus = "Spooked"; } else if (fear >= fearPanicAmount && fear < 100) { fearStatus = "PANIC!"; } else if (fear >= 100) { fearStatus = "FAINTED!!!"; } barScript.SetAmount(fear * 0.01f, fearStatus); } //Updating state for the player if (fear > fearPanicAmount) { isPanicking = true; } else { isPanicking = false; } if (fear >= 100 && state != ACTOR_STATE.FIENTED) { PlaySound(4, 0.0f); string randoFall = "Fall 0" + Random.Range(1, 4); gameObject.GetComponent <Animator>().Play(randoFall); state = ACTOR_STATE.FIENTED; } //Always decrease fear if (fear > 0) { fear -= Time.deltaTime * fearReductionPerSecond; } //Lower limit is zero fear = Mathf.Max(fear, 0); if (state == ACTOR_STATE.FIENTED) { if (fear < 80) { roamingWaitTime = Random.Range(minRoamingWaitTime, maxRoamingWaitTime); roamingT = 0f; state = ACTOR_STATE.CHOOSING; } } else if (state == ACTOR_STATE.MOVING) { if (talkRefreshTimer > 0) { talkRefreshTimer -= Time.deltaTime; if (talkRefreshTimer < 0) { talkRefreshTimer = 0; } } if (GetActorFearState() == LevelController.ACTOR_STATES.NORMAL && !forceDirection) { gameObject.GetComponent <Animator>().Play("Walk 01"); } else { gameObject.GetComponent <Animator>().Play("Run 01"); } movementT += Time.deltaTime * GetSpeed(); if (movementT < 1.0f) { if (currentTile != null && movementTarget != null) { this.transform.position = Vector3.Lerp(currentTile.characterPosition, movementTarget.characterPosition, movementT); } else { Debug.LogError("Movement attempt without start or end tile"); } } else { this.transform.position = movementTarget.characterPosition; state = ACTOR_STATE.GETNEXTTILE; } } else if (state == ACTOR_STATE.GETNEXTTILE) { if (talkRefreshTimer > 0) { talkRefreshTimer -= Time.deltaTime; if (talkRefreshTimer < 0) { talkRefreshTimer = 0; } } previousTile = currentTile; currentTile = movementTarget; movementT = 0; currentTile.occupant = this.gameObject; //Checking if the tile is marked with something; //This will set Processed state //If none then next operation is talking or continue moving if panicked CheckTileState(); switch (processedState) { case ACTOR_STATE.NONE: if (forceDirection) { //forcing actor to move in a direction, if failing forcedirection will be setted to false TryForceActorToNextTile(); if (forceDirection) { transform.LookAt(movementTarget.characterPosition, Vector3.up); state = ACTOR_STATE.MOVING; } else { this.GetComponent <Animator>().Play("Run 06 Stop"); roamingWaitTime = Random.Range(minRoamingWaitTime, maxRoamingWaitTime); roamingT = 0f; currIdleAnimationPlaying = Random.Range(1, 5); state = ACTOR_STATE.CHOOSING; } } else { //can talk or continue to move if (talkTarget != null && !isPanicking && Mathf.Approximately(talkRefreshTimer, 0f)) { this.GetComponent <Animator>().Play(talkAnimation); talkCooldownTimer = talkCooldown; talkRefreshTimer = talkRefresh; state = ACTOR_STATE.TALKING; } else { if (isPanicking) { talkRefreshTimer = talkRefresh; talkCooldownTimer = talkCooldown; ResetTalkingActor(); } movementTarget = pathHelper.GetNextMove(); if (movementTarget == null) { //string str = "Idle 0"+Random.Range(1,5); //Debug.Log(str); //gameObject.GetComponent<Animator>().Play(str); roamingWaitTime = Random.Range(minRoamingWaitTime, maxRoamingWaitTime); roamingT = 0f; currIdleAnimationPlaying = Random.Range(1, 5); state = ACTOR_STATE.CHOOSING; } else { transform.LookAt(movementTarget.characterPosition, Vector3.up); state = ACTOR_STATE.MOVING; } } } break; case ACTOR_STATE.HAUNTED: if (forceDirection) { //forcing actor to move in a direction, if failing forcedirection will be setted to false TryForceActorToNextTile(); if (forceDirection) { transform.LookAt(movementTarget.characterPosition, Vector3.up); state = ACTOR_STATE.MOVING; } else { roamingWaitTime = Random.Range(minRoamingWaitTime, maxRoamingWaitTime); roamingT = 0f; currIdleAnimationPlaying = Random.Range(1, 5); state = ACTOR_STATE.CHOOSING; } } break; case ACTOR_STATE.CHILLED: scaredAnimation = "Scared 0" + Random.Range(1, 3); GameObject player = GameObject.FindGameObjectWithTag("Player"); if (player) { Chill c = player.GetComponent <Chill>(); if (c) { c.AddActorToCurrentChill(this); } } state = ACTOR_STATE.CHILLED; break; default: break; } } else if (state == ACTOR_STATE.CHOOSING) { roamingT += Time.deltaTime; CheckTileState(); switch (processedState) { case ACTOR_STATE.NONE: //If someone is asking me to talk do that if (talkTarget != null && !isPanicking) { this.GetComponent <Animator>().Play(talkAnimation); talkCooldownTimer = talkCooldown; state = ACTOR_STATE.TALKING; } //playing idle animation when choosing else if (roamingT < roamingWaitTime) { LevelController.ACTOR_STATES fearState = GetActorFearState(); if (fearState == LevelController.ACTOR_STATES.NORMAL) { string str = "Idle 0" + currIdleAnimationPlaying.ToString(); //Debug.Log(str); gameObject.GetComponent <Animator>().Play(str); } if (fearState == LevelController.ACTOR_STATES.SPOOKED) { gameObject.GetComponent <Animator>().Play("Scared 01"); } if (fearState == LevelController.ACTOR_STATES.PANICKED) { gameObject.GetComponent <Animator>().Play("Scared 02"); } } else { roamingT = 0; pathHelper.RequestNewRandomPath(currentTile); movementTarget = pathHelper.GetNextMove(); if (movementTarget == null) { roamingWaitTime = Random.Range(minRoamingWaitTime, maxRoamingWaitTime); roamingT = 0f; currIdleAnimationPlaying = Random.Range(1, 5); state = ACTOR_STATE.CHOOSING; } else { transform.LookAt(movementTarget.characterPosition, Vector3.up); currentTile.occupant = null; state = ACTOR_STATE.MOVING; } } break; case ACTOR_STATE.HAUNTED: if (forceDirection) { //forcing actor to move in a direction, if failing forcedirection will be setted to false TryForceActorToNextTile(); if (forceDirection) { transform.LookAt(movementTarget.characterPosition, Vector3.up); state = ACTOR_STATE.MOVING; } else { roamingWaitTime = Random.Range(minRoamingWaitTime, maxRoamingWaitTime); roamingT = 0f; currIdleAnimationPlaying = Random.Range(1, 5); state = ACTOR_STATE.CHOOSING; } } break; case ACTOR_STATE.CHILLED: scaredAnimation = "Scared 0" + Random.Range(1, 3); GameObject player = GameObject.FindGameObjectWithTag("Player"); if (player) { Chill c = player.GetComponent <Chill>(); if (c) { c.AddActorToCurrentChill(this); } } state = ACTOR_STATE.CHILLED; break; default: break; } } else if (state == ACTOR_STATE.TALKING) { fear -= Time.deltaTime * fearReductionPerSecond; if (talkCooldownTimer > 0) { talkCooldownTimer -= Time.deltaTime; if (talkCooldownTimer < 0) { talkCooldownTimer = 0; } } CheckTileState(); switch (processedState) { case ACTOR_STATE.NONE: //PLAY TALK ANIMATION if (talkTarget != null) { transform.LookAt(talkTarget.transform.position, Vector3.up); if (Mathf.Approximately(talkCooldownTimer, 0.0f)) { talkCooldownTimer = talkCooldown; talkRefreshTimer = talkRefresh; ResetTalkingActor(); roamingWaitTime = Random.Range(minRoamingWaitTime, maxRoamingWaitTime); roamingT = 0f; currIdleAnimationPlaying = Random.Range(1, 5); if (audioSource.isPlaying) { audioSource.Stop(); } state = ACTOR_STATE.CHOOSING; } else if (!audioSource.isPlaying) { PlaySound(Random.Range(0, 3), Random.Range(minDelayTimeTalking, maxDelayTimeTalking)); } } //NICE! This makes them go back to the previous tile for talking: //TODO can be dangerous when applying powers // if(talkTarget != null && this.currentTile == talkTarget.currentTile) // { // movementTarget = previousTile; // state = ACTOR_STATE.MOVING; // } //STOP talking go to do something else else if (talkTarget == null) { roamingWaitTime = Random.Range(minRoamingWaitTime, maxRoamingWaitTime); roamingT = 0f; currIdleAnimationPlaying = Random.Range(1, 5); talkCooldownTimer = talkCooldown; state = ACTOR_STATE.CHOOSING; } break; case ACTOR_STATE.HAUNTED: talkRefreshTimer = talkRefresh; talkCooldownTimer = talkCooldown; ResetTalkingActor(); if (forceDirection) { //forcing actor to move in a direction, if failing forcedirection will be setted to false TryForceActorToNextTile(); if (forceDirection) { transform.LookAt(movementTarget.characterPosition, Vector3.up); state = ACTOR_STATE.MOVING; } else { roamingWaitTime = Random.Range(minRoamingWaitTime, maxRoamingWaitTime); roamingT = 0f; currIdleAnimationPlaying = Random.Range(1, 5); state = ACTOR_STATE.CHOOSING; } } break; case ACTOR_STATE.CHILLED: scaredAnimation = "Scared 0" + Random.Range(1, 3); GameObject player = GameObject.FindGameObjectWithTag("Player"); if (player) { Chill c = player.GetComponent <Chill>(); if (c) { c.AddActorToCurrentChill(this); } else { Debug.LogError("Chilled activated but not detected from actrors"); } } talkRefreshTimer = talkRefresh; talkCooldownTimer = talkCooldown; ResetTalkingActor(); state = ACTOR_STATE.CHILLED; break; default: break; } } else if (state == ACTOR_STATE.CHILLED) { if (scaredAnimation != string.Empty) { gameObject.GetComponent <Animator>().Play(scaredAnimation); } if (deActivateChilled) { deActivateChilled = false; roamingWaitTime = Random.Range(minRoamingWaitTime, maxRoamingWaitTime); roamingT = 0f; currIdleAnimationPlaying = Random.Range(1, 5); state = ACTOR_STATE.CHOOSING; } } else if (state == ACTOR_STATE.NONE) { talkCooldownTimer = talkCooldown; talkRefreshTimer = talkRefresh; roamingWaitTime = Random.Range(minRoamingWaitTime, maxRoamingWaitTime); roamingT = 0f; currIdleAnimationPlaying = Random.Range(1, 5); state = ACTOR_STATE.CHOOSING; } }