SupaLidlGame/Entities/DynamicDoor.cs

107 lines
2.7 KiB
C#
Raw Normal View History

2023-08-05 23:50:08 -07:00
using Godot;
using SupaLidlGame.Extensions;
namespace SupaLidlGame.Entities;
public partial class DynamicDoor : StaticBody2D
{
[Export]
public string MapStateKey { get; set; }
[Export]
2023-12-09 16:26:44 -08:00
public Godot.Collections.Array<NodePath> VisibleOnToggle { get; set; }
[Export]
public Godot.Collections.Array<NavigationRegion2D> Rebake { get; set; }
2023-08-05 23:50:08 -07:00
[Export]
public bool DefaultState { get; set; }
private AnimationPlayer _animPlayer;
public override void _Ready()
{
_animPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
var globalState = this.GetGlobalState();
globalState.MapState.MapStateBoolChanged += OnMapStateChanged;
2023-08-07 02:38:51 -07:00
SetAnimations(false);
2023-08-05 23:50:08 -07:00
RefreshMapState((bool)globalState.MapState[MapStateKey]);
}
public virtual void OnMapStateChanged(string key, bool value)
{
GD.Print("Map state changed");
if (key == MapStateKey)
{
2023-08-07 02:38:51 -07:00
SetAnimations(true);
2023-08-05 23:50:08 -07:00
RefreshMapState(value);
}
}
2023-12-09 16:26:44 -08:00
private async void RefreshMapState(bool value)
2023-08-05 23:50:08 -07:00
{
if (value)
{
Open();
}
else
{
Close();
}
2023-12-09 16:26:44 -08:00
await ToSignal(_animPlayer,
AnimationPlayer.SignalName.AnimationFinished);
if (Rebake is not null)
{
foreach (var navmesh in Rebake)
{
// rebake navmesh so NPCs can correctly travel conditionally
GD.Print("Dynamic door updated; rebaking navmeshes...");
if (!navmesh.IsBaking())
{
navmesh.BakeNavigationPolygon();
}
2023-12-09 16:26:44 -08:00
}
}
2023-08-05 23:50:08 -07:00
}
public virtual void Open()
{
_animPlayer?.TryPlay("open");
2023-08-07 02:38:51 -07:00
//this.GetWorld().CurrentPlayer.Camera.Shake(1, 0.5f);
2023-08-05 23:50:08 -07:00
}
public virtual void Close()
{
2023-08-07 02:38:51 -07:00
_animPlayer?.TryPlay("close");
//this.GetWorld().CurrentPlayer.Camera.Shake(2, 0.25f);
}
public void SetAnimations(bool isEnabled)
{
2023-12-09 16:26:44 -08:00
if (VisibleOnToggle is null)
{
GD.PushWarning($"DynamicDoor {GetPath()} has no VisibleOnToggle " +
"array. Set this to empty array to disable this warning.");
return;
}
2023-08-07 02:38:51 -07:00
foreach (var animKey in _animPlayer.GetAnimationList())
{
var anim = _animPlayer.GetAnimation(animKey);
for (int i = 0; i < anim.GetTrackCount(); i++)
{
foreach (var nodePath in VisibleOnToggle)
{
if (anim.TrackGetPath(i) == nodePath)
{
anim.TrackSetEnabled(i, isEnabled);
}
}
}
}
2023-08-05 23:50:08 -07:00
}
}