cirnogodot/3D/TrenchBroom/EntityScripts/Triggers/TriggerArea.cs
2025-09-09 16:57:26 +02:00

140 lines
2.9 KiB
C#

using Godot;
using System;
using Cirno.Scripts;
using Cirno.Scripts.Components.FSM._3DPlayer;
using Cirno.Scripts.Utils;
using Godot.Collections;
[Tool]
public partial class TriggerArea : Area3D
{
[Export] public string Target { get; private set; }
[Export] public string TargetFunc { get; private set; }
[Export] public string TargetName { get; private set; }
[Export] public int MinActivations { get; private set; } = 0;
[Export] public int MaxActivations { get; private set; } = 1;
[Export] public ActivationType ActivationType { get; private set; } = ActivationType.Toggle;
public enum TriggerStates
{
READY,
USED,
WAITING
}
private TriggerStates _triggerState = TriggerStates.READY;
private float _timeout = 0f;
private Node _lastActivator;
private int _activations = 0;
public void _func_godot_apply_properties(Dictionary<string, string> props)
{
Target = props["target"];
TargetFunc = props["targetfunc"];
TargetName = props["targetname"];
if (props.TryGetValue("activationtype", out var type))
{
var t = Enum.TryParse(type, true, out ActivationType activationType);
if (t)
{
ActivationType = activationType;
}
}
if (int.TryParse(props["minactivations"], out var minActivations))
{
MinActivations = minActivations;
}
if (int.TryParse(props["maxactivations"], out var maxActivations))
{
MaxActivations = maxActivations;
}
}
public void _on_ent_entered(Node ent)
{
//GD.Print($"Trigger entered by {ent.Name}");
switch (_triggerState)
{
case TriggerStates.READY:
if (ent is IsoPlayerFSMProxy)
{
if (_activations >= MinActivations && _activations < MaxActivations )
{
Use();
}
else
{
_activations++;
}
}
break;
case TriggerStates.USED:
break;
}
// if (_triggerState is TriggerStates.READY)
// {
// if (ent is IsoPlayerFSMProxy)
// {
// //GD.Print($"Entity {ent} is player, trying to use");
// Use();
// }
// else
// {
// //GD.Print($"{ent.Name} was not interaction controller");
// }
// }
// else
// {
// //GD.Print("Trigger was not ready");
// }
}
public void Use()
{
if (_triggerState is not TriggerStates.READY) return;
ActivationHelper.UseTargets(this, Target, ActivationType);
_activations++;
if (_activations >= MaxActivations)
{
_triggerState = TriggerStates.USED;
ToggleCollision(false);
}
}
private void ToggleCollision(bool toggle)
{
Monitoring = toggle;
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
if (Engine.IsEditorHint()) return;
//TBGAME.set_targetname(self, targetname)
if (!string.IsNullOrEmpty(TargetName))
{
this.AddToGroup(TargetName);
}
}
public TriggerArea()
{
Monitoring = true;
Monitorable = false;
BodyEntered += _on_ent_entered;
//AreaEntered += _on_ent_entered;
}
}