298 lines
11 KiB
C#
298 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Drawing;
|
|
using Sirenix.OdinInspector;
|
|
using Sirenix.Serialization;
|
|
using Unity.Mathematics;
|
|
using UnityEngine.Serialization;
|
|
|
|
[Serializable]
|
|
public class EnvironmentObserver{
|
|
// TODO: Clean this ugly shit up. Custom inspector with Odin plz.
|
|
public string label;
|
|
|
|
enum LabelDrawingLocation{
|
|
PlayerOffset,
|
|
HitLocation,
|
|
IntersectingLength,
|
|
}
|
|
|
|
public enum CastType{
|
|
Ray,
|
|
BoxOverlap,
|
|
SphereOverlap,
|
|
BoxCast,
|
|
SphereCast
|
|
}
|
|
|
|
// [BoxGroup("Settings")]
|
|
[ShowInInspector, SerializeField] public CastType castType;
|
|
|
|
[Button(ButtonSizes.Large), GUIColor("@GetObserverStatusColorStatic(active, hit)")]
|
|
public void Active(){
|
|
active = !active;
|
|
}
|
|
|
|
[HideInInspector]
|
|
public bool active;
|
|
|
|
// Parameters for Cast cast types
|
|
[BoxGroup("Settings")]
|
|
public float length;
|
|
[BoxGroup("Settings")]
|
|
public Vector3 direction;
|
|
[BoxGroup("Settings")]
|
|
public Vector3 offset;
|
|
|
|
[ShowIfGroup("Settings/Casts3D", VisibleIf = "@castType == CastType.Ray || castType == CastType.BoxCast || castType == CastType.SphereCast")]
|
|
[BoxGroup("Settings/Casts3D")]
|
|
public float width;
|
|
|
|
|
|
// Parameters for Overlap cast types
|
|
[BoxGroup("Settings/Overlaps")]
|
|
public Vector3 size;
|
|
|
|
[PropertyTooltip(
|
|
"Only use rotation when an observers direction alone can't (for whatever reason) fully be expressed without specifying a rotation.")]
|
|
[ShowIfGroup("Settings/Casts3D")]
|
|
[BoxGroup("Settings/Casts3D")]
|
|
public Vector3 rotation;
|
|
|
|
[BoxGroup("Settings")]
|
|
public LayerMask ignoreLayers = ~0;
|
|
|
|
public RaycastHit hit;
|
|
|
|
[SerializeReference]
|
|
public List<EnvironmentObserver> children;
|
|
|
|
[HideInInspector]
|
|
public Collider[] overlapHits;
|
|
|
|
public bool drawLabel;
|
|
public Vector3 labelLocationOffset;
|
|
public Vector3 labelRotationOffset;
|
|
|
|
[ShowInInspector, SerializeField]
|
|
LabelDrawingLocation labelLocation;
|
|
|
|
// NOTE: I had a ref for a RaycastHit here that would correspond to hit but idk if it's needed.
|
|
public bool Evaluate(GameObject player){
|
|
if (active) {
|
|
// Remove player's layer from LayerMask.
|
|
ignoreLayers -= player.layer;
|
|
|
|
// Set some of the variables used later during casting
|
|
Vector3 relativeStart = player.transform.position + offset;
|
|
Vector3 relativeStartWithRotation = player.transform.position + player.transform.rotation * (offset) ;
|
|
|
|
switch (castType) {
|
|
case CastType.Ray:
|
|
Physics.Raycast(relativeStart, player.transform.rotation * direction, out hit, length, ignoreLayers);
|
|
break;
|
|
case CastType.BoxOverlap:
|
|
overlapHits = Physics.OverlapBox(relativeStartWithRotation, size / 2f,
|
|
player.transform.rotation * Quaternion.Euler(rotation), ignoreLayers);
|
|
|
|
if (overlapHits.Length > 0) {
|
|
return true;
|
|
};
|
|
|
|
break;
|
|
case CastType.SphereOverlap:
|
|
break;
|
|
case CastType.BoxCast:
|
|
// TODO: Make this not an if statement. Check that it works with NodeCanvas first
|
|
if (Physics.BoxCast(relativeStartWithRotation, Vector3.one * (width / 2f), (player.transform.rotation * Quaternion.Euler(rotation) * direction),
|
|
out hit, Quaternion.LookRotation(direction) * Quaternion.Euler(rotation), length, ignoreLayers)
|
|
);
|
|
break;
|
|
case CastType.SphereCast:
|
|
break;
|
|
}
|
|
|
|
if (hit.transform != null) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void DrawObserverGizmo(GameObject player){
|
|
Vector3 relativeStart = player.transform.position + offset;
|
|
Vector3 relativeStartWithRotation = player.transform.position + player.transform.rotation * (offset) ;
|
|
|
|
Color gizmoColor = Evaluate(player) ? Color.green : Color.red;
|
|
gizmoColor = active ? gizmoColor : Color.gray;
|
|
|
|
using (Draw.ingame.WithColor(gizmoColor)){
|
|
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;
|
|
}
|
|
}
|
|
|
|
switch (castType) {
|
|
case CastType.Ray:
|
|
Draw.ingame.Line(relativeStart, relativeStart + (player.transform.rotation * direction.normalized) * length);
|
|
break;
|
|
case CastType.BoxOverlap:
|
|
Draw.ingame.WireBox(relativeStartWithRotation, player.transform.rotation * Quaternion.Euler(rotation), size);
|
|
break;
|
|
case CastType.SphereOverlap:
|
|
break;
|
|
case CastType.BoxCast:
|
|
// Create an offset start point for the center of the wirebox, since gizmos are drawn with their pivot in the center.
|
|
Vector3 offsetWithRotationAndLength = (Quaternion.LookRotation(direction) * Quaternion.Euler(rotation)) * (Vector3.forward * length / 2f);
|
|
Vector3 offsetFromCenter = relativeStartWithRotation + player.transform.rotation * offsetWithRotationAndLength;
|
|
|
|
// Also create a rotation for use with the gizmos. Mainly just to shorten the lines
|
|
Quaternion gizmosRotation = player.transform.rotation * Quaternion.LookRotation(direction) * Quaternion.Euler(rotation);
|
|
|
|
// Draw the gizmos for the boxcast
|
|
Draw.ingame.WireBox(relativeStartWithRotation, gizmosRotation, Vector3.one * width);
|
|
Draw.ingame.WireBox(offsetFromCenter, gizmosRotation, new float3(width, width, length + width));
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
|
|
Draw.ingame.Label3D(
|
|
labelStartPos,
|
|
player.transform.rotation * Quaternion.Euler(labelRotationOffset),
|
|
hit.collider == null ? "" : hit.collider.name,
|
|
.5f,
|
|
LabelAlignment.Center,
|
|
Color.black
|
|
);
|
|
|
|
Sirenix.Utilities.Editor.GUIHelper.RequestRepaint();
|
|
}
|
|
}
|
|
|
|
static Color GetObserverStatusColorStatic(bool active, RaycastHit hit){
|
|
if (active) {
|
|
if (hit.Equals(default(RaycastHit))) {
|
|
return Color.green;
|
|
}
|
|
|
|
return Color.red;
|
|
}
|
|
|
|
return Color.gray;
|
|
}
|
|
}
|
|
|
|
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 observer.Evaluate(gameObject);
|
|
}
|
|
|
|
if (observer.children != null && observer.children.Count > 0) {
|
|
foreach (EnvironmentObserver childObserver in observer.children) {
|
|
EvaluateFromString(searchLabel, childObserver.children);
|
|
}
|
|
}
|
|
}
|
|
|
|
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(){
|
|
// Draw Gizmos
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Clear hit
|
|
foreach (EnvironmentObserver observer in observers) {
|
|
observer.hit = default;
|
|
|
|
if (observer.children != null && observer.children.Count > 0) {
|
|
foreach (EnvironmentObserver childObserver in observer.children) {
|
|
childObserver.hit = default;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|