Inventory management of granted abilities
This commit is contained in:
@@ -1,55 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Gamesmiths.Forge.Godot.Resources.Abilities;
|
||||
using Godot;
|
||||
using Movementtests.systems;
|
||||
|
||||
[Tool, GlobalClass]
|
||||
public partial class AbilitySelection : Control
|
||||
{
|
||||
[Signal] public delegate void AbilityAddedEventHandler(WeaponSystem.WeaponEvent forEvent, string abilityName);
|
||||
[Signal] public delegate void AbilityAddedEventHandler(WeaponSystem.WeaponEvent forEvent, ForgeAbilityBehavior behavior);
|
||||
[Signal] public delegate void AbilityRemovedEventHandler(WeaponSystem.WeaponEvent forEvent, ForgeAbilityBehavior behavior);
|
||||
|
||||
[Export] public WeaponSystem.WeaponEvent ForEvent { get; set; } = WeaponSystem.WeaponEvent.StartedFlying;
|
||||
|
||||
private string _title = string.Empty;
|
||||
[Export] public string Title {
|
||||
get => _title;
|
||||
set
|
||||
{
|
||||
_title = value;
|
||||
TitleChanged();
|
||||
}
|
||||
}
|
||||
[Export] public string Title { get; set; } = string.Empty;
|
||||
|
||||
[Export] public PackedScene AbilitySelectionItem { get; set; }
|
||||
[Export] public PackedScene AbilitySelectedItem { get; set; }
|
||||
|
||||
[Export] public ForgeAbilityBehavior[] AbilityBehaviors { get; set; }
|
||||
|
||||
private VBoxContainer _abilities;
|
||||
private VBoxContainer _selectedAbilities;
|
||||
private MenuButton _addAbility;
|
||||
private PopupMenu _addAbilityMenu;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_abilities = GetNode<VBoxContainer>("%SelectedAbilities");
|
||||
var titleLabel = GetNode<Label>("%TitleLabel");
|
||||
titleLabel.Text = Title;
|
||||
|
||||
_selectedAbilities = GetNode<VBoxContainer>("%SelectedAbilities");
|
||||
_addAbility = GetNode<MenuButton>("%AddAbility");
|
||||
_addAbilityMenu = _addAbility.GetPopup();
|
||||
|
||||
_addAbilityMenu.IdPressed += AddAbilityMenuOnIdPressed;
|
||||
if (Engine.IsEditorHint()) return;
|
||||
|
||||
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<Resource> equippedAbilities)
|
||||
{
|
||||
foreach (var equippedAbility in equippedAbilities)
|
||||
{
|
||||
if (equippedAbility is not ForgeAbilityBehavior ability) continue;
|
||||
AddSelectedAbility(ability);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddAbilityMenuOnIndexPressed(long index)
|
||||
{
|
||||
var indexInt = Convert.ToInt32(index);
|
||||
var metadata = _addAbilityMenu.GetItemMetadata(indexInt);
|
||||
var name = _addAbilityMenu.GetItemText(indexInt);
|
||||
EmitSignalAbilityAdded(ForEvent, name);
|
||||
var metadata = (ForgeAbilityBehavior) _addAbilityMenu.GetItemMetadata(indexInt);
|
||||
EmitSignalAbilityAdded(ForEvent, metadata);
|
||||
}
|
||||
|
||||
private void AddAbilityMenuOnIdPressed(long id)
|
||||
public void AddSelectedAbility(ForgeAbilityBehavior behavior)
|
||||
{
|
||||
var newSelectedAbilityItem = AbilitySelectedItem.Instantiate() as SelectedAbility;
|
||||
if (newSelectedAbilityItem == null) return;
|
||||
|
||||
newSelectedAbilityItem.SetAbility(behavior);
|
||||
newSelectedAbilityItem.AbilityRemoved += ability => EmitSignalAbilityRemoved(ForEvent, ability);
|
||||
_selectedAbilities.AddChild(newSelectedAbilityItem);
|
||||
}
|
||||
|
||||
public void TitleChanged()
|
||||
|
||||
public void RemoveSelectedAbility(ForgeAbilityBehavior behavior)
|
||||
{
|
||||
var titleLabel = GetNode<Label>("%TitleLabel");
|
||||
titleLabel.Text = Title;
|
||||
foreach (var child in _selectedAbilities.GetChildren())
|
||||
{
|
||||
if (child is not SelectedAbility selectedAbility || selectedAbility.Ability != behavior) continue;
|
||||
_selectedAbilities.RemoveChild(selectedAbility);
|
||||
_addAbility.GrabFocus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user