From 7a84e52233c46882ee352d31d53f45e494de32b0 Mon Sep 17 00:00:00 2001 From: billc27 <110593711+billc27@users.noreply.github.com> Date: Wed, 1 May 2024 03:03:48 +0700 Subject: [PATCH] feat: one hit kill cheat --- Assets/Scenes/Main.unity | 50 +++++++++++++++++++++++++ Assets/Scripts/Enemy/EnemyHealth.cs | 32 +++++++++++++++- Assets/Scripts/Managers/CheatManager.cs | 15 ++++++-- 3 files changed, 92 insertions(+), 5 deletions(-) diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 4a9d8fc..2da7177 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -2552,6 +2552,55 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1038376789} m_CullTransparentMesh: 1 +--- !u!1 &1124016427 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1124016429} + - component: {fileID: 1124016428} + m_Layer: 0 + m_Name: Enemy + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1124016428 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1124016427} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: aa10cc87aff30c04dbe0aa648f5b6adc, type: 3} + m_Name: + m_EditorClassIdentifier: + startingHealth: 100 + sinkSpeed: 2.5 + scoreValue: 10 + deathClip: {fileID: 0} + isCheatOneHitKill: 0 +--- !u!4 &1124016429 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1124016427} + serializedVersion: 2 + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 638.0238, y: 287.54834, z: 8.902347} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1125301618 GameObject: m_ObjectHideFlags: 0 @@ -4611,6 +4660,7 @@ SceneRoots: m_ObjectHideFlags: 0 m_Roots: - {fileID: 1851598982} + - {fileID: 1124016429} - {fileID: 1641232233} - {fileID: 1139190134} - {fileID: 244922607} diff --git a/Assets/Scripts/Enemy/EnemyHealth.cs b/Assets/Scripts/Enemy/EnemyHealth.cs index 947fe84..4be00f9 100644 --- a/Assets/Scripts/Enemy/EnemyHealth.cs +++ b/Assets/Scripts/Enemy/EnemyHealth.cs @@ -16,6 +16,10 @@ namespace Nightmare CapsuleCollider capsuleCollider; EnemyMovement enemyMovement; + // Cheat One Hit Kill + public bool isCheatOneHitKill = false; + public static bool IsActiveCheatOneHitKill = false; // For indicator to change isCheatOneHitKill + void Awake () { anim = GetComponent <Animator> (); @@ -39,6 +43,8 @@ namespace Nightmare void Update () { + SetCheatOneHitKill(IsActiveCheatOneHitKill); + if (IsDead()) { transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime); @@ -54,14 +60,36 @@ namespace Nightmare return (currentHealth <= 0f); } + // Activate or deactivate cheat one hit kill + public void SetCheatOneHitKill(bool isActive) + { + // Get an array of all EnemyHealth scripts + EnemyHealth[] allEnemies = FindObjectsOfType<EnemyHealth>(); + + // Loop and set isCheatOneHitKill to true on each enemy + foreach (EnemyHealth eHealth in allEnemies) + { + eHealth.isCheatOneHitKill = isActive; + } + + // Change indicator + IsActiveCheatOneHitKill = isActive; + } + public void TakeDamage (int amount, Vector3 hitPoint) { if (!IsDead()) - { + { + // Cheat one hit kill + if (isCheatOneHitKill) + { + currentHealth = 0; + } + enemyAudio.Play(); currentHealth -= amount; - if (IsDead()) + if (currentHealth <= 0) { Death(); } diff --git a/Assets/Scripts/Managers/CheatManager.cs b/Assets/Scripts/Managers/CheatManager.cs index a023b90..62eb488 100644 --- a/Assets/Scripts/Managers/CheatManager.cs +++ b/Assets/Scripts/Managers/CheatManager.cs @@ -22,6 +22,7 @@ public class CheatManager : MonoBehaviour HUDisplay hud; PlayerHealth playerHealth; PlayerMovement playerMovement; + EnemyHealth enemyHealth; string textInput; public InputField inputField; @@ -33,15 +34,20 @@ public class CheatManager : MonoBehaviour hud = GameObject.Find("HUDCanvas").GetComponent<HUDisplay>(); playerHealth = GameObject.Find("Player").GetComponent<PlayerHealth>(); playerMovement = GameObject.Find("Player").GetComponent<PlayerMovement>(); + enemyHealth = GameObject.Find("Enemy").GetComponent<EnemyHealth>(); } private void Update() { - if (UnityEngine.Input.GetKeyDown(KeyCode.Y)) // Open + // Reason: the read string conditionals do not contain Y or Z character + // Open input field by pressing Y key + if (UnityEngine.Input.GetKeyDown(KeyCode.Y)) { hud.OpenInput(); } - if (UnityEngine.Input.GetKeyDown(KeyCode.Z)) // Close + + // Close input field by pressing Z key + if (UnityEngine.Input.GetKeyDown(KeyCode.Z)) { hud.CloseInput(); } @@ -103,7 +109,9 @@ public class CheatManager : MonoBehaviour private void ActivateOneHitKill() { - // TODO + enemyHealth.SetCheatOneHitKill(true); + hud.OpenPanel("One Hit Kill Cheat Activated!"); + cheats[(int)CheatsType.ONEHITKILL] = true; } private void ActivateXTwoSpeed() @@ -117,6 +125,7 @@ public class CheatManager : MonoBehaviour { playerHealth.SetCheatNoDamage(false); playerMovement.ResetSpeed(); + enemyHealth.SetCheatOneHitKill(false); hud.OpenPanel("Successfully Reset Cheat(s)!"); } -- GitLab