cirnogodot/3D/TrenchBroom/EntityScripts/Triggers/TriggerArea.cs

147 lines
3.2 KiB
C#
Raw Permalink Normal View History

2025-06-18 11:33:27 +02:00
using Godot;
using System;
2025-09-10 11:08:47 +02:00
using Cirno._3D.TrenchBroom.EntityScripts.Triggers;
2025-06-18 11:33:27 +02:00
using Cirno.Scripts;
using Cirno.Scripts.Components.FSM._3DPlayer;
2025-07-04 10:31:23 +02:00
using Cirno.Scripts.Utils;
2025-06-18 11:33:27 +02:00
using Godot.Collections;
[Tool]
2025-09-10 11:08:47 +02:00
public partial class TriggerArea : Area3D, ITargetable, ITargeting, IActivationType
2025-06-18 11:33:27 +02:00
{
2025-09-10 11:08:47 +02:00
[Export] public string Target { get; set; }
2025-06-18 11:33:27 +02:00
[Export] public string TargetFunc { get; private set; }
2025-09-10 11:08:47 +02:00
[Export] public string TargetName { get; set; }
2025-06-18 11:33:27 +02:00
2025-09-09 16:57:26 +02:00
[Export] public int MinActivations { get; private set; } = 0;
[Export] public int MaxActivations { get; private set; } = 1;
2025-09-10 11:08:47 +02:00
[Export] public ActivationType ActivationType { get; set; } = ActivationType.Toggle;
2025-06-27 21:28:01 +02:00
2025-06-18 11:33:27 +02:00
public enum TriggerStates
{
READY,
2025-09-09 16:57:26 +02:00
USED,
WAITING
2025-06-18 11:33:27 +02:00
}
private TriggerStates _triggerState = TriggerStates.READY;
private float _timeout = 0f;
private Node _lastActivator;
2025-09-09 16:57:26 +02:00
private int _activations = 0;
2025-09-10 11:08:47 +02:00
public void _func_godot_apply_properties(Dictionary<string, Variant> props)
2025-06-18 11:33:27 +02:00
{
2025-09-10 11:08:47 +02:00
((ITargetable)this).ApplyTargetProperties(props);
((ITargeting)this).ApplyTargetingProperties(props);
//Target = props["target"];
TargetFunc = props["targetfunc"].AsString();
//TargetName = props["targetname"];
// if (props.TryGetValue("activationtype", out var type))
// {
// var t = Enum.TryParse(type.AsString(), true, out ActivationType activationType);
// if (t)
// {
// ActivationType = activationType;
// }
// }
((IActivationType)this).ApplyActivationTypeProperties(props);
2025-09-09 16:57:26 +02:00
2025-09-10 11:08:47 +02:00
if (props.TryGetValue("minactivations", out var minActivations))
2025-09-09 16:57:26 +02:00
{
2025-09-10 11:08:47 +02:00
MinActivations = minActivations.AsInt32();
2025-09-09 16:57:26 +02:00
}
2025-09-10 11:08:47 +02:00
if (props.TryGetValue("maxactivations", out var maxActivations))
2025-09-09 16:57:26 +02:00
{
2025-09-10 11:08:47 +02:00
MaxActivations = maxActivations.AsInt32();
2025-09-09 16:57:26 +02:00
}
2025-06-18 11:33:27 +02:00
}
public void _on_ent_entered(Node ent)
{
2025-09-09 16:57:26 +02:00
//GD.Print($"Trigger entered by {ent.Name}");
switch (_triggerState)
2025-06-18 11:33:27 +02:00
{
2025-09-09 16:57:26 +02:00
case TriggerStates.READY:
if (ent is IsoPlayerFSMProxy)
{
if (_activations >= MinActivations && _activations < MaxActivations )
{
Use();
}
else
{
_activations++;
}
}
break;
case TriggerStates.USED:
break;
2025-06-18 11:33:27 +02:00
}
2025-09-09 16:57:26 +02:00
// 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");
// }
2025-06-18 11:33:27 +02:00
}
public void Use()
{
2025-09-09 16:57:26 +02:00
if (_triggerState is not TriggerStates.READY) return;
ActivationHelper.UseTargets(this, Target, ActivationType);
_activations++;
if (_activations >= MaxActivations)
2025-06-18 11:33:27 +02:00
{
_triggerState = TriggerStates.USED;
ToggleCollision(false);
}
}
private void ToggleCollision(bool toggle)
{
2025-09-10 16:16:05 +02:00
SetDeferred(nameof(Monitoring), toggle);
//Monitoring = toggle;
2025-06-18 11:33:27 +02:00
}
// 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;
}
}