From c84aa768e51c911cdf82d27f92ebbc4e55b236c2 Mon Sep 17 00:00:00 2001 From: jreason Date: Sun, 30 Jan 2022 18:01:21 +1100 Subject: [PATCH] Working monsters --- Assets/Scenes/MonsterWorking.unity | 3 ++ Assets/Scenes/MonsterWorking.unity.meta | 7 ++++ Assets/Scripts/LevelManager.cs | 2 +- Assets/Scripts/MonsterController.cs | 44 +++++++++++++++++++++---- 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 Assets/Scenes/MonsterWorking.unity create mode 100644 Assets/Scenes/MonsterWorking.unity.meta diff --git a/Assets/Scenes/MonsterWorking.unity b/Assets/Scenes/MonsterWorking.unity new file mode 100644 index 0000000..3ce746f --- /dev/null +++ b/Assets/Scenes/MonsterWorking.unity @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:abd6c264eebe98fefd6c2e4a013567caeb3864a117a6cc8d445d91145f6eb1de +size 110580 diff --git a/Assets/Scenes/MonsterWorking.unity.meta b/Assets/Scenes/MonsterWorking.unity.meta new file mode 100644 index 0000000..ddf9ec3 --- /dev/null +++ b/Assets/Scenes/MonsterWorking.unity.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 72869211a51722a49b486c6163795717 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/LevelManager.cs b/Assets/Scripts/LevelManager.cs index f8c5414..d92c812 100644 --- a/Assets/Scripts/LevelManager.cs +++ b/Assets/Scripts/LevelManager.cs @@ -54,7 +54,7 @@ public class LevelManager : MonoBehaviour private void Start() { - m_allResetable = FindObjectsOfType().OfType().ToArray(); + m_allResetable = FindObjectsOfType(true).OfType().ToArray(); m_timer.Value = m_timeInLevel; m_isVictory.Value = false; diff --git a/Assets/Scripts/MonsterController.cs b/Assets/Scripts/MonsterController.cs index 3afb748..f321ccb 100644 --- a/Assets/Scripts/MonsterController.cs +++ b/Assets/Scripts/MonsterController.cs @@ -14,12 +14,16 @@ public class MonsterController : MonoBehaviour, IResettable private GameObject[] m_possibleFrames; - [SerializeField] private Transform m_player; + public GameObject m_defaultObject; + [SerializeField] + private float m_distance = 3; [SerializeField] - public GameObject m_defaultObject; + private Vector3 m_offset = Vector3.zero; + + private bool m_hasChanged = false; private void OnEnable() { @@ -33,6 +37,7 @@ public class MonsterController : MonoBehaviour, IResettable private void Start() { + m_player = GameObject.FindGameObjectWithTag("Player").transform; foreach (Transform child in transform) { if (child.gameObject.activeInHierarchy) @@ -53,14 +58,35 @@ public class MonsterController : MonoBehaviour, IResettable } } + private void ResetMonster() + { + foreach (Transform child in transform) + { + child.gameObject.SetActive(child.gameObject == m_defaultObject); + } + } + private void OnLightChange(float value) { - if (value <= 0) + float distanceToPlayer = Vector3.Distance(m_player.position, transform.position + m_offset); + + if (value <= 0.1 && distanceToPlayer < m_distance && !m_hasChanged) { + Debug.Log(distanceToPlayer); ChangeRandomMonster(); + m_hasChanged = true; + } + else if (value <= 0) + { + ResetMonster(); + } + if (value > 0.5) + { + m_hasChanged = false; } + } public void OnLevelLoad() @@ -75,9 +101,13 @@ public class MonsterController : MonoBehaviour, IResettable public void OnResetEnd() { - foreach (Transform child in transform) - { - child.gameObject.SetActive(child.gameObject == m_defaultObject); - } + ResetMonster(); + } + + + private void OnDrawGizmosSelected() + { + Gizmos.color = Color.yellow; + Gizmos.DrawWireSphere(transform.position + m_offset, m_distance); } }