added: player environment manager, and accompanying aline and odin changes

This commit is contained in:
Chris
2025-07-18 20:06:38 -04:00
parent 185580c3af
commit 43de6b0d3b
12 changed files with 438 additions and 28 deletions

View File

@@ -0,0 +1,146 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using Drawing;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using Unity.Mathematics;
[Serializable]
public class EnvironmentObserver{
// TODO: Clean this ugly shit up.
public string label;
public bool drawLabel;
enum LabelDrawingLocation{
PlayerOffset,
HitLocation,
IntersectingLength,
}
[SerializeReference]
public List<EnvironmentObserver> children;
public Vector3 labelLocationOffset;
public Vector3 labelRotationOffset;
[ShowInInspector]
LabelDrawingLocation labelLocation;
public bool active;
public float length;
public float width;
public Vector3 start;
public Vector3 direction;
public bool useLayerMask;
public LayerMask ignoreMask;
public RaycastHit hit;
public bool Evaluate(GameObject player){
if (active) {
ignoreMask -= player.layer;
Vector3 relativeStart = player.transform.position + start;
// 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);
if (hit.transform != null) {
return true;
}
}
hit = default;
return false;
}
public void DrawObserverGizmo(GameObject player){
Vector3 relativeStart = player.transform.position + start;
using (Draw.ingame.WithColor(Evaluate(player) ? Color.green : Color.red)) {
Vector3 labelStartPos = Vector3.zero;
switch (labelLocation) {
case LabelDrawingLocation.PlayerOffset:
labelStartPos = player.transform.position + labelLocationOffset;
break;
case LabelDrawingLocation.IntersectingLength:
labelStartPos = (relativeStart + (player.transform.rotation * direction * length) / 2f ) + labelLocationOffset;
break;
case LabelDrawingLocation.HitLocation:{
if (hit.transform != null) {
labelStartPos = hit.point + labelLocationOffset;
}
break;
}
}
Draw.ingame.Line(relativeStart, relativeStart + (player.transform.rotation * direction) * length);
Draw.ingame.Label3D(
labelStartPos,
player.transform.rotation * Quaternion.Euler(labelRotationOffset),
hit.collider == null ? "" : hit.collider.name,
.5f,
LabelAlignment.Center,
Color.black
);
}
}
}
public class PlayerEnvironmentManager : MonoBehaviour{
[OdinSerialize]
public List<EnvironmentObserver> observers;
void Start(){
CheckDuplicateLabels(observers);
}
// TODO: Not working.
void CheckDuplicateLabels(List<EnvironmentObserver> sourceList){
foreach (EnvironmentObserver sourceObserver in observers) {
foreach (EnvironmentObserver observer in sourceList) {
if (sourceObserver == observer) {
continue;
}
if (sourceObserver.label == observer.label) {
Debug.LogError($"Duplicate label found in observer: {observer.label} is in use multiple times");
}
if (observer.children != null && observer.children.Count > 0) {
CheckDuplicateLabels(observer.children);
}
}
}
}
public bool EvaluateFromString(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 true;
}
if (observer.children != null && observer.children.Count > 0) {
foreach (EnvironmentObserver childObserver in observer.children) {
EvaluateFromString(searchLabel, childObserver.children);
}
}
}
return false;
}
void LateUpdate(){
foreach (EnvironmentObserver observer in observers) {
observer.DrawObserverGizmo(gameObject);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 7a8c0593fef54844383c2f154cf8806c

View File

@@ -15,28 +15,28 @@ public class PlayerMovement : MonoBehaviour
thisPlayer = GetComponent<Player>();
}
void Update(){
// Create Ray Colors
Color forwardRayStatus = Color.red;
Color leftRayStatus = Color.red;
Color rightRayStatus = Color.red;
if (forwardRay.collider&& forwardRay.transform.gameObject.layer == LayerMask.NameToLayer("Environment")){ forwardRayStatus = Color.green;}
if (leftRay.collider&& leftRay.transform.gameObject.layer == LayerMask.NameToLayer("Environment")){ leftRayStatus = Color.green;}
if (rightRay.collider&& rightRay.transform.gameObject.layer == LayerMask.NameToLayer("Environment")){ rightRayStatus = Color.green;}
using (Draw.WithColor(forwardRayStatus)) {
Draw.Line(transform.position + transform.up, transform.position + transform.forward * 2.5f + transform.up);
}
using (Draw.WithColor(leftRayStatus)) {
Draw.Line(transform.position + transform.up, transform.position + -transform.right * 2f + transform.up);
}
using (Draw.WithColor(rightRayStatus)) {
Draw.Line(transform.position + transform.up, transform.position + transform.right * 2f + transform.up);
}
}
// void Update(){
// // Create Ray Colors
// Color forwardRayStatus = Color.red;
// Color leftRayStatus = Color.red;
// Color rightRayStatus = Color.red;
//
// if (forwardRay.collider&& forwardRay.transform.gameObject.layer == LayerMask.NameToLayer("Environment")){ forwardRayStatus = Color.green;}
// if (leftRay.collider&& leftRay.transform.gameObject.layer == LayerMask.NameToLayer("Environment")){ leftRayStatus = Color.green;}
// if (rightRay.collider&& rightRay.transform.gameObject.layer == LayerMask.NameToLayer("Environment")){ rightRayStatus = Color.green;}
//
// using (Draw.WithColor(forwardRayStatus)) {
// Draw.Line(transform.position + transform.up, transform.position + transform.forward * 2.5f + transform.up);
// }
//
// using (Draw.WithColor(leftRayStatus)) {
// Draw.Line(transform.position + transform.up, transform.position + -transform.right * 2f + transform.up);
// }
//
// using (Draw.WithColor(rightRayStatus)) {
// Draw.Line(transform.position + transform.up, transform.position + transform.right * 2f + transform.up);
// }
// }
void FixedUpdate(){
LayerMask environmentLayer = LayerMask.NameToLayer("Environment");

View File

@@ -8,7 +8,8 @@
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:de4e6084e6d474788bb8c799d6b461ec",
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:4307f53044263cf4b835bd812fc161a4"
"GUID:4307f53044263cf4b835bd812fc161a4",
"GUID:e0cd26848372d4e5c891c569017e11f1"
],
"includePlatforms": [],
"excludePlatforms": [],