94 lines
3.2 KiB
C#
94 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Chickensoft.AutoInject;
|
|
using Chickensoft.Introspection;
|
|
using Gamesmiths.Forge.Godot.Resources.Abilities;
|
|
using Godot;
|
|
using Movementtests.systems;
|
|
|
|
[Tool, GlobalClass, Meta(typeof(IAutoConnect), typeof(IAutoOn))]
|
|
public partial class AbilitySelection : Control
|
|
{
|
|
public override void _Notification(int what) => this.Notify(what);
|
|
|
|
#region Signals
|
|
|
|
[Signal] public delegate void AbilityAddedEventHandler(WeaponSystem.WeaponEvent forEvent, ForgeAbilityBehavior behavior);
|
|
[Signal] public delegate void AbilityRemovedEventHandler(WeaponSystem.WeaponEvent forEvent, ForgeAbilityBehavior behavior);
|
|
|
|
#endregion
|
|
|
|
#region Exports
|
|
|
|
[Export] public WeaponSystem.WeaponEvent ForEvent { get; set; } = WeaponSystem.WeaponEvent.StartedFlying;
|
|
[Export] public string Title { get; set; } = string.Empty;
|
|
[Export] public PackedScene? AbilitySelectedItem { get; set; }
|
|
[Export] public ForgeAbilityBehavior[] AbilityBehaviors { get; set; } = [];
|
|
|
|
#endregion
|
|
|
|
#region Nodes
|
|
|
|
[Node("%SelectedAbilities")] public required VBoxContainer SelectedAbilities { get; set; }
|
|
[Node("%AddAbility")] public required MenuButton AddAbility { get; set; }
|
|
public required PopupMenu AddAbilityMenu { get; set; }
|
|
|
|
#endregion
|
|
|
|
public void OnReady()
|
|
{
|
|
// Initialize title (also in Editor as Tool)
|
|
var titleLabel = GetNode<Label>("%TitleLabel");
|
|
titleLabel.Text = Title;
|
|
|
|
AddAbilityMenu = AddAbility.GetPopup();
|
|
|
|
if (Engine.IsEditorHint()) return; // Editor flow stops here
|
|
|
|
var i = 0;
|
|
foreach (var behavior in AbilityBehaviors)
|
|
{
|
|
AddAbilityMenu.AddIconItem(behavior.Icon, behavior.Name);
|
|
AddAbilityMenu.SetItemMetadata(i, behavior);
|
|
i += 1;
|
|
}
|
|
|
|
AddAbilityMenu.IndexPressed += AddAbilityMenuOnIndexPressed;
|
|
}
|
|
|
|
public void Initialize(IEnumerable<ForgeAbilityBehavior> equippedAbilities)
|
|
{
|
|
foreach (var equippedAbility in equippedAbilities)
|
|
{
|
|
AddSelectedAbility(equippedAbility);
|
|
}
|
|
}
|
|
|
|
private void AddAbilityMenuOnIndexPressed(long index)
|
|
{
|
|
var indexInt = Convert.ToInt32(index);
|
|
var metadata = (ForgeAbilityBehavior) AddAbilityMenu.GetItemMetadata(indexInt);
|
|
EmitSignalAbilityAdded(ForEvent, metadata);
|
|
}
|
|
|
|
public void AddSelectedAbility(ForgeAbilityBehavior behavior)
|
|
{
|
|
if (AbilitySelectedItem?.Instantiate() is not SelectedAbility newSelectedAbilityItem) return;
|
|
|
|
newSelectedAbilityItem.SetAbility(behavior);
|
|
newSelectedAbilityItem.AbilityRemoved += ability => EmitSignalAbilityRemoved(ForEvent, ability);
|
|
SelectedAbilities.AddChild(newSelectedAbilityItem);
|
|
}
|
|
|
|
public void RemoveSelectedAbility(ForgeAbilityBehavior behavior)
|
|
{
|
|
foreach (var child in SelectedAbilities.GetChildren())
|
|
{
|
|
if (child is not SelectedAbility selectedAbility || selectedAbility.Ability != behavior) continue;
|
|
SelectedAbilities.RemoveChild(selectedAbility);
|
|
AddAbility.GrabFocus();
|
|
return;
|
|
}
|
|
}
|
|
}
|