Interface default functions

This commit is contained in:
Marco 2025-09-10 11:08:47 +02:00
commit 5e357e1a96
4 changed files with 75 additions and 25 deletions

View file

@ -0,0 +1,42 @@
using System;
using Cirno.Scripts;
using Godot;
using Godot.Collections;
namespace Cirno._3D.TrenchBroom.EntityScripts.Triggers;
public interface ITargetable
{
string TargetName { get; set; }
void ApplyTargetProperties(Dictionary<string, Variant> props)
{
TargetName = props["targetname"].AsString();
}
}
public interface ITargeting
{
string Target { get; set; }
void ApplyTargetingProperties(Dictionary<string, Variant> props)
{
Target = props["target"].AsString();
}
}
public interface IActivationType
{
ActivationType ActivationType { get; set; }
void ApplyActivationTypeProperties(Dictionary<string, Variant> props)
{
if (props.TryGetValue("activationtype", out var type))
{
var t = Enum.TryParse(type.AsString(), true, out ActivationType activationType);
if (t)
{
ActivationType = activationType;
}
}
}
}

View file

@ -0,0 +1 @@
uid://cdvucuir4p2hy

View file

@ -1,21 +1,22 @@
using Godot; using Godot;
using System; using System;
using Cirno._3D.TrenchBroom.EntityScripts.Triggers;
using Cirno.Scripts; using Cirno.Scripts;
using Cirno.Scripts.Components.FSM._3DPlayer; using Cirno.Scripts.Components.FSM._3DPlayer;
using Cirno.Scripts.Utils; using Cirno.Scripts.Utils;
using Godot.Collections; using Godot.Collections;
[Tool] [Tool]
public partial class TriggerArea : Area3D public partial class TriggerArea : Area3D, ITargetable, ITargeting, IActivationType
{ {
[Export] public string Target { get; private set; } [Export] public string Target { get; set; }
[Export] public string TargetFunc { get; private set; } [Export] public string TargetFunc { get; private set; }
[Export] public string TargetName { get; private set; } [Export] public string TargetName { get; set; }
[Export] public int MinActivations { get; private set; } = 0; [Export] public int MinActivations { get; private set; } = 0;
[Export] public int MaxActivations { get; private set; } = 1; [Export] public int MaxActivations { get; private set; } = 1;
[Export] public ActivationType ActivationType { get; private set; } = ActivationType.Toggle; [Export] public ActivationType ActivationType { get; set; } = ActivationType.Toggle;
public enum TriggerStates public enum TriggerStates
{ {
@ -31,28 +32,33 @@ public partial class TriggerArea : Area3D
private int _activations = 0; private int _activations = 0;
public void _func_godot_apply_properties(Dictionary<string, string> props) public void _func_godot_apply_properties(Dictionary<string, Variant> props)
{ {
Target = props["target"];
TargetFunc = props["targetfunc"]; ((ITargetable)this).ApplyTargetProperties(props);
TargetName = props["targetname"]; ((ITargeting)this).ApplyTargetingProperties(props);
if (props.TryGetValue("activationtype", out var type)) //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);
if (props.TryGetValue("minactivations", out var minActivations))
{ {
var t = Enum.TryParse(type, true, out ActivationType activationType); MinActivations = minActivations.AsInt32();
if (t)
{
ActivationType = activationType;
}
} }
if (int.TryParse(props["minactivations"], out var minActivations)) if (props.TryGetValue("maxactivations", out var maxActivations))
{ {
MinActivations = minActivations; MaxActivations = maxActivations.AsInt32();
}
if (int.TryParse(props["maxactivations"], out var maxActivations))
{
MaxActivations = maxActivations;
} }
} }

View file

@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Cirno._3D.TrenchBroom.EntityScripts.Triggers;
using Cirno.Scripts.Components.FSM; using Cirno.Scripts.Components.FSM;
using Cirno.Scripts.Components.FSM._3DPlayer; using Cirno.Scripts.Components.FSM._3DPlayer;
using Godot; using Godot;
@ -11,7 +12,7 @@ using GTweensGodot.Extensions;
namespace Cirno.Scripts.Activables._3D; namespace Cirno.Scripts.Activables._3D;
[Tool] [Tool]
public partial class Teleporter3D : StaticBody3D, IActivable public partial class Teleporter3D : StaticBody3D, IActivable, ITargetable
{ {
[Export] public bool IsEnabled { get; set; } [Export] public bool IsEnabled { get; set; }
@ -34,12 +35,12 @@ public partial class Teleporter3D : StaticBody3D, IActivable
[Export] public StringName DefaultAnimationName { get; private set; } = "Default"; [Export] public StringName DefaultAnimationName { get; private set; } = "Default";
public void _func_godot_apply_properties(Dictionary<string, string> props) public void _func_godot_apply_properties(Dictionary<string, Variant> props)
{ {
TargetGroup = props["target"]; TargetGroup = props["target"].AsString();
TargetName = props["targetname"]; TargetName = props["targetname"].AsString();
IsEnabled = bool.Parse(props["enabled"]); IsEnabled = props["enabled"].AsBool();
} }
private void PlayAnimation(StringName name) private void PlayAnimation(StringName name)