From 2da73f12d04a5deafd32072b6630dd8c046866e8 Mon Sep 17 00:00:00 2001 From: Glence Date: Sun, 26 Mar 2023 13:27:42 +0800 Subject: [PATCH] ai fix along wit new music --- Assets/Audio/Music.bank | Bin 2888192 -> 2888192 bytes Assets/Scenes/Level1.shade | 2 +- Assets/Scenes/Level2.shade | 2 +- Assets/Scenes/Level3.shade | 24 +- .../AIBehaviour/AIRework/HomeOwnerAI.cs | 265 +++++++++--------- .../AIRework/HomeOwnerAttackHitbox.cs | 43 +-- .../AIBehaviour/AIRework/States/AlertState.cs | 87 +++--- .../AIRework/States/AttackState.cs | 262 ++++++++--------- Assets/Scripts/Gameplay/Item/SC_Item.cs | 3 +- Assets/Scripts/Gameplay/SC_GameManager.cs | 10 + 10 files changed, 361 insertions(+), 337 deletions(-) diff --git a/Assets/Audio/Music.bank b/Assets/Audio/Music.bank index 30d594ca5919c723a3f397e20a98446605858b87..2e05bb9b5aeda144e05e00b56d062ac2df4232f0 100644 GIT binary patch delta 231 zcmWm1OA3Ne06@{6Y5)IU)6A?iTSL$!2=~*hfshbHGq(_pg%`DiMlGUEM8j?u&frFu zR76N`hVnJ#-PkHx=3lbXJsDV6UlW{2-fKsGHYZhmo!#9b4v%ptpdbqs8gj^^fFepL vqk<}GsH1@L8=m;2`rk1>2b^nr<~E}oB patrolPointPool; + + public void Reset() { - public float idleDuration = 1.0f; - public float timeoutDuration = 2.0f; - public GameObject patrolPointParent; - - public float patrolSpeed = 2.0f; - public float chaseSpeed = 3.0f; - - public float alertCooldown = 0.0f; - - public GameObject player; - public GameObject attackHitbox; - - public AnimationClipAsset walkingAnim; - public AnimationClipAsset idleAnim; - public AnimationClipAsset alertAnim; - public AnimationClipAsset alertRunAnim; - public AnimationClipAsset alertIdleAnim; - public AnimationClipAsset timeoutAnim; - public AnimationClipAsset atkWindupAnim; - public AnimationClipAsset atkHoldAnim; - public AnimationClipAsset atkSeqAnim; - - private Vector3 startPos; - - [NonSerialized] - public IEnumerable patrolPointPool; - - public void Reset() - { - Transform transform = GetComponent(); - StateMachine machine = GetScript(); - if (transform && machine) - { - transform.GlobalPosition = startPos; - machine.SetState(typeof(IdleState)); - } - } + Transform transform = GetComponent(); + StateMachine machine = GetScript(); + if (transform && machine) + { + transform.GlobalPosition = startPos; + machine.SetState(typeof(IdleState)); + } + } - protected override void awake() - { - StateMachine machine = GetScript(); - if(machine) - { - Dictionary dictionary = new Dictionary(); - dictionary.Add(typeof(IdleState), new IdleState(machine)); - dictionary.Add(typeof(PatrolState), new PatrolState(machine)); - dictionary.Add(typeof(TimeoutState), new TimeoutState(machine)); - dictionary.Add(typeof(ChaseState), new ChaseState(machine)); - dictionary.Add(typeof(AlertState), new AlertState(machine)); - dictionary.Add(typeof(AttackState), new AttackState(machine)); + protected override void awake() + { + StateMachine machine = GetScript(); + if (machine) + { + Dictionary dictionary = new Dictionary(); + dictionary.Add(typeof(IdleState), new IdleState(machine)); + dictionary.Add(typeof(PatrolState), new PatrolState(machine)); + dictionary.Add(typeof(TimeoutState), new TimeoutState(machine)); + dictionary.Add(typeof(ChaseState), new ChaseState(machine)); + dictionary.Add(typeof(AlertState), new AlertState(machine)); + dictionary.Add(typeof(AttackState), new AttackState(machine)); - machine.InitStateMachine(dictionary); - } + machine.InitStateMachine(dictionary); + } - AudioHandler.audioClipHandlers["HO_footsteps"] = SHADE.Audio.CreateAudioClip("event:/Homeowner/homeowner_footsteps"); + AudioHandler.audioClipHandlers["HO_footsteps"] = SHADE.Audio.CreateAudioClip("event:/Homeowner/homeowner_footsteps"); - patrolPointPool = patrolPointParent.GetComponentsInChildren(); - Transform transform = GetComponent(); - if (transform) - { - startPos = transform.GlobalPosition; - } - - } - - protected override void start() - { - attackHitbox.SetActive(false); - } - - - protected override void update() - { - if(alertCooldown > 0.0f) - { - alertCooldown -= Time.DeltaTimeF; - } - else - { - alertCooldown = 0.0f; - } - AICheat(); - } - - public bool ShouldTransitAlert() - { - AILineOfSight los = GetScript(); - if (los) - { - if (los.withinSight && alertCooldown <= 0.0f) - { - return true; - } - } - return false; - } - - public void RotateToPlayer() - { - - //Transform playerTransform = player.GetComponent(); - - ////Rotate to face player. - //Transform aiTransform = GetComponent(); - //if(playerTransform && aiTransform) - //{ - // Vector3 direction = playerTransform.GlobalPosition - aiTransform.GlobalPosition; - // Quaternion currentRotation = aiTransform.LocalRotation; - // Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(direction.x, direction.z), 0.0f); - // aiTransform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, 5.0f * (float)Time.FixedDeltaTime); - //} - - } - - private void AICheat() - { - StateMachine machine = GetScript(); - AILineOfSight los = GetScript(); - if(machine && los) - { - if (Input.GetKeyDown(Input.KeyCode.K)) - { - los.range = 5.0f; - machine.SetState(typeof(AlertState)); - } - if(Input.GetKeyDown(Input.KeyCode.L)) - { - los.range = 0.0f; - machine.SetState(typeof(IdleState)); - } - - } - - } + patrolPointPool = patrolPointParent.GetComponentsInChildren(); + Transform transform = GetComponent(); + if (transform) + { + startPos = transform.GlobalPosition; + } + atk = false; } + + protected override void start() + { + //attackHitbox.SetActive(false); + } + + + protected override void update() + { + if (alertCooldown > 0.0f) + { + alertCooldown -= Time.DeltaTimeF; + } + else + { + alertCooldown = 0.0f; + } + AICheat(); + } + + public bool ShouldTransitAlert() + { + AILineOfSight los = GetScript(); + if (los) + { + if (los.withinSight && alertCooldown <= 0.0f) + { + return true; + } + } + return false; + } + + public void RotateToPlayer() + { + + //Transform playerTransform = player.GetComponent(); + + ////Rotate to face player. + //Transform aiTransform = GetComponent(); + //if(playerTransform && aiTransform) + //{ + // Vector3 direction = playerTransform.GlobalPosition - aiTransform.GlobalPosition; + // Quaternion currentRotation = aiTransform.LocalRotation; + // Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(direction.x, direction.z), 0.0f); + // aiTransform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, 5.0f * (float)Time.FixedDeltaTime); + //} + + } + + private void AICheat() + { + StateMachine machine = GetScript(); + AILineOfSight los = GetScript(); + if (machine && los) + { + if (Input.GetKeyDown(Input.KeyCode.K)) + { + los.range = 5.0f; + machine.SetState(typeof(AlertState)); + } + if (Input.GetKeyDown(Input.KeyCode.L)) + { + los.range = 0.0f; + machine.SetState(typeof(IdleState)); + } + + } + + } + + + } } diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs index 1c9ac802..30d99e57 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs @@ -13,14 +13,18 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework { public GameObject aiGO; - Transform transform; - Transform aiTransform; + private bool raccoonFound = false; + Transform transform; + Transform aiTransform; + private PlayerController pc; + private HomeOwnerAI ai; protected override void start() { - transform = GetComponent(); - aiTransform = aiGO.GetComponent(); + transform = GetComponent(); + aiTransform = aiGO.GetComponent(); + ai = aiGO.GetScript(); } protected override void update() @@ -30,29 +34,28 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework transform.GlobalPosition = aiTransform.GlobalPosition + aiTransform.Forward * 0.7f; transform.GlobalEulerAngles = aiTransform.GlobalEulerAngles; - + + } + + if (raccoonFound && pc && ai) + { + pc.currentState = RaccoonStates.CAUGHT; + if (pc.stateMachine && !pc.stateMachine.IsState(typeof(PlayerCaughtState))) + pc.stateMachine.SetState(typeof(PlayerCaughtState)); + + pc.GotCaught(); + ai.Reset(); + raccoonFound = false; } } protected override void onTriggerStay(CollisionInfo info) { - - PlayerController pc = info.GameObject.GetScript(); - HomeOwnerAI ai = aiGO.GetScript(); - if (pc) + pc = info.GameObject.GetScript(); + if (ai && ai.atk && pc) { - pc.currentState = RaccoonStates.CAUGHT; - if (pc.stateMachine && !pc.stateMachine.IsState(typeof(PlayerCaughtState))) - pc.stateMachine.SetState(typeof(PlayerCaughtState)); - - pc.GotCaught(); - if (ai) - { - ai.Reset(); - GameObject.SetActive(false); - } - + raccoonFound = true; } } diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AlertState.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AlertState.cs index 272b694d..f3c9e436 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AlertState.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AlertState.cs @@ -4,52 +4,55 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using SHADE_Scripting.Audio; namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States { - public class AlertState: AIBaseState + public class AlertState : AIBaseState + { + const float alertDuration = 16.0f / 30.0f + 1.0f; + float alertTimer = alertDuration; + + public AlertState(StateMachine machine) : base(machine) { - const float alertDuration = 16.0f/30.0f + 1.0f; - float alertTimer = alertDuration; - - - public AlertState(StateMachine machine): base(machine) - { - stateName = "Alert"; - } - - public override void OnEnter() - { - alertTimer = alertDuration; - if(animator) - { - animator.PlayOneShot(ai.alertAnim); - - } - SHADE.Audio.SetParameterWithLabel("PlayerDetection", "Detected"); - } - public override void OnExit() - { - animator.Stop(); - } - - public override void update() - { - alertTimer -= Time.DeltaTimeF; - if (alertTimer <= 0.0f) - { - machine.SetState(typeof(ChaseState)); - } - - } - - - - - public override void fixedUpdate() - { - - } + stateName = "Alert"; + AudioHandler.audioClipHandlers["raccoondetected"] = SHADE.Audio.CreateAudioClip("event:/Raccoon/raccoon_detected"); } + + public override void OnEnter() + { + alertTimer = alertDuration; + if (animator) + { + animator.PlayOneShot(ai.alertAnim); + + } + SHADE.Audio.SetParameterWithLabel("PlayerDetection", "Detected"); + AudioHandler.audioClipHandlers["raccoondetected"].Play(); + } + public override void OnExit() + { + animator.Stop(); + } + + public override void update() + { + alertTimer -= Time.DeltaTimeF; + if (alertTimer <= 0.0f) + { + machine.SetState(typeof(ChaseState)); + } + + } + + + + + public override void fixedUpdate() + { + + } + + } } diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AttackState.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AttackState.cs index cd25ef9d..b444c2fb 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AttackState.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AttackState.cs @@ -7,145 +7,149 @@ using System.Threading.Tasks; namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States { - public class AttackState: AIBaseState + public class AttackState : AIBaseState + { + + float timer = 0.0f; + + float windupTime = 8.0f / 30.0f; + float holdTime = 0.3f; + float seqTime = 8.0f / 30.0f; + float endTime = 0.5f; + + + bool windUp = false; + bool hold = false; + bool seq = false; + bool end = false; + + + public AttackState(StateMachine machine) : base(machine) { - float timer = 0.0f; - - float windupTime = 8.0f / 30.0f; - float holdTime = 0.3f; - float seqTime = 8.0f / 30.0f ; - float endTime = 0.5f; + } - bool windUp = false; - bool hold = false; - bool seq = false; - bool end = false; + public override void OnEnter() + { + timer = 0.0f; + animator.PlayOneShot(ai.atkWindupAnim); + windUp = true; + hold = false; + seq = false; + end = false; + timer = windupTime; - public AttackState(StateMachine machine): base(machine) - { + RotateToVelocity rotate = machine.GetScript(); + if (rotate) + rotate.active = false; - } + RigidBody rigid = machine.GetComponent(); + if (rigid) + { + rigid.LinearVelocity = Vector3.Zero; + } + Transform transform = machine.GetComponent(); + AILineOfSight los = machine.GetScript(); + Transform playerTransform = ai.player.GetComponent(); - public override void OnEnter() - { - timer = 0.0f; - animator.PlayOneShot(ai.atkWindupAnim); - windUp = true; - hold = false; - seq = false; - end = false; - timer = windupTime; + if (los && transform) + { + Vector3 direction = playerTransform.GlobalPosition - transform.GlobalPosition; + Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(direction.x, direction.z), 0.0f); + transform.LocalRotation = targetRotation; - RotateToVelocity rotate = machine.GetScript(); - if (rotate) - rotate.active = false; + } - RigidBody rigid = machine.GetComponent(); - if(rigid) - { - rigid.LinearVelocity = Vector3.Zero; - } - Transform transform = machine.GetComponent(); - AILineOfSight los = machine.GetScript(); - Transform playerTransform = ai.player.GetComponent(); - - - if (los && transform) - { - Vector3 direction = playerTransform.GlobalPosition - transform.GlobalPosition; - Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(direction.x, direction.z), 0.0f); - transform.LocalRotation = targetRotation; - - } - - ai.attackHitbox.SetActive(false); - - } - - public override void OnExit() - { - animator.Stop(); - RotateToVelocity rotate = ai.GetScript(); - if (rotate) - { - rotate.active = true; - rotate.lookAround = false; - } - - - ai.attackHitbox.SetActive(false); - - } - - public override void update() - { - timer -= Time.DeltaTimeF; - if(windUp) - { - if(timer <= 0.0f) - { - windUp = false; - hold = true; - animator.Play(ai.atkHoldAnim); - timer = holdTime; - ai.attackHitbox.SetActive(true); - } - } - else if(hold) - { - if(timer <= 0.0f) - { - hold = false; - seq = true; - animator.PlayOneShot(ai.atkSeqAnim); - timer = seqTime; - } - } - else if(seq) - { - - if(timer <= 1.0f / 30.0f) - { - ai.attackHitbox.SetActive(true); - } - - if(timer <= 0.0f) - { - seq = false; - end = true; - - timer = endTime; - - - } - } - else if(end) - { - if(timer <= 0.0f) - { - end = false; - AILineOfSight los = ai.GetScript(); - if (los && los.withinSight) - { - machine.SetState(typeof(ChaseState)); - } - else - { - machine.SetState(typeof(TimeoutState)); - } - } - } - - - } - - public override void fixedUpdate() - { - - } + //ai.attackHitbox.SetActive(false); } + + public override void OnExit() + { + animator.Stop(); + RotateToVelocity rotate = ai.GetScript(); + if (rotate) + { + rotate.active = true; + rotate.lookAround = false; + } + + + //ai.attackHitbox.SetActive(false); + machine.GetScript().atk = false; + + } + + public override void update() + { + timer -= Time.DeltaTimeF; + if (windUp) + { + if (timer <= 0.0f) + { + windUp = false; + hold = true; + animator.Play(ai.atkHoldAnim); + timer = holdTime; + //ai.attackHitbox.SetActive(true); + //machine.GetScript().atk = true; + } + } + else if (hold) + { + if (timer <= 0.0f) + { + hold = false; + seq = true; + animator.PlayOneShot(ai.atkSeqAnim); + timer = seqTime; + } + } + else if (seq) + { + + if (timer <= 1.0f / 30.0f) + { + //ai.attackHitbox.SetActive(true); + machine.GetScript().atk = true; + } + + if (timer <= 0.0f) + { + seq = false; + end = true; + + timer = endTime; + + + } + } + else if (end) + { + if (timer <= 0.0f) + { + end = false; + AILineOfSight los = ai.GetScript(); + if (los && los.withinSight) + { + machine.SetState(typeof(ChaseState)); + } + else + { + machine.SetState(typeof(TimeoutState)); + } + } + } + + + } + + public override void fixedUpdate() + { + + } + + } } diff --git a/Assets/Scripts/Gameplay/Item/SC_Item.cs b/Assets/Scripts/Gameplay/Item/SC_Item.cs index 86b605c0..10b04d6c 100644 --- a/Assets/Scripts/Gameplay/Item/SC_Item.cs +++ b/Assets/Scripts/Gameplay/Item/SC_Item.cs @@ -1,5 +1,6 @@ using SHADE; using SHADE_Scripting.Audio; +using SHADE_Scripting.Gameplay.AIBehaviour.AIRework; using System; public class Item : Script { @@ -155,7 +156,7 @@ public class Item : Script emitter.Emit(); } - if (info.GameObject.GetScript() && homeownerOnce) + if (info.GameObject.GetScript() && homeownerOnce) { homeownerOnce = false; returnBack = true; diff --git a/Assets/Scripts/Gameplay/SC_GameManager.cs b/Assets/Scripts/Gameplay/SC_GameManager.cs index 4a81a67d..b4e17d29 100644 --- a/Assets/Scripts/Gameplay/SC_GameManager.cs +++ b/Assets/Scripts/Gameplay/SC_GameManager.cs @@ -51,6 +51,8 @@ public class GameManager : Script private bool goingToWin; private bool goingToLose; + private bool playSpeedUp = false; + protected override void awake() { if (Instance != null && Instance != this) @@ -91,6 +93,7 @@ public class GameManager : Script AudioHandler.audioClipHandlers["SFXItemScore"] = Audio.CreateAudioClip("event:/Music/stingers/item_scored"); AudioHandler.audioClipHandlers["RaccoonScored"] = Audio.CreateAudioClip("event:/Raccoon/raccoon_scored"); + Audio.SetParameterWithLabel("PlaybackSpeed", "Normal"); } protected override void start() @@ -123,6 +126,13 @@ public class GameManager : Script if (currGameState == GameState.START) { timer -= Time.DeltaTimeF; + + if (timer < 30.0f && !playSpeedUp) + { + Audio.SetParameterWithLabel("PlaybackSpeed", "ToTransition"); + playSpeedUp = true; + } + if(scoreText) scoreText.GetComponent().Text = $"{Score}"; if(timeText)