From de5942213e89200d26e3e1d82bedbbfb605d4f28 Mon Sep 17 00:00:00 2001 From: Aditya Singh Date: Fri, 22 Dec 2023 15:32:30 +0530 Subject: [PATCH] Game Update : - Create a Teleporting State for Hitman - Create a HitmanController which initializes its state machine and handles player interaction logic. - Create a state machine for Hitman using the GenericStateMachine we created earlier. - Create a new Level & add a few Hitman enemies in it. --- Assets/Scripts/Enemy/EnemyController.cs | 2 +- Assets/Scripts/Enemy/EnemyService.cs | 3 ++ Assets/Scripts/Enemy/Hitman.meta | 8 ++++ .../Scripts/Enemy/Hitman/HitmanController.cs | 45 +++++++++++++++++++ .../Enemy/Hitman/HitmanController.cs.meta | 11 +++++ .../Enemy/Hitman/HitmanStateMachine.cs | 27 +++++++++++ .../Enemy/Hitman/HitmanStateMachine.cs.meta | 11 +++++ .../Scripts/Enemy/States/TeleportingState.cs | 44 ++++++++++++++++++ .../Enemy/States/TeleportingState.cs.meta | 11 +++++ Assets/Scripts/StateMachine/States.cs | 3 +- Packages/packages-lock.json | 12 ++--- ProjectSettings/ProjectVersion.txt | 4 +- 12 files changed, 171 insertions(+), 10 deletions(-) create mode 100644 Assets/Scripts/Enemy/Hitman.meta create mode 100644 Assets/Scripts/Enemy/Hitman/HitmanController.cs create mode 100644 Assets/Scripts/Enemy/Hitman/HitmanController.cs.meta create mode 100644 Assets/Scripts/Enemy/Hitman/HitmanStateMachine.cs create mode 100644 Assets/Scripts/Enemy/Hitman/HitmanStateMachine.cs.meta create mode 100644 Assets/Scripts/Enemy/States/TeleportingState.cs create mode 100644 Assets/Scripts/Enemy/States/TeleportingState.cs.meta diff --git a/Assets/Scripts/Enemy/EnemyController.cs b/Assets/Scripts/Enemy/EnemyController.cs index be39fda4..2e07b7c0 100644 --- a/Assets/Scripts/Enemy/EnemyController.cs +++ b/Assets/Scripts/Enemy/EnemyController.cs @@ -64,7 +64,7 @@ public virtual void Die() public void ToggleEnemyColor(bool value)=> enemyView.ToggleColor(value); - public void Shoot() + public virtual void Shoot() { enemyView.PlayShootingEffect(); GameService.Instance.SoundService.PlaySoundEffects(Sound.SoundType.ENEMY_SHOOT); diff --git a/Assets/Scripts/Enemy/EnemyService.cs b/Assets/Scripts/Enemy/EnemyService.cs index 012dbe7c..f70911dd 100644 --- a/Assets/Scripts/Enemy/EnemyService.cs +++ b/Assets/Scripts/Enemy/EnemyService.cs @@ -60,6 +60,9 @@ public EnemyController CreateEnemy(EnemyScriptableObject enemyScriptableObject) case EnemyType.PatrolMan: enemy = new PatrolManController(enemyScriptableObject); break; + case EnemyType.Hitman: + enemy = new HitmanController(enemyScriptableObject); + break; default: enemy = new EnemyController(enemyScriptableObject); break; diff --git a/Assets/Scripts/Enemy/Hitman.meta b/Assets/Scripts/Enemy/Hitman.meta new file mode 100644 index 00000000..0c5692dd --- /dev/null +++ b/Assets/Scripts/Enemy/Hitman.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 32c62e4af35cc82449b2f7d275498674 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/Hitman/HitmanController.cs b/Assets/Scripts/Enemy/Hitman/HitmanController.cs new file mode 100644 index 00000000..c40f9b7f --- /dev/null +++ b/Assets/Scripts/Enemy/Hitman/HitmanController.cs @@ -0,0 +1,45 @@ +using StatePattern.Player; +using StatePattern.StateMachine; +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace StatePattern.Enemy +{ + public class HitmanController : EnemyController + { + private HitmanStateMachine stateMachine; + + public HitmanController(EnemyScriptableObject enemyScriptableObject) : base(enemyScriptableObject) + { + enemyView.SetController(this); + CreateStateMachine(); + stateMachine.ChangeState(States.IDLE); + } + + private void CreateStateMachine() => stateMachine = new HitmanStateMachine(this); + + public override void UpdateEnemy() + { + if (currentState == EnemyState.DEACTIVE) + return; + + stateMachine.Update(); + } + + public override void Shoot() + { + base.Shoot(); + stateMachine.ChangeState(States.TELEPORTING); + } + + public override void PlayerEnteredRange(PlayerController targetToSet) + { + base.PlayerEnteredRange(targetToSet); + stateMachine.ChangeState(States.CHASING); + } + + public override void PlayerExitedRange() => stateMachine.ChangeState(States.IDLE); + } +} diff --git a/Assets/Scripts/Enemy/Hitman/HitmanController.cs.meta b/Assets/Scripts/Enemy/Hitman/HitmanController.cs.meta new file mode 100644 index 00000000..da02b540 --- /dev/null +++ b/Assets/Scripts/Enemy/Hitman/HitmanController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a6e81718f12d8ca4e9e252343f82aa63 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/Hitman/HitmanStateMachine.cs b/Assets/Scripts/Enemy/Hitman/HitmanStateMachine.cs new file mode 100644 index 00000000..669e6445 --- /dev/null +++ b/Assets/Scripts/Enemy/Hitman/HitmanStateMachine.cs @@ -0,0 +1,27 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using StatePattern.StateMachine; +using System; + +namespace StatePattern.Enemy +{ + public class HitmanStateMachine : GenericStateMachine + { + public HitmanStateMachine(HitmanController Owner) : base(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)); + States.Add(StateMachine.States.TELEPORTING, new TeleportingState(this)); + } + } +} diff --git a/Assets/Scripts/Enemy/Hitman/HitmanStateMachine.cs.meta b/Assets/Scripts/Enemy/Hitman/HitmanStateMachine.cs.meta new file mode 100644 index 00000000..5cf72493 --- /dev/null +++ b/Assets/Scripts/Enemy/Hitman/HitmanStateMachine.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fc0712e89dab2034899e1dd04325ed33 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/States/TeleportingState.cs b/Assets/Scripts/Enemy/States/TeleportingState.cs new file mode 100644 index 00000000..5089665c --- /dev/null +++ b/Assets/Scripts/Enemy/States/TeleportingState.cs @@ -0,0 +1,44 @@ +using StatePattern.Enemy; +using StatePattern.StateMachine; +using System.Collections; +using UnityEngine; +using UnityEngine.AI; + +namespace StatePattern.Enemy +{ + public class TeleportingState : IState where T : EnemyController + { + public EnemyController Owner { get; set; } + private GenericStateMachine stateMachine; + + public TeleportingState(GenericStateMachine stateMachine) => this.stateMachine = stateMachine; + + public void OnStateEnter() + { + TeleportToRandomPosition(); + + stateMachine.ChangeState(States.CHASING); + } + + public void Update() + { + + } + + public void OnStateExit() + { + + } + private void TeleportToRandomPosition() => Owner.Agent.Warp(GetRandomNavMeshPoint()); + + private Vector3 GetRandomNavMeshPoint() + { + Vector3 randomDirection = Random.insideUnitSphere * 5f + Owner.Position; + NavMeshHit hit; + if (NavMesh.SamplePosition(randomDirection, out hit, 5f, NavMesh.AllAreas)) + return hit.position; + else + return Owner.Data.SpawnPosition; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Enemy/States/TeleportingState.cs.meta b/Assets/Scripts/Enemy/States/TeleportingState.cs.meta new file mode 100644 index 00000000..82f783bf --- /dev/null +++ b/Assets/Scripts/Enemy/States/TeleportingState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2902f2f1442c317448a48535dc1e01aa +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/StateMachine/States.cs b/Assets/Scripts/StateMachine/States.cs index 69f9c3c2..6eb26936 100644 --- a/Assets/Scripts/StateMachine/States.cs +++ b/Assets/Scripts/StateMachine/States.cs @@ -6,6 +6,7 @@ public enum States ROTATING, SHOOTING, PATROLLING, - CHASING + CHASING, + TELEPORTING } } \ No newline at end of file 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": { diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index bca3d022..1f883d75 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1,2 +1,2 @@ -m_EditorVersion: 2021.3.21f1 -m_EditorVersionWithRevision: 2021.3.21f1 (1b156197d683) +m_EditorVersion: 2021.3.10f1 +m_EditorVersionWithRevision: 2021.3.10f1 (1c7d0df0160b)