added: player environment manager, and accompanying aline and odin changes
This commit is contained in:
146
Assets/Scripts/Player/PlayerEnvironmentManager.cs
Normal file
146
Assets/Scripts/Player/PlayerEnvironmentManager.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user