From 355f6207a57e6151d0f4fa5057c006bfe7f1b033 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 22 Jul 2025 16:12:40 -0400 Subject: [PATCH] added: expanded environment observers with changed types, better gizmos, some fixes --- .../Graph Tasks/CheckEnvironmentObserver.cs | 38 +++++++ .../CheckEnvironmentObserver.cs.meta | 2 + .../Player/PlayerEnvironmentManager.cs | 100 ++++++++++++++++-- 3 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 Assets/Scripts/Core/Graph Tasks/CheckEnvironmentObserver.cs create mode 100644 Assets/Scripts/Core/Graph Tasks/CheckEnvironmentObserver.cs.meta diff --git a/Assets/Scripts/Core/Graph Tasks/CheckEnvironmentObserver.cs b/Assets/Scripts/Core/Graph Tasks/CheckEnvironmentObserver.cs new file mode 100644 index 0000000..3b56001 --- /dev/null +++ b/Assets/Scripts/Core/Graph Tasks/CheckEnvironmentObserver.cs @@ -0,0 +1,38 @@ +using NodeCanvas.Framework; +using ParadoxNotion.Design; +using UnityEngine; + + +namespace NodeCanvas.Tasks.Conditions { + [Category("Reset")] + public class CheckEnvironmentObserver : ConditionTask{ + + public BBParameter observerLabel; + //Use for initialization. This is called only once in the lifetime of the task. + //Return null if init was successfull. Return an error string otherwise + protected override string OnInit(){ + var observer = agent.FindObserverFromString(observerLabel.value); + if (observer == null) { + return $"An environment observer couldn't be found under the name {observerLabel.value}. Check your spelling and if it exists??"; + } + + return null; + } + + //Called whenever the condition gets enabled. + protected override void OnEnable() { + agent.FindObserverFromString(observerLabel.value).active = true; + } + + //Called whenever the condition gets disabled. + protected override void OnDisable() { + agent.FindObserverFromString(observerLabel.value).active = false; + } + + //Called once per frame while the condition is active. + //Return whether the condition is success or failure. + protected override bool OnCheck(){ + return agent.EvaluateFromString(observerLabel.value); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Core/Graph Tasks/CheckEnvironmentObserver.cs.meta b/Assets/Scripts/Core/Graph Tasks/CheckEnvironmentObserver.cs.meta new file mode 100644 index 0000000..7af0474 --- /dev/null +++ b/Assets/Scripts/Core/Graph Tasks/CheckEnvironmentObserver.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 95ad4bd047d9654478597c68a81b01a0 \ No newline at end of file diff --git a/Assets/Scripts/Player/PlayerEnvironmentManager.cs b/Assets/Scripts/Player/PlayerEnvironmentManager.cs index 04a57d6..4175cb3 100644 --- a/Assets/Scripts/Player/PlayerEnvironmentManager.cs +++ b/Assets/Scripts/Player/PlayerEnvironmentManager.cs @@ -8,7 +8,7 @@ using Unity.Mathematics; [Serializable] public class EnvironmentObserver{ - // TODO: Clean this ugly shit up. + // TODO: Clean this ugly shit up. Custom inspector with Odin plz. public string label; public bool drawLabel; @@ -18,19 +18,35 @@ public class EnvironmentObserver{ IntersectingLength, } + enum CastType{ + Ray, + Box, + Sphere, + BoxCast, + SphereCast + } + + [ShowInInspector, SerializeField] + CastType castType; + [SerializeReference] public List children; public Vector3 labelLocationOffset; public Vector3 labelRotationOffset; - [ShowInInspector] + [ShowInInspector, SerializeField] LabelDrawingLocation labelLocation; public bool active; + + // Parameters public float length; public float width; + public Vector3 size; + public Vector3 rotation; + public Vector3 start; public Vector3 direction; @@ -38,14 +54,34 @@ public class EnvironmentObserver{ public LayerMask ignoreMask; public RaycastHit hit; + private Collider[] overlapHits; public bool Evaluate(GameObject player){ if (active) { ignoreMask -= player.layer; Vector3 relativeStart = player.transform.position + start; + Vector3 startOffsetFromRotation = player.transform.position + player.transform.rotation * (start) ; + float halfExtents = width / 2f; + // NOTE: I had a ref for a RaycastHit here that would correspond to hit but idk if it's needed. - Physics.Raycast(relativeStart, player.transform.rotation * direction, out hit, length, ignoreMask); + + switch (castType) { + case CastType.Ray: + Physics.Raycast(relativeStart, player.transform.rotation * direction, out hit, length, ignoreMask); + break; + case CastType.Box: + if (Physics.OverlapBox(startOffsetFromRotation, size / 2f, player.transform.rotation * Quaternion.Euler(rotation), ignoreMask).Length > 0) { + return true; + }; + break; + case CastType.Sphere: + break; + case CastType.BoxCast: + break; + case CastType.SphereCast: + break; + } if (hit.transform != null) { return true; @@ -57,8 +93,12 @@ public class EnvironmentObserver{ public void DrawObserverGizmo(GameObject player){ Vector3 relativeStart = player.transform.position + start; + Vector3 startOffsetFromRotation = player.transform.position + player.transform.rotation * (start) ; + + Color gizmoColor = Evaluate(player) ? Color.green : Color.red; + gizmoColor = active ? gizmoColor : Color.gray; - using (Draw.ingame.WithColor(Evaluate(player) ? Color.green : Color.red)) { + using (Draw.ingame.WithColor(gizmoColor)){ Vector3 labelStartPos = Vector3.zero; switch (labelLocation) { case LabelDrawingLocation.PlayerOffset: @@ -75,8 +115,22 @@ public class EnvironmentObserver{ break; } } - - Draw.ingame.Line(relativeStart, relativeStart + (player.transform.rotation * direction) * length); + + switch (castType) { + case CastType.Ray: + Draw.ingame.Line(relativeStart, relativeStart + (player.transform.rotation * direction.normalized) * length); + break; + case CastType.Box: + Draw.ingame.WireBox(startOffsetFromRotation, player.transform.rotation * Quaternion.Euler(rotation), size); + break; + case CastType.Sphere: + break; + case CastType.BoxCast: + break; + default: + throw new ArgumentOutOfRangeException(); + } + Draw.ingame.Label3D( labelStartPos, player.transform.rotation * Quaternion.Euler(labelRotationOffset), @@ -125,7 +179,7 @@ public class PlayerEnvironmentManager : MonoBehaviour{ foreach (EnvironmentObserver observer in listToUse) { if (observer.label == searchLabel) { - return true; + return observer.Evaluate(gameObject); } if (observer.children != null && observer.children.Count > 0) { @@ -137,10 +191,42 @@ public class PlayerEnvironmentManager : MonoBehaviour{ return false; } + + public EnvironmentObserver FindObserverFromString(string searchLabel, List observerList = null){ + List listToUse = observers; + + if (observerList != null) { + listToUse = observerList; + } + + foreach (EnvironmentObserver observer in listToUse) { + if (observer.label == searchLabel) { + return observer; + } + if (observer.children != null && observer.children.Count > 0) { + foreach (EnvironmentObserver childObserver in observer.children) { + FindObserverFromString(searchLabel, childObserver.children); + } + } + } + + return null; + } + + void Update(){ + Debug.Log(EvaluateFromString("gamedev")); + } + void LateUpdate(){ foreach (EnvironmentObserver observer in observers) { observer.DrawObserverGizmo(gameObject); + + if (observer.children != null && observer.children.Count > 0) { + foreach (EnvironmentObserver childObserver in observer.children) { + childObserver.DrawObserverGizmo(gameObject); + } + } } } }