Files
2026-06-01 17:19:55 +02:00

106 lines
2.2 KiB
C#

using System;
using UnityEngine;
[DisallowMultipleComponent]
public sealed class GetPositionBehavior : NodeBehavior
{
private bool evaluating;
private void Start()
{
Recompute();
}
private void Update()
{
Recompute();
}
public override void OnInputValueChanged(string inputPortName, BSValue value)
{
Recompute();
}
private void Recompute()
{
if (Node == null)
{
return;
}
if (evaluating)
{
ClearOutputsForCurrentTarget();
return;
}
evaluating = true;
try
{
BSValue inputValue = GetInputValue("Group");
if (inputValue.Kind == BSValueKind.Player)
{
RecomputePlayer(inputValue.AsPlayerId());
}
else if (base.IsAuthoritative)
{
if (!TryGetInputGroup("Group", out var group) || group == null)
{
ClearOutputsSynced();
return;
}
SetOutputValueIfChanged("X", BSValue.FromNumber(group.position.x));
SetOutputValueIfChanged("Y", BSValue.FromNumber(group.position.y));
SetOutputValueIfChanged("Z", BSValue.FromNumber(group.position.z));
}
}
catch (Exception exception)
{
Debug.LogException(exception, this);
ClearOutputsForCurrentTarget();
}
finally
{
evaluating = false;
}
}
private void RecomputePlayer(int playerId)
{
Transform transform = NodePlayerUtility.ResolveHeadTransform(NodePlayerUtility.ResolvePlayer(playerId));
if (transform == null)
{
ClearOutputsLocal();
return;
}
Vector3 vector = NodePlayerUtility.WorldToVoxelPosition(transform.position);
SetLocalOutputValueIfChanged("X", BSValue.FromNumber(vector.x));
SetLocalOutputValueIfChanged("Y", BSValue.FromNumber(vector.y));
SetLocalOutputValueIfChanged("Z", BSValue.FromNumber(vector.z));
}
private void ClearOutputsLocal()
{
SetLocalOutputValueIfChanged("X", BSValue.None);
SetLocalOutputValueIfChanged("Y", BSValue.None);
SetLocalOutputValueIfChanged("Z", BSValue.None);
}
private void ClearOutputsSynced()
{
SetOutputValueIfChanged("X", BSValue.None);
SetOutputValueIfChanged("Y", BSValue.None);
SetOutputValueIfChanged("Z", BSValue.None);
}
private void ClearOutputsForCurrentTarget()
{
if (GetInputValue("Group").Kind == BSValueKind.Player)
{
ClearOutputsLocal();
}
else if (base.IsAuthoritative)
{
ClearOutputsSynced();
}
}
}