added: expanded environment observers with changed types, better gizmos, some fixes

This commit is contained in:
Chris
2025-07-22 16:12:40 -04:00
parent 43de6b0d3b
commit 355f6207a5
3 changed files with 133 additions and 7 deletions

View File

@@ -0,0 +1,38 @@
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
namespace NodeCanvas.Tasks.Conditions {
[Category("Reset")]
public class CheckEnvironmentObserver : ConditionTask<PlayerEnvironmentManager>{
public BBParameter<string> 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);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 95ad4bd047d9654478597c68a81b01a0

View File

@@ -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<EnvironmentObserver> 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<EnvironmentObserver> observerList = null){
List<EnvironmentObserver> 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);
}
}
}
}
}