Files
MovementTests/menus/scenes/overlaid_menus/InventoryUi.cs

133 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using Chickensoft.Sync.Primitives;
using Gamesmiths.Forge.Effects;
using Gamesmiths.Forge.Godot.Resources.Abilities;
using Godot;
using Movementtests.managers;
using Movementtests.systems;
[Tool, GlobalClass, Icon("res://assets/ui/IconGodotNode/control/icon_crate.png"), Meta(typeof(IAutoNode))]
public partial class InventoryUi : Control, IDisposable
{
public override void _Notification(int what) => this.Notify(what);
public record struct EventElements(
AbilitySelection AbilitySelection,
AutoSet<ForgeAbilityBehavior>.Binding Binding,
IAutoSet<ForgeAbilityBehavior> Inventory);
#region Dependencies
[Dependency]
public InventoryManager InventoryManager => this.DependOn<InventoryManager>();
#endregion Dependencies
#region Nodes
[Node]
public required AbilitySelection StartedFlying { get; set; }
[Node]
public required AbilitySelection WhileFlying { get; set; }
[Node]
public required AbilitySelection StoppedFlying { get; set; }
[Node]
public required AbilitySelection StartedPlanted { get; set; }
[Node]
public required AbilitySelection WhilePlanted { get; set; }
[Node]
public required AbilitySelection StoppedPlanted { get; set; }
#endregion Nodes
public required AutoSet<ForgeAbilityBehavior>.Binding StartedFlyingBinding { get; set; }
public required AutoSet<ForgeAbilityBehavior>.Binding FlyingTickBinding { get; set; }
public required AutoSet<ForgeAbilityBehavior>.Binding StoppedFlyingBinding { get; set; }
public required AutoSet<ForgeAbilityBehavior>.Binding StartedPlantedBinding { get; set; }
public required AutoSet<ForgeAbilityBehavior>.Binding PlantedTickBinding { get; set; }
public required AutoSet<ForgeAbilityBehavior>.Binding StoppedPlantedBinding { get; set; }
public Dictionary<WeaponSystem.WeaponEvent, EventElements> EventElementsMap { get; private set; } = [];
public void OnReady()
{
}
public void OnResolved()
{
EventElementsMap = new Dictionary<WeaponSystem.WeaponEvent, EventElements>
{
{ WeaponSystem.WeaponEvent.StartedFlying, new EventElements(StartedFlying, StartedFlyingBinding, InventoryManager.StartedFlyingAbilities) },
{ WeaponSystem.WeaponEvent.StoppedFlying, new EventElements(StoppedFlying, StoppedFlyingBinding, InventoryManager.StoppedFlyingAbilities)},
{ WeaponSystem.WeaponEvent.FlyingTick, new EventElements(WhileFlying, FlyingTickBinding, InventoryManager.FlyingTickAbilities) },
{ WeaponSystem.WeaponEvent.StartedPlanted, new EventElements(StartedPlanted, StartedPlantedBinding, InventoryManager.StartedPlantedAbilities) },
{ WeaponSystem.WeaponEvent.StoppedPlanted, new EventElements(StoppedPlanted, StoppedPlantedBinding, InventoryManager.StoppedPlantedAbilities) },
{ WeaponSystem.WeaponEvent.PlantedTick, new EventElements(WhilePlanted, PlantedTickBinding, InventoryManager.PlantedTickAbilities) },
};
foreach (var (forEvent, element) in EventElementsMap)
{
element.AbilitySelection.AbilityAdded += AddAbilityForEvent;
element.AbilitySelection.AbilityRemoved += RemoveAbilityForEvent;
var elem = EventElementsMap[forEvent];
elem.AbilitySelection.Initialize(InventoryManager.WeaponEventsInventory[forEvent]);
elem.Binding = element.Inventory.Bind();
elem.Binding
.OnAdd(behavior => OnWeaponEventInventoryAdded(forEvent, behavior))
.OnRemove(behavior => OnWeaponEventInventoryRemoved(forEvent, behavior))
.OnClear(() => OnWeaponEventInventoryCleared(forEvent));
}
foreach (var forEvent in EventElementsMap.Keys)
{
}
}
public new void Dispose()
{
GC.SuppressFinalize(this);
foreach (var element in EventElementsMap.Values)
element.Binding.Dispose();
base.Dispose();
}
public void AddAbilityForEvent(WeaponSystem.WeaponEvent forEvent, ForgeAbilityBehavior abilityBehavior)
{
InventoryManager.AddAbilityForWeaponEvent(forEvent, abilityBehavior);
}
public void RemoveAbilityForEvent(WeaponSystem.WeaponEvent forEvent, ForgeAbilityBehavior abilityBehavior)
{
InventoryManager.RemoveAbilityForWeaponEvent(forEvent, abilityBehavior);
}
public void OnWeaponEventInventoryAdded(WeaponSystem.WeaponEvent forEvent, ForgeAbilityBehavior abilityBehavior)
{
var selection = GetAbilitySelection(forEvent);
selection.AddSelectedAbility(abilityBehavior);
}
public void OnWeaponEventInventoryRemoved(WeaponSystem.WeaponEvent forEvent, ForgeAbilityBehavior abilityBehavior)
{
var selection = GetAbilitySelection(forEvent);
selection.RemoveSelectedAbility(abilityBehavior);
}
public void OnWeaponEventInventoryCleared(WeaponSystem.WeaponEvent forEvent)
{
var selection = GetAbilitySelection(forEvent);
selection.ClearAbilities();
}
public AbilitySelection GetAbilitySelection(WeaponSystem.WeaponEvent forEvent)
{
return EventElementsMap[forEvent].AbilitySelection;
}
}