2025-07-15 14:44:28 +02:00

141 lines
4.5 KiB
C#

//============= Copyright (c) Ludic GmbH, All rights reserved. ==============
//
// Purpose: Part of the My Behaviour Tree Controller Code
//
//=============================================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyBT;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(NamedGameObject))]
public class NamedGameObjectInspector : ComponentHandlerInspector {
}
#endif
public class NamedGameObject : ComponentHandler {
public override string TypeLabel () {
return "GO";
}
public override string ContentLabel() {
UpdateComponent();
return objName;
}
public override void UpdateComponent() {
base.UpdateComponent();
go = gameObject;
}
public override void Show(MyBT.NodeState nodeState) {
if ((nodeState == NodeState.FirstRun) || (nodeState == NodeState.Running)) {
gameObject.SetActive(true);
Task.SetSucceeded();
}
}
public override void Hide(MyBT.NodeState nodeState) {
if ((nodeState == NodeState.FirstRun) || (nodeState == NodeState.Running)) {
gameObject.SetActive(false);
Task.SetSucceeded();
}
}
public override string[][] helpText {
get {
return new string[][] {
new string[] {"Show", $"BTC.Show(\"{gameObject.name}\")"},
new string[] {"Hide", $"BTC.Hide(\"{gameObject.name}\")"},
new string[] {"Set", $"BTC.Set(\"{gameObject.name}\", \"parent\", \"TARGETOBJECT\")"}
};
}
}
public override void Set(MyBT.NodeState nodeState, string key, string value) {
if ((nodeState == NodeState.FirstRun) || (nodeState == NodeState.Running)) {
if (key == "parent") {
Transform newParent = null;
List<ComponentHandler> componentHandlers = BTC.Instance.GetHandlers(value);
if (componentHandlers.Count > 0) {
newParent = componentHandlers[0].gameObject.transform;
}
gameObject.transform.SetParent(newParent);
gameObject.transform.localPosition = Vector3.zero;
gameObject.transform.localRotation = Quaternion.identity;
gameObject.transform.localScale = Vector3.one;
Task.SetSucceeded();
return;
}
gameObject.transform.localPosition = Vector3.zero;
gameObject.transform.localRotation = Quaternion.identity;
gameObject.transform.localScale = Vector3.one;
Task.SetFailed();
}
}
public override void SetPosition(NodeState nodeState, float newX, float newY, float newZ)
{
if ((nodeState == NodeState.FirstRun) || (nodeState == NodeState.Running))
{
Vector3 newPos = new Vector3(newX, newY, newZ);
gameObject.transform.localPosition = newPos;
Task.SetSucceeded();
}
}
public override void RotateToAngle(NodeState nodeState, float targetAngle)
{
if ((nodeState == NodeState.FirstRun) || (nodeState == NodeState.Running))
{
// Rotate y-Achse
gameObject.transform.rotation = Quaternion.Euler(0, targetAngle, 0);
Task.SetSucceeded();
}
}
public override void DetectRotationAtLeast(NodeState nodeState, float minRotation)
{
if (nodeState == NodeState.FirstRun)
{
_startQuaternion = gameObject.transform.rotation;
}
else if (nodeState == NodeState.Running)
{
float angleOffset = Quaternion.Angle(_startQuaternion, gameObject.transform.rotation);
if (angleOffset > minRotation)
{
Task.SetSucceeded();
}
}
}
public override void DetectPositionAtLeast(NodeState nodeState, float minDistance)
{
if (nodeState == NodeState.FirstRun)
{
_initialPosition = gameObject.transform.position;
}
else if (nodeState == NodeState.Running)
{
float movedDistance = Vector3.Distance(_initialPosition, gameObject.transform.position);
if (movedDistance > minDistance)
{
Task.SetSucceeded();
}
}
}
public GameObject go;
public string objName = "GoXY";
Quaternion _startQuaternion;
Vector3 _initialPosition;
}