From 47f195e99821d3467ff46572069c22c6c95f1df7 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Sat, 25 Mar 2023 14:51:28 +0800 Subject: [PATCH] HomeOwner attack and animations --- .../Level2_AITest_NavData.shnav.shmeta | 2 +- Assets/Navigation Data/Level2_NavData.shnav | Bin 0 -> 836 bytes .../Level2_NavData.shnav.shmeta | 3 + Assets/Scenes/Level2.shade | 143 +++++++++++++++--- Assets/Scenes/Level2_AITest.shade | 12 +- .../AIBehaviour/AIRework/AILineOfSight.cs | 2 +- .../AIBehaviour/AIRework/HomeOwnerAI.cs | 60 +++++++- .../AIRework/HomeOwnerAttackHitbox.cs | 26 ++++ .../AIRework/HomeOwnerAttackHitbox.cs.shmeta | 3 + .../AIBehaviour/AIRework/RotateToVelocity.cs | 60 +++++++- .../AIRework/States/AIBaseState.cs | 6 +- .../AIBehaviour/AIRework/States/AlertState.cs | 14 +- .../AIRework/States/AttackState.cs | 124 +++++++++++++++ .../AIRework/States/AttackState.cs.shmeta | 3 + .../AIBehaviour/AIRework/States/ChaseState.cs | 67 +++++++- .../AIBehaviour/AIRework/States/IdleState.cs | 16 ++ .../AIRework/States/PatrolState.cs | 21 ++- .../AIRework/States/TimeoutState.cs | 4 +- .../src/Animation/SHAnimatorComponent.cpp | 3 +- .../src/Navigation/SHNavigationSystem.cpp | 8 +- 20 files changed, 522 insertions(+), 55 deletions(-) create mode 100644 Assets/Navigation Data/Level2_NavData.shnav create mode 100644 Assets/Navigation Data/Level2_NavData.shnav.shmeta create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs.shmeta create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AttackState.cs create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AttackState.cs.shmeta diff --git a/Assets/Navigation Data/Level2_AITest_NavData.shnav.shmeta b/Assets/Navigation Data/Level2_AITest_NavData.shnav.shmeta index 045dc5de..8a86cbd9 100644 --- a/Assets/Navigation Data/Level2_AITest_NavData.shnav.shmeta +++ b/Assets/Navigation Data/Level2_AITest_NavData.shnav.shmeta @@ -1,3 +1,3 @@ Name: Level2_AITest_NavData -ID: 258243609 +ID: 254149790 Type: 15 diff --git a/Assets/Navigation Data/Level2_NavData.shnav b/Assets/Navigation Data/Level2_NavData.shnav new file mode 100644 index 0000000000000000000000000000000000000000..745c5688387b45a05701b254aba0b03940ec95bd GIT binary patch literal 836 zcmcJN%?-jZ4253>QpbQ}x2U>7M_`nW2AeQKk3Ge_m&C0i0ihE0A^PHfI~g&elx_f~ z9_WVa$t{Z|0lpKS?)R3kS}O#TL#$pSR?J=Af)hl+S{iE9u6`x;jGdVw*w1vgCP!>! zuH!h_n&`~T`$)Z{2&Y98M%|HQ7q9VO;t$E0KTv#mYcw_=gF7urN{W^eVbUg!BV~W+ Ih_Vr{FZspKP5=M^ literal 0 HcmV?d00001 diff --git a/Assets/Navigation Data/Level2_NavData.shnav.shmeta b/Assets/Navigation Data/Level2_NavData.shnav.shmeta new file mode 100644 index 00000000..5b3e895a --- /dev/null +++ b/Assets/Navigation Data/Level2_NavData.shnav.shmeta @@ -0,0 +1,3 @@ +Name: Level2_NavData +ID: 267663500 +Type: 15 diff --git a/Assets/Scenes/Level2.shade b/Assets/Scenes/Level2.shade index 29266a48..ee0abb92 100644 --- a/Assets/Scenes/Level2.shade +++ b/Assets/Scenes/Level2.shade @@ -1,4 +1,4 @@ -- NavData: 0 +- NavData: 267663500 - EID: 20 Name: ===== Light ===== IsActive: true @@ -3144,8 +3144,6 @@ armLength: 2 turnSpeedPitch: 0.200000003 turnSpeedYaw: 0.400000006 - inverseXControls: false - inverseYControls: false pitchUpperClamp: 45 pitchLowerClamp: 5 - EID: 9 @@ -5547,7 +5545,7 @@ - EID: 158 Name: ====AI===== IsActive: true - NumberOfChildren: 0 + NumberOfChildren: 2 Components: Transform Component: Translate: {x: 0, y: 0, z: -2.93461704} @@ -5585,19 +5583,99 @@ Position Offset: {x: 0, y: 0.899999976, z: 0} Rotation Offset: {x: 0, y: 0, z: 0} IsActive: true + Navigation Component: + Target: {x: 0, y: 0, z: 0} + Forward: {x: 0, y: 0, z: 0} + Recalculate Path: true + Unreachable Target: false + Tolerance: 1 + Acceptance threshold: 0.100000001 + IsActive: true + Animator Component: + Rig: 76586906 + AnimationController: 0 + IsActive: true Scripts: - - Type: Homeowner1 + - Type: StateMachine Enabled: true - waypointsPool: 166 - startWaypoint: 162 - patrolSpeed: 0.75 - chaseSpeed: 2 - turningSpeed: 5 - sightDistance: 8 - eyeOffset: [0, 1.64999998, 0] - distanceToCapture: 0.800000012 - captureTime: 0.5 - footstepSFXIntervalMultiplier: 0.5 + currentStateName: "" + currentAnimName: "" + - Type: SHADE_Scripting.Gameplay.AIBehaviour.AIRework.RotateToVelocity + Enabled: true + rotationPerSecond: 5 + active: true + rotateToPlayerLastKnown: false + lookAround: false + - Type: SHADE_Scripting.Gameplay.AIBehaviour.AIRework.HomeOwnerAI + Enabled: true + idleDuration: 1 + timeoutDuration: 2 + patrolPointParent: 166 + patrolSpeed: 1 + chaseSpeed: 3 + alertCooldown: 0 + player: 2 + attackHitbox: 612 + walkingAnim: 229189609 + idleAnim: 224442713 + alertAnim: 227890696 + alertRunAnim: 230172366 + alertIdleAnim: 234046577 + timeoutAnim: 219563559 + atkWindupAnim: 223078653 + atkHoldAnim: 228250554 + atkSeqAnim: 226129627 + - Type: SHADE_Scripting.Gameplay.AIBehaviour.AIRework.AILineOfSight + Enabled: true + player: 2 + range: 5 + angle: 30 + angleBetween: 0 + distance: 0 + heightLimit: 1 + rayOffset: [0, 0, 0] + withinRange: false + withinSight: false + lastFoundPos: [0, 0, 0] + lastFoundTimer: 0 +- EID: 612 + Name: AttackHitbox + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: 0, z: 0} + Rotate: {x: 0, y: 0, z: 0} + Scale: {x: 1, y: 1, z: 1} + IsActive: true + Collider Component: + Colliders: + - Is Trigger: true + Collision Tag: 5 + Type: Box + Half Extents: {x: 0.800000012, y: 1.79999995, z: 1} + Friction: 0.400000006 + Bounciness: 0 + Density: 1 + Position Offset: {x: 0, y: 0.899999976, z: 0.400000006} + Rotation Offset: {x: 0, y: 0, z: 0} + IsActive: true + Scripts: ~ +- EID: 610 + Name: Net + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: 0, z: 0} + Rotate: {x: 0, y: 0, z: 0} + Scale: {x: 1, y: 1, z: 1} + IsActive: true + Renderable Component: + Mesh: 0 + Material: 0 + IsActive: true + Scripts: ~ - EID: 10 Name: Pause Canvas IsActive: false @@ -5649,7 +5727,7 @@ Components: Transform Component: Translate: {x: 0, y: -300, z: 0} - Rotate: {x: 0.5, y: 0.5, z: 0.5} + Rotate: {x: -1.48352981, y: 0.5, z: 0.5} Scale: {x: 400, y: 100, z: 500} IsActive: true Renderable Component: @@ -6208,7 +6286,7 @@ Collider Component: Colliders: - Is Trigger: false - Collision Tag: 7 + Collision Tag: 8 Type: Box Half Extents: {x: 10, y: 1, z: 18} Friction: 0.400000006 @@ -9206,7 +9284,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: -0.901912689, y: -2.37551575e-07, z: 0.00345230103} + Translate: {x: -0.75174284, y: -2.38418579e-07, z: 0.09204638} Rotate: {x: -0, y: 1.95162022, z: 0} Scale: {x: 0.999974966, y: 1, z: 0.999974966} IsActive: true @@ -9242,7 +9320,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: -0.180002213, y: 0, z: -0.507282257} + Translate: {x: -0.180002213, y: 0, z: -0.452192545} Rotate: {x: -0, y: 2.26356983, z: 0} Scale: {x: 0.999929309, y: 1, z: 0.999929309} IsActive: true @@ -9306,6 +9384,15 @@ Density: 1 Position Offset: {x: 0.180000007, y: 0.5, z: 0} Rotation Offset: {x: 0, y: 0, z: 0} + - Is Trigger: false + Collision Tag: 9 + Type: Box + Half Extents: {x: 0.600000024, y: 1, z: 0.5} + Friction: 0.400000006 + Bounciness: 0 + Density: 1 + Position Offset: {x: 0, y: 0.699999988, z: -0.100000001} + Rotation Offset: {x: 0, y: 0, z: 0} IsActive: true Scripts: ~ - EID: 204 @@ -9342,6 +9429,15 @@ Density: 1 Position Offset: {x: 0.180000007, y: 0.5, z: 0} Rotation Offset: {x: 0, y: 0, z: 0} + - Is Trigger: false + Collision Tag: 9 + Type: Box + Half Extents: {x: 1, y: 1, z: 1} + Friction: 0.400000006 + Bounciness: 0 + Density: 1 + Position Offset: {x: 0, y: 0.400000006, z: 0} + Rotation Offset: {x: 0, y: 0, z: 0} IsActive: true Scripts: ~ - EID: 236 @@ -10247,6 +10343,15 @@ Density: 1 Position Offset: {x: -0.474999994, y: 0.600000024, z: 0} Rotation Offset: {x: 0, y: 0, z: 0} + - Is Trigger: false + Collision Tag: 9 + Type: Box + Half Extents: {x: 4.30000019, y: 1.20000005, z: 1.20000005} + Friction: 0.400000006 + Bounciness: 0 + Density: 1 + Position Offset: {x: 1.5, y: 0.600000024, z: 0} + Rotation Offset: {x: 0, y: 0, z: 0} IsActive: true Scripts: ~ - EID: 230 diff --git a/Assets/Scenes/Level2_AITest.shade b/Assets/Scenes/Level2_AITest.shade index ccbc2737..49250c2e 100644 --- a/Assets/Scenes/Level2_AITest.shade +++ b/Assets/Scenes/Level2_AITest.shade @@ -1,4 +1,4 @@ -- NavData: 258243609 +- NavData: 254149790 - EID: 20 Name: ===== Light ===== IsActive: true @@ -5429,7 +5429,7 @@ Freeze Position Y: false Freeze Position Z: false Freeze Rotation X: true - Freeze Rotation Y: false + Freeze Rotation Y: true Freeze Rotation Z: true IsActive: true Collider Component: @@ -5437,7 +5437,7 @@ - Is Trigger: false Collision Tag: 5 Type: Box - Half Extents: {x: 0.600000024, y: 1.79999995, z: 0.400000006} + Half Extents: {x: 0.400000006, y: 1.79999995, z: 0.400000006} Friction: 0.400000006 Bounciness: 0 Density: 1 @@ -5466,9 +5466,6 @@ Enabled: true currentStateName: "" currentAnimName: "" - - Type: SHADE_Scripting.Gameplay.AIBehaviour.AIRework.RotateToVelocity - Enabled: true - rotationPerSecond: 5 - Type: SHADE_Scripting.Gameplay.AIBehaviour.AIRework.AILineOfSight Enabled: true player: 2 @@ -5482,6 +5479,9 @@ withinSight: false lastFoundPos: [0, 0, 0] lastFoundTimer: 0 + - Type: SHADE_Scripting.Gameplay.AIBehaviour.AIRework.RotateToVelocity + Enabled: true + rotationPerSecond: 5 - EID: 10 Name: Pause Canvas IsActive: false diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/AILineOfSight.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/AILineOfSight.cs index c95add93..2e8610c5 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/AILineOfSight.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/AILineOfSight.cs @@ -75,7 +75,7 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework { if (hit.Hit && hit.Other != player) { - Debug.Log("AI LOS: HIT OTHER"); + //Debug.Log("AI LOS: HIT OTHER"); withinSight = false; break; } diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs index 08bafb88..437d6ac6 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs @@ -22,12 +22,35 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework 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)); + } + } + + protected override void awake() { @@ -40,13 +63,23 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework 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); } patrolPointPool = patrolPointParent.GetComponentsInChildren(); + Transform transform = GetComponent(); + if (transform) + { + startPos = transform.GlobalPosition; + } + } + protected override void start() + { + attackHitbox.SetActive(false); } @@ -60,14 +93,7 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework { alertCooldown = 0.0f; } - - RigidBody rigid = GetComponent(); - if(rigid) - { - rigid.AngularVelocity = Vector3.Zero; - } - - + AICheat(); } public bool ShouldTransitAlert() @@ -100,6 +126,24 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework } + private void AICheat() + { + StateMachine machine = GetScript(); + if(machine) + { + if (Input.GetKeyDown(Input.KeyCode.F)) + { + machine.SetState(typeof(AlertState)); + } + if(Input.GetKeyDown(Input.KeyCode.L)) + { + machine.SetState(typeof(IdleState)); + } + + } + + } + } } diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs new file mode 100644 index 00000000..3bf3d2d6 --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs @@ -0,0 +1,26 @@ +using SHADE; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework +{ + public class HomeOwnerAttackHitbox:Script + { + + protected override void onCollisionEnter(CollisionInfo info) + { + PlayerController pc = info.GameObject.GetScript(); + HomeOwnerAI ai = GameObject.Parent.GetScript(); + if (pc && ai) + { + Debug.Log("HomeOwnerAttackHitbox: Player caught"); + + } + } + + + } +} diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs.shmeta b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs.shmeta new file mode 100644 index 00000000..1d9ac5ec --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAttackHitbox.cs.shmeta @@ -0,0 +1,3 @@ +Name: HomeOwnerAttackHitbox +ID: 164649501 +Type: 9 diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/RotateToVelocity.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/RotateToVelocity.cs index 4b0fc8d0..7791aa77 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/RotateToVelocity.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/RotateToVelocity.cs @@ -11,23 +11,73 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework { public float rotationPerSecond = 2.0f; + public bool active = true; + public bool rotateToPlayerLastKnown = false; + + public bool lookAround = false; + private bool left = true; + private float lookOffset = 0.0f; + private float lookAroundAngle = 30.0f; + + protected override void start() + { + rotateToPlayerLastKnown = false; + active = true; + } + + protected override void update() { + if (!active) + return; + RigidBody rigid = GetComponent(); Transform transform = GetComponent(); + if(!lookAround) + { + lookOffset = 0.0f; + } + else + { + if(left ) + { + if (lookOffset > -lookAroundAngle) + lookOffset -= rotationPerSecond * Time.DeltaTimeF; + else + left = false; + } + if (!left) + { + if (lookOffset < lookAroundAngle) + lookOffset += rotationPerSecond * Time.DeltaTimeF; + else + left = false; + } + } + + if(rigid && transform) { Vector3 vel = rigid.LinearVelocity; + rigid.AngularVelocity = Vector3.Zero; - if(vel.GetSqrMagnitude() > 1.0f) + + AILineOfSight los = GetScript(); + if(los && rotateToPlayerLastKnown) + { + Vector3 direction = los.lastFoundPos - transform.GlobalPosition; + Quaternion currentRotation = transform.LocalRotation; + Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(direction.x, direction.z) + lookOffset, 0.0f); + transform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationPerSecond * (float)Time.DeltaTimeF); + return; + } + if(vel.GetMagnitude() > 0.01f) { Quaternion currentRotation = transform.LocalRotation; - Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(vel.x, vel.z), 0.0f); - transform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationPerSecond * (float)Time.FixedDeltaTime); + Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(vel.x, vel.z) + lookOffset, 0.0f); + transform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationPerSecond * (float)Time.DeltaTimeF); } - - } diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AIBaseState.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AIBaseState.cs index bf1dd730..3a42a15f 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AIBaseState.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AIBaseState.cs @@ -1,4 +1,5 @@ -using System; +using SHADE; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,11 +11,12 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States { protected HomeOwnerAI ai; - + protected Animator animator; public AIBaseState(StateMachine stateMachine): base(stateMachine, "") { stateName = "AI Base State"; ai = stateMachine.GetScript(); + animator = stateMachine.GetComponent(); } diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AlertState.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AlertState.cs index 16379b34..e8dd7014 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AlertState.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AlertState.cs @@ -9,7 +9,7 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States { public class AlertState: AIBaseState { - const float alertDuration = 2.0f; + const float alertDuration = 16.0f/30.0f + 1.0f; float alertTimer = alertDuration; @@ -21,6 +21,15 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States public override void OnEnter() { alertTimer = alertDuration; + if(animator) + { + animator.PlayOneShot(ai.alertAnim); + + } + } + public override void OnExit() + { + animator.Stop(); } public override void update() @@ -30,9 +39,12 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States { 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 new file mode 100644 index 00000000..6ce99ab2 --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AttackState.cs @@ -0,0 +1,124 @@ +using SHADE; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States +{ + public class AttackState: AIBaseState + { + + float timer = 0.0f; + + float windupTime = 8.0f / 30.0f; + float holdTime = 0.3f; + float seqTime = 8.0f / 30.0f + 0.5f; + + + bool windUp = false; + bool hold = false; + bool seq = false; + + public AttackState(StateMachine machine): base(machine) + { + + } + + + public override void OnEnter() + { + timer = 0.0f; + animator.PlayOneShot(ai.atkWindupAnim); + windUp = true; + hold = false; + seq = false; + timer = windupTime; + + RotateToVelocity rotate = machine.GetScript(); + if (rotate) + rotate.active = false; + + ai.attackHitbox.SetActive(false); + + } + + public override void OnExit() + { + animator.Stop(); + RotateToVelocity rotate = ai.GetScript(); + if (rotate) + { + rotate.active = true; + rotate.lookAround = false; + } + + 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 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 <= 0.0f) + { + seq = 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/AIBehaviour/AIRework/States/AttackState.cs.shmeta b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AttackState.cs.shmeta new file mode 100644 index 00000000..10ac18ad --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/AttackState.cs.shmeta @@ -0,0 +1,3 @@ +Name: AttackState +ID: 156854236 +Type: 9 diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/ChaseState.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/ChaseState.cs index c54f64d0..2eed4ffd 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/ChaseState.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/ChaseState.cs @@ -1,4 +1,5 @@ using SHADE; +using SHADE.Test; using System; using System.Collections.Generic; using System.Linq; @@ -12,6 +13,10 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States float giveUpDuration = 10.0f; float giveUpTimer = 0.0f; + float atkDistance = 2.0f; + + + bool run = true; public ChaseState(StateMachine machine): base(machine) @@ -22,13 +27,37 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States public override void OnEnter() { giveUpTimer = giveUpDuration; + + animator.Play(ai.alertRunAnim); + run = true; + RotateToVelocity r = machine.GetScript(); + if (r) + { + r.rotateToPlayerLastKnown = true; + } + } + + public override void OnExit() + { + animator.Stop(); + RotateToVelocity r = machine.GetScript(); + if (r) + { + r.rotateToPlayerLastKnown = false; + } + RotateToVelocity rotate = ai.GetScript(); + if (rotate) + { + rotate.lookAround = false; + } } public override void update() { Navigation nav = machine.GetComponent(); AILineOfSight los = ai.GetScript(); - if(los && nav) + RigidBody rigid = machine.GetComponent(); + if (los && nav) { Transform playerTransform = los.player.GetComponent(); if (los.withinSight) @@ -45,13 +74,18 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States machine.SetState(typeof(TimeoutState)); } - RigidBody rigid = machine.GetComponent(); + if(los.distance < atkDistance) + { + machine.SetState(typeof(AttackState)); + } + + if(rigid) { if (los.withinSight) rigid.LinearVelocity = nav.GetForward() * ai.chaseSpeed; else - rigid.LinearVelocity = nav.GetForward() * ai.patrolSpeed; + rigid.LinearVelocity = nav.GetForward() * ai.chaseSpeed; } if(nav.ReachedTarget()) @@ -59,8 +93,35 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States giveUpTimer -= Time.DeltaTimeF; ai.RotateToPlayer(); + RotateToVelocity rotate = ai.GetScript(); + if(rotate) + { + rotate.lookAround = true; + } + } + } + + if (animator && rigid) + { + if (rigid.LinearVelocity.GetMagnitude() < 0.001f) + { + if(run) + { + animator.Play(ai.idleAnim); + run = false; + } + + } + else + { + if(!run) + { + animator.Play(ai.alertRunAnim); + run = true; + } + } } diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs index 027192b7..987f45cf 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs @@ -23,6 +23,22 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States public override void OnEnter() { timer = 0.0f; + animator.Play(ai.idleAnim); + RotateToVelocity rotate = ai.GetScript(); + if (rotate) + { + rotate.lookAround = true; + } + } + + public override void OnExit() + { + animator.Stop(); + RotateToVelocity rotate = ai.GetScript(); + if (rotate) + { + rotate.lookAround = false; + } } public override void update() diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/PatrolState.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/PatrolState.cs index 5724b5eb..6517731f 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/PatrolState.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/PatrolState.cs @@ -14,7 +14,7 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States Vector3 lastFramePos; float stuckTimer ; - + bool run = true; public PatrolState(StateMachine machine) : base(machine) { @@ -34,7 +34,7 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States if (dest) { nav.MoveTo(dest.GlobalPosition); - Debug.Log("Moving to" + dest.GlobalPosition.ToString()); + } lastFramePos = transform.GlobalPosition; @@ -42,6 +42,21 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States stuckTimer = 0.0f; } + + + animator.Play(ai.walkingAnim); + + RotateToVelocity r = machine.GetScript(); + if(r) + { + r.rotateToPlayerLastKnown = false; + } + + } + + public override void OnExit() + { + animator.Stop(); } public override void update() @@ -55,7 +70,7 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States Vector3 d = lastFramePos - transform.GlobalPosition; if (d.GetSqrMagnitude() < 0.001f) { - stuckTimer += Time.DeltaTimeF; + //stuckTimer += Time.DeltaTimeF; } diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/TimeoutState.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/TimeoutState.cs index fd3d1516..a896af78 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/TimeoutState.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/TimeoutState.cs @@ -10,7 +10,7 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States public class TimeoutState : AIBaseState { float timer = 0.0f; - float alertCooldown = 10.0f; + float alertCooldown = 1.0f; public TimeoutState(StateMachine machine) : base(machine) { @@ -21,6 +21,8 @@ namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States public override void OnEnter() { timer = 0.0f; + animator.Play(ai.idleAnim); + } public override void update() diff --git a/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp b/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp index 25a42a53..99a83472 100644 --- a/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp +++ b/SHADE_Engine/src/Animation/SHAnimatorComponent.cpp @@ -262,7 +262,8 @@ namespace SHADE { playOnce = false; isPlaying = false; - currPlaybackTime = currClip->GetStartTimeStamp() + currClip->GetTotalDuration(); + const float SINGLE_FRAME_TIME = 1.0f / static_cast(currClip->GetRawAnimation()->GetTicksPerSecond()); + currPlaybackTime = currClip->GetStartTimeStamp() + currClip->GetTotalDuration() - SINGLE_FRAME_TIME; raiseFinishEvent(); } else diff --git a/SHADE_Engine/src/Navigation/SHNavigationSystem.cpp b/SHADE_Engine/src/Navigation/SHNavigationSystem.cpp index 1c194fe6..2164e503 100644 --- a/SHADE_Engine/src/Navigation/SHNavigationSystem.cpp +++ b/SHADE_Engine/src/Navigation/SHNavigationSystem.cpp @@ -27,8 +27,8 @@ namespace SHADE SHComponentManager::CreateComponentSparseSet(); SystemID i = SystemFamily::GetID(); - drawNavigationArea = true; - drawNavigationData = true; + //drawNavigationArea = true; + //drawNavigationData = true; drawPath = true; } @@ -259,7 +259,7 @@ namespace SHADE topleft += SHVec3{ c * system->GetGridSize().x, 0.0f, r * system->GetGridSize().y } + SHVec3{ halfGridSize.x,0.0f,halfGridSize.y }; SHTransform t; t.position = system->GetGridWorldPos({ r,c }); - t.scale = SHVec3{ halfGridSize.x * 2.0f, 1.0f, halfGridSize.y * 2.0f }; + t.scale = SHVec3{ halfGridSize.x * 1.9f, 0.1f, halfGridSize.y * 1.9f }; t.ComputeTRS(); debugDrawSystem->DrawCube(t.trs, SHColour::RED, true); @@ -273,7 +273,7 @@ namespace SHADE topleft += SHVec3{ c * system->GetGridSize().x, 0.0f, r * system->GetGridSize().y } + SHVec3{ halfGridSize.x,0.0f,halfGridSize.y }; SHTransform t; t.position = system->GetGridWorldPos({ r,c }); - t.scale = SHVec3{ halfGridSize.x * 2.0f, 1.0f, halfGridSize.y * 2.0f }; + t.scale = SHVec3{ halfGridSize.x * 1.9f, 0.1f, halfGridSize.y * 1.9f }; t.ComputeTRS(); debugDrawSystem->DrawCube(t.trs, SHColour::WHITE, true); }