From a6ae025e050edb0d7e4d0de5bad215f52a7bfee9 Mon Sep 17 00:00:00 2001 From: Aditya Singh Date: Tue, 19 Dec 2023 19:28:32 +0530 Subject: [PATCH 1/2] Game Update : - Patrolling State - Chasing State --- Assets/Scripts/Enemy/EnemyController.cs | 2 +- Assets/Scripts/Enemy/OnePunchMan/IState.cs | 2 +- .../OnePunchMan/OnePunchManStateMachine.cs | 4 +- .../Enemy/OnePunchMan/States/IStateMachine.cs | 20 +++++++ .../OnePunchMan/States/IStateMachine.cs.meta | 11 ++++ .../Enemy/OnePunchMan/States/IdleState.cs | 9 +-- .../OnePunchMan/States/PatrollingState.cs | 55 +++++++++++++++++++ .../States/PatrollingState.cs.meta | 11 ++++ .../Enemy/OnePunchMan/States/RotatingState.cs | 9 +-- .../Enemy/OnePunchMan/States/ShootingState.cs | 7 ++- Packages/packages-lock.json | 12 ++-- 11 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs create mode 100644 Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs.meta create mode 100644 Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs create mode 100644 Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs.meta diff --git a/Assets/Scripts/Enemy/EnemyController.cs b/Assets/Scripts/Enemy/EnemyController.cs index f7713da3..be39fda4 100644 --- a/Assets/Scripts/Enemy/EnemyController.cs +++ b/Assets/Scripts/Enemy/EnemyController.cs @@ -13,7 +13,7 @@ public class EnemyController protected int currentHealth; protected EnemyState currentState; - protected NavMeshAgent Agent => enemyView.Agent; + public NavMeshAgent Agent => enemyView.Agent; public EnemyScriptableObject Data => enemyScriptableObject; public Quaternion Rotation => enemyView.transform.rotation; public Vector3 Position => enemyView.transform.position; diff --git a/Assets/Scripts/Enemy/OnePunchMan/IState.cs b/Assets/Scripts/Enemy/OnePunchMan/IState.cs index 7cd2a8e4..8f9f8f21 100644 --- a/Assets/Scripts/Enemy/OnePunchMan/IState.cs +++ b/Assets/Scripts/Enemy/OnePunchMan/IState.cs @@ -2,7 +2,7 @@ namespace StatePattern.Enemy { public interface IState { - public OnePunchManController Owner { get; set; } + public EnemyController Owner { get; set; } public void OnStateEnter(); public void Update(); public void OnStateExit(); diff --git a/Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs b/Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs index 18385333..1040b83f 100644 --- a/Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs +++ b/Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs @@ -46,6 +46,8 @@ public enum OnePunchManStates { IDLE, ROTATING, - SHOOTING + SHOOTING, + PATROLLING, + CHASING } } \ No newline at end of file diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs b/Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs new file mode 100644 index 00000000..c358dc82 --- /dev/null +++ b/Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs @@ -0,0 +1,20 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace StatePattern.StateMachine +{ + public interface IStateMachine + { + public void ChangeState(States newState); + } + public enum States + { + IDLE, + ROTATING, + SHOOTING, + PATROLLING, + CHASING + } + +} \ No newline at end of file diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs.meta b/Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs.meta new file mode 100644 index 00000000..2358a910 --- /dev/null +++ b/Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1e58521317bef1c44b0aabfcf19f7bfa +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/IdleState.cs b/Assets/Scripts/Enemy/OnePunchMan/States/IdleState.cs index ba19c8ba..09b3bd85 100644 --- a/Assets/Scripts/Enemy/OnePunchMan/States/IdleState.cs +++ b/Assets/Scripts/Enemy/OnePunchMan/States/IdleState.cs @@ -1,3 +1,4 @@ +using StatePattern.StateMachine; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -6,11 +7,11 @@ namespace StatePattern.Enemy { public class IdleState : IState { - public OnePunchManController Owner { get; set; } - private OnePunchManStateMachine stateMachine; + public EnemyController Owner { get; set; } + private IStateMachine stateMachine ; private float timer; - public IdleState(OnePunchManStateMachine stateMachine) => this.stateMachine = stateMachine; + public IdleState(IStateMachine stateMachine) => this.stateMachine = stateMachine; public void OnStateEnter() => ResetTimer(); @@ -18,7 +19,7 @@ public void Update() { timer -= Time.deltaTime; if (timer <= 0) - stateMachine.ChangeState(OnePunchManStates.ROTATING); + stateMachine.ChangeState(States.ROTATING); } public void OnStateExit() => timer = 0; diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs b/Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs new file mode 100644 index 00000000..39635816 --- /dev/null +++ b/Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs @@ -0,0 +1,55 @@ +using StatePattern.StateMachine; +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace StatePattern.Enemy +{ + public class PatrollingState : IState + { + public EnemyController Owner { get; set; } + private IStateMachine stateMachine; + private int currentPatrollingIndex = -1; + private Vector3 destination; + + public PatrollingState(IStateMachine stateMachine) => this.stateMachine = stateMachine; + + public void OnStateEnter() + { + SetNextWayPointIndex(); + destination = GetDestination(); + MoveTowardsDestination(); + } + + public void Update() + { + if (ReachedDestination()) + stateMachine.ChangeState(States.IDLE); + } + + public void OnStateExit() { } + + private void SetNextWayPointIndex() + { + if (currentPatrollingIndex == Owner.Data.PatrollingPoints.Count - 1) + { + currentPatrollingIndex = 0; + } + else + { + currentPatrollingIndex++; + } + } + private Vector3 GetDestination() => Owner.Data.PatrollingPoints[currentPatrollingIndex]; + + private void MoveTowardsDestination() + { + Owner.Agent.isStopped = false; + + Owner.Agent.SetDestination(destination); + } + + private bool ReachedDestination() => Owner.Agent.remainingDistance <= Owner.Agent.stoppingDistance; + } +} diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs.meta b/Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs.meta new file mode 100644 index 00000000..514fd12c --- /dev/null +++ b/Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 94a8d45b01cdc994886a16a25b3527a5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/RotatingState.cs b/Assets/Scripts/Enemy/OnePunchMan/States/RotatingState.cs index 5b815b06..2737ad83 100644 --- a/Assets/Scripts/Enemy/OnePunchMan/States/RotatingState.cs +++ b/Assets/Scripts/Enemy/OnePunchMan/States/RotatingState.cs @@ -1,3 +1,4 @@ +using StatePattern.StateMachine; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -6,11 +7,11 @@ namespace StatePattern.Enemy { public class RotatingState : IState { - public OnePunchManController Owner { get; set; } - private OnePunchManStateMachine stateMachine; + public EnemyController Owner { get; set; } + private IStateMachine stateMachine; private float targetRotation; - public RotatingState(OnePunchManStateMachine stateMachine) => this.stateMachine = stateMachine; + public RotatingState(IStateMachine stateMachine) => this.stateMachine = stateMachine; public void OnStateEnter() => targetRotation = (Owner.Rotation.eulerAngles.y + 180) % 360; @@ -18,7 +19,7 @@ public void Update() { Owner.SetRotation(CalculateRotation()); if (IsRotationComplete()) - stateMachine.ChangeState(OnePunchManStates.IDLE); + stateMachine.ChangeState(States.IDLE); } public void OnStateExit() => targetRotation = 0; diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/ShootingState.cs b/Assets/Scripts/Enemy/OnePunchMan/States/ShootingState.cs index 48a2a5f9..90491642 100644 --- a/Assets/Scripts/Enemy/OnePunchMan/States/ShootingState.cs +++ b/Assets/Scripts/Enemy/OnePunchMan/States/ShootingState.cs @@ -1,5 +1,6 @@ using StatePattern.Main; using StatePattern.Player; +using StatePattern.StateMachine; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -8,12 +9,12 @@ namespace StatePattern.Enemy { public class ShootingState : IState { - public OnePunchManController Owner { get; set; } - private OnePunchManStateMachine stateMachine; + public EnemyController Owner { get; set; } + private IStateMachine stateMachine; private PlayerController target; private float shootTimer; - public ShootingState(OnePunchManStateMachine stateMachine) => this.stateMachine = stateMachine; + public ShootingState(IStateMachine stateMachine) => this.stateMachine = stateMachine; public void OnStateEnter() { diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index c6bec04f..bf36f2c7 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -32,13 +32,13 @@ "depth": 0, "source": "builtin", "dependencies": { - "com.unity.ide.visualstudio": "2.0.17", - "com.unity.ide.rider": "3.0.18", + "com.unity.ide.visualstudio": "2.0.16", + "com.unity.ide.rider": "3.0.15", "com.unity.ide.vscode": "1.2.5", "com.unity.editorcoroutines": "1.0.0", - "com.unity.performance.profile-analyzer": "1.2.2", + "com.unity.performance.profile-analyzer": "1.1.1", "com.unity.test-framework": "1.1.31", - "com.unity.testtools.codecoverage": "1.2.2" + "com.unity.testtools.codecoverage": "1.0.1" } }, "com.unity.ide.rider": { @@ -67,7 +67,7 @@ "url": "https://packages.unity.com" }, "com.unity.performance.profile-analyzer": { - "version": "1.2.2", + "version": "1.1.1", "depth": 1, "source": "registry", "dependencies": {}, @@ -101,7 +101,7 @@ "url": "https://packages.unity.com" }, "com.unity.testtools.codecoverage": { - "version": "1.2.2", + "version": "1.0.1", "depth": 1, "source": "registry", "dependencies": { From ead7f39e74e450605604a370a30e880c07716e69 Mon Sep 17 00:00:00 2001 From: Aditya Singh Date: Tue, 19 Dec 2023 23:07:38 +0530 Subject: [PATCH 2/2] Game Update : - Create a state machine for Patrol Man => PatrolManStateMachine - Set up a new Level by following the given instructions. - Design a new Level & add the newly created patrol man in it. --- Assets/Scenes/GameScene.unity | 3 +- .../OnePunchMan/{States => }/IStateMachine.cs | 17 +++---- .../{States => }/IStateMachine.cs.meta | 0 .../OnePunchMan/OnePunchManStateMachine.cs | 1 + .../Scripts/Enemy/PatrolManStateMachine.meta | 8 +++ .../PatrolManController.cs | 39 +++++++++++++++ .../PatrolManController.cs.meta | 11 ++++ .../PatrolManStateMachine.cs | 47 +++++++++++++++++ .../PatrolManStateMachine.cs.meta | 11 ++++ .../Enemy/{OnePunchMan => }/States.meta | 0 Assets/Scripts/Enemy/States/ChasingState.cs | 50 +++++++++++++++++++ .../Scripts/Enemy/States/ChasingState.cs.meta | 11 ++++ .../{OnePunchMan => }/States/IdleState.cs | 2 +- .../States/IdleState.cs.meta | 0 .../States/PatrollingState.cs | 0 .../States/PatrollingState.cs.meta | 0 .../{OnePunchMan => }/States/RotatingState.cs | 0 .../States/RotatingState.cs.meta | 0 .../{OnePunchMan => }/States/ShootingState.cs | 0 .../States/ShootingState.cs.meta | 0 20 files changed, 189 insertions(+), 11 deletions(-) rename Assets/Scripts/Enemy/OnePunchMan/{States => }/IStateMachine.cs (57%) rename Assets/Scripts/Enemy/OnePunchMan/{States => }/IStateMachine.cs.meta (100%) create mode 100644 Assets/Scripts/Enemy/PatrolManStateMachine.meta create mode 100644 Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManController.cs create mode 100644 Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManController.cs.meta create mode 100644 Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManStateMachine.cs create mode 100644 Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManStateMachine.cs.meta rename Assets/Scripts/Enemy/{OnePunchMan => }/States.meta (100%) create mode 100644 Assets/Scripts/Enemy/States/ChasingState.cs create mode 100644 Assets/Scripts/Enemy/States/ChasingState.cs.meta rename Assets/Scripts/Enemy/{OnePunchMan => }/States/IdleState.cs (94%) rename Assets/Scripts/Enemy/{OnePunchMan => }/States/IdleState.cs.meta (100%) rename Assets/Scripts/Enemy/{OnePunchMan => }/States/PatrollingState.cs (100%) rename Assets/Scripts/Enemy/{OnePunchMan => }/States/PatrollingState.cs.meta (100%) rename Assets/Scripts/Enemy/{OnePunchMan => }/States/RotatingState.cs (100%) rename Assets/Scripts/Enemy/{OnePunchMan => }/States/RotatingState.cs.meta (100%) rename Assets/Scripts/Enemy/{OnePunchMan => }/States/ShootingState.cs (100%) rename Assets/Scripts/Enemy/{OnePunchMan => }/States/ShootingState.cs.meta (100%) diff --git a/Assets/Scenes/GameScene.unity b/Assets/Scenes/GameScene.unity index f5f44df7..01f762b4 100644 --- a/Assets/Scenes/GameScene.unity +++ b/Assets/Scenes/GameScene.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 705507994} - m_IndirectSpecularColor: {r: 0.17276844, g: 0.21589246, b: 0.2978263, a: 1} + m_IndirectSpecularColor: {r: 0.172768, g: 0.21589167, b: 0.29782546, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -798,6 +798,7 @@ MonoBehaviour: playerScriptableObject: {fileID: 11400000, guid: 4240caefbf2e2f64eb8046b7c92646e0, type: 2} levelScriptableObjects: - {fileID: 11400000, guid: bada358cc71564c49831629438d87724, type: 2} + - {fileID: 11400000, guid: 3ceefc333a7c35a4d8743f64f88754db, type: 2} sfxSource: {fileID: 629913882} bgMusicSource: {fileID: 973737936} --- !u!4 &2053379264 diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs b/Assets/Scripts/Enemy/OnePunchMan/IStateMachine.cs similarity index 57% rename from Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs rename to Assets/Scripts/Enemy/OnePunchMan/IStateMachine.cs index c358dc82..ec9d31ee 100644 --- a/Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs +++ b/Assets/Scripts/Enemy/OnePunchMan/IStateMachine.cs @@ -8,13 +8,12 @@ public interface IStateMachine { public void ChangeState(States newState); } - public enum States - { - IDLE, - ROTATING, - SHOOTING, - PATROLLING, - CHASING - } - + public enum States + { + IDLE, + ROTATING, + SHOOTING, + PATROLLING, + CHASING + } } \ No newline at end of file diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs.meta b/Assets/Scripts/Enemy/OnePunchMan/IStateMachine.cs.meta similarity index 100% rename from Assets/Scripts/Enemy/OnePunchMan/States/IStateMachine.cs.meta rename to Assets/Scripts/Enemy/OnePunchMan/IStateMachine.cs.meta diff --git a/Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs b/Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs index 1040b83f..5cbc5935 100644 --- a/Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs +++ b/Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using StatePattern.StateMachine; namespace StatePattern.Enemy { diff --git a/Assets/Scripts/Enemy/PatrolManStateMachine.meta b/Assets/Scripts/Enemy/PatrolManStateMachine.meta new file mode 100644 index 00000000..1eb5d78e --- /dev/null +++ b/Assets/Scripts/Enemy/PatrolManStateMachine.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1a2f3413ae647aa468cb63ecf885ee77 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManController.cs b/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManController.cs new file mode 100644 index 00000000..7c806de4 --- /dev/null +++ b/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManController.cs @@ -0,0 +1,39 @@ +using StatePattern.Player; +using StatePattern.StateMachine; +using StatePattern.Main; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace StatePattern.Enemy +{ + public class PatrolManController : EnemyController + { + private PatrolManStateMachine stateMachine; + + public PatrolManController(EnemyScriptableObject enemyScriptableObject) : base(enemyScriptableObject) + { + enemyView.SetController(this); + CreateStateMachine(); + stateMachine.ChangeState(States.IDLE); + } + + private void CreateStateMachine() => stateMachine = new PatrolManStateMachine(this); + + public override void UpdateEnemy() + { + if (currentState == EnemyState.DEACTIVE) + return; + + stateMachine.Update(); + } + + public override void PlayerEnteredRange(PlayerController targetToSet) + { + base.PlayerEnteredRange(targetToSet); + stateMachine.ChangeState(States.CHASING); + } + + public override void PlayerExitedRange() => stateMachine.ChangeState(States.IDLE); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManController.cs.meta b/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManController.cs.meta new file mode 100644 index 00000000..6270ea34 --- /dev/null +++ b/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 19ef1102532b4df409dd699f4122fbe5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManStateMachine.cs b/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManStateMachine.cs new file mode 100644 index 00000000..e2c94bec --- /dev/null +++ b/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManStateMachine.cs @@ -0,0 +1,47 @@ +using StatePattern.StateMachine; +using System.Collections.Generic; +using UnityEngine; + +namespace StatePattern.Enemy +{ + public class PatrolManStateMachine : IStateMachine + { + private PatrolManController Owner; + private IState currentState; + protected Dictionary States = new Dictionary(); + + public PatrolManStateMachine(PatrolManController Owner) + { + this.Owner = Owner; + CreateStates(); + SetOwner(); + } + + private void CreateStates() + { + States.Add(StateMachine.States.IDLE, new IdleState(this)); + States.Add(StateMachine.States.PATROLLING, new PatrollingState(this)); + States.Add(StateMachine.States.CHASING, new ChasingState(this)); + States.Add(StateMachine.States.SHOOTING, new ShootingState(this)); + } + + private void SetOwner() + { + foreach (IState state in States.Values) + { + state.Owner = Owner; + } + } + + public void Update() => currentState?.Update(); + + protected void ChangeState(IState newState) + { + currentState?.OnStateExit(); + currentState = newState; + currentState?.OnStateEnter(); + } + + public void ChangeState(States newState) => ChangeState(States[newState]); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManStateMachine.cs.meta b/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManStateMachine.cs.meta new file mode 100644 index 00000000..7e36d2f3 --- /dev/null +++ b/Assets/Scripts/Enemy/PatrolManStateMachine/PatrolManStateMachine.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6cccabbba62c053498875d54648a3173 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/OnePunchMan/States.meta b/Assets/Scripts/Enemy/States.meta similarity index 100% rename from Assets/Scripts/Enemy/OnePunchMan/States.meta rename to Assets/Scripts/Enemy/States.meta diff --git a/Assets/Scripts/Enemy/States/ChasingState.cs b/Assets/Scripts/Enemy/States/ChasingState.cs new file mode 100644 index 00000000..adae2af3 --- /dev/null +++ b/Assets/Scripts/Enemy/States/ChasingState.cs @@ -0,0 +1,50 @@ +using StatePattern.Main; +using StatePattern.Player; +using StatePattern.StateMachine; +using System.Collections; +using UnityEngine; + +namespace StatePattern.Enemy +{ + public class ChasingState : IState + { + public EnemyController Owner { get; set; } + private IStateMachine stateMachine; + private PlayerController target; + + public ChasingState(IStateMachine stateMachine) => this.stateMachine = stateMachine; + + public void OnStateEnter() + { + SetTarget(); + SetStoppingDistance(); + } + + public void Update() + { + MoveTowardsTarget(); + if (ReachedTarget()) + { + ResetPath(); + stateMachine.ChangeState(States.SHOOTING); + } + } + + public void OnStateExit() => target = null; + + + private void SetTarget() => target = GameService.Instance.PlayerService.GetPlayer(); + + private void SetStoppingDistance() => Owner.Agent.stoppingDistance = Owner.Data.PlayerStoppingDistance; + + private bool MoveTowardsTarget() => Owner.Agent.SetDestination(target.Position); + + private bool ReachedTarget() => Owner.Agent.remainingDistance <= Owner.Agent.stoppingDistance; + + private void ResetPath() + { + Owner.Agent.isStopped = true; + Owner.Agent.ResetPath(); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Enemy/States/ChasingState.cs.meta b/Assets/Scripts/Enemy/States/ChasingState.cs.meta new file mode 100644 index 00000000..a06877f2 --- /dev/null +++ b/Assets/Scripts/Enemy/States/ChasingState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7cd77c79b7a37e24cb87b8584fad8ddd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/IdleState.cs b/Assets/Scripts/Enemy/States/IdleState.cs similarity index 94% rename from Assets/Scripts/Enemy/OnePunchMan/States/IdleState.cs rename to Assets/Scripts/Enemy/States/IdleState.cs index 09b3bd85..a7210410 100644 --- a/Assets/Scripts/Enemy/OnePunchMan/States/IdleState.cs +++ b/Assets/Scripts/Enemy/States/IdleState.cs @@ -8,7 +8,7 @@ namespace StatePattern.Enemy public class IdleState : IState { public EnemyController Owner { get; set; } - private IStateMachine stateMachine ; + private IStateMachine stateMachine; private float timer; public IdleState(IStateMachine stateMachine) => this.stateMachine = stateMachine; diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/IdleState.cs.meta b/Assets/Scripts/Enemy/States/IdleState.cs.meta similarity index 100% rename from Assets/Scripts/Enemy/OnePunchMan/States/IdleState.cs.meta rename to Assets/Scripts/Enemy/States/IdleState.cs.meta diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs b/Assets/Scripts/Enemy/States/PatrollingState.cs similarity index 100% rename from Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs rename to Assets/Scripts/Enemy/States/PatrollingState.cs diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs.meta b/Assets/Scripts/Enemy/States/PatrollingState.cs.meta similarity index 100% rename from Assets/Scripts/Enemy/OnePunchMan/States/PatrollingState.cs.meta rename to Assets/Scripts/Enemy/States/PatrollingState.cs.meta diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/RotatingState.cs b/Assets/Scripts/Enemy/States/RotatingState.cs similarity index 100% rename from Assets/Scripts/Enemy/OnePunchMan/States/RotatingState.cs rename to Assets/Scripts/Enemy/States/RotatingState.cs diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/RotatingState.cs.meta b/Assets/Scripts/Enemy/States/RotatingState.cs.meta similarity index 100% rename from Assets/Scripts/Enemy/OnePunchMan/States/RotatingState.cs.meta rename to Assets/Scripts/Enemy/States/RotatingState.cs.meta diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/ShootingState.cs b/Assets/Scripts/Enemy/States/ShootingState.cs similarity index 100% rename from Assets/Scripts/Enemy/OnePunchMan/States/ShootingState.cs rename to Assets/Scripts/Enemy/States/ShootingState.cs diff --git a/Assets/Scripts/Enemy/OnePunchMan/States/ShootingState.cs.meta b/Assets/Scripts/Enemy/States/ShootingState.cs.meta similarity index 100% rename from Assets/Scripts/Enemy/OnePunchMan/States/ShootingState.cs.meta rename to Assets/Scripts/Enemy/States/ShootingState.cs.meta