add: Item Manager

This commit is contained in:
h z
2025-02-28 08:49:32 +00:00
parent e90f701bd5
commit d2f0ab8153
21 changed files with 648 additions and 24 deletions

View File

@@ -5,9 +5,9 @@ namespace Polonium.Agents;
public abstract partial class ActionSet : Node
{
[Signal]
public delegate void ActionSubmittedEventHandler(ActionBody action);
public HashSet<AgentAction.Template> Templates { get; set; } = new();
public virtual void Learn(AgentAction.Template template) => Templates.Add(template);
}

View File

@@ -15,7 +15,6 @@ public abstract partial class Agent : Node
{
Knowledge = GetNode<Knowledge>("Knowledge");
ActionSet = GetNode<ActionSet>("ActionSet");
ActionSet.ActionSubmitted += ExecuteAction;
}
public sealed override void _Ready()
@@ -34,6 +33,14 @@ public abstract partial class Agent : Node
header.SetSender(this);
EmitSignalActionExecuted(header, actionBody);
}
public void Act<TAction>(AgentAction.Parameter parameter)
where TAction : AgentAction
{
AgentAction.Template template = ActionSet.Templates
.FirstOrDefault(t => t.ActionName == PoloniumRegistry.Action<TAction>.Name);
}
}

60
src/Agents/AgentAction.cs Normal file
View File

@@ -0,0 +1,60 @@
using Godot;
namespace Polonium.Agents;
public abstract partial class AgentAction : Node
{
public abstract class Parameter
{
}
public virtual void SetParameter(Parameter p)
{
}
public abstract void Execute();
public abstract void Finish();
public abstract void Reset();
public abstract class Template : PoloniumTemplate<AgentAction>
{
public abstract string ActionName { get; }
public abstract Template Copy { get; }
}
public class Template<TAction> : Template
where TAction : AgentAction
{
public override string ActionName => PoloniumRegistry.Action<TAction>.Name;
public override TAction Get
{
get
{
TAction res = PoloniumRegistry.Asset<TAction>.Get();
res.Reset();
Modify(res);
return res;
}
}
public override void Modify(AgentAction obj)
{
if (obj is not TAction act)
return;
Modify(act);
}
public void Return(TAction res) => PoloniumRegistry.Asset<TAction>.Return(res);
public override Template Copy => new Template<TAction> { };
public virtual void Modify(TAction action)
{
}
}
}

View File

@@ -1,8 +1,12 @@
using Godot;
using Polonium.Resources;
namespace Polonium.Agents;
public abstract partial class Knowledge : Node
{
}
[Signal]
public delegate void KnowledgeUpdatedEventHandler(KnowledgePatch update);
public abstract void UpdateKnowledge(KnowledgePatch update);
}

View File

@@ -5,7 +5,7 @@ namespace Polonium.Agents;
public abstract partial class World : Node
{
private WorldModel Model { get; set; }
protected WorldModel Model { get; set; }
public HashSet<Agent> Agents { get; } = new();
public Knowledge CommonKnowledge { get; set; }

View File

@@ -11,7 +11,7 @@ public abstract partial class WorldModel : Node
[Signal]
public delegate void PrivateKnowledgeUpdatedEventHandler(SignalHeader header, KnowledgePatch update);
private Knowledge WorldKnowledge { get; set; }
protected Knowledge WorldKnowledge { get; set; }
public override void _Ready()
{

View File

@@ -0,0 +1,8 @@
namespace Polonium.Attributes;
[AttributeUsage(AttributeTargets.Class)]
public class SkinInfo(string path, int roots = 1) : Attribute
{
public string Path { get; set; } = path;
public int Roots { get; set; } = roots;
}

View File

@@ -0,0 +1,33 @@
using Godot;
using Polonium.SkinManagers.Generic;
namespace Polonium.ItemManagers.Generic;
public abstract partial class SkinItem<TItem, TSkin> : SkinItem
where TItem : SkinItem<TItem, TSkin>
where TSkin : Skin<TSkin>
{
public new TSkin Skin
{
get => base.Skin as TSkin;
set => base.Skin = value;
}
public new abstract class Template : SkinItem.Template
{
public Dictionary<int, (Color, bool)> DyeInfo { get; set; } = new();
public override SkinItem Get => GenericGet<TItem, TSkin>();
public virtual void Return(TItem item) => GenericReturn<TItem, TSkin>(item);
public override void Modify(SkinItem obj)
{
foreach (int key in DyeInfo.Keys)
{
(Color c, bool g) = DyeInfo[key];
obj.Skin.Dye(key, c, g);
}
}
}
}

36
src/ItemManagers/Item.cs Normal file
View File

@@ -0,0 +1,36 @@
using Godot;
namespace Polonium.ItemManagers;
public abstract partial class Item : Node
{
public Texture2D ItemIcon { get; set; }
public abstract class Template : PoloniumTemplate<Item>
{
}
public class Template<TItem> : Template
where TItem : Item
{
public override Item Get
{
get
{
TItem res = PoloniumRegistry.Asset<TItem>.Get();
Modify(res);
return res;
}
}
public override void Modify(Item obj)
{
if (obj is not TItem item)
return;
Modify(item);
}
public virtual void Modify(TItem res)
{
}
}
}

View File

@@ -0,0 +1,53 @@
using Polonium.SkinManagers;
namespace Polonium.ItemManagers;
public abstract partial class SkinItem : Item
{
public Skin Skin { get; set; }
public virtual void Reset()
{
}
public new abstract class Template : PoloniumTemplate<SkinItem>
{
protected SkinItem Instance { get; set; }
protected virtual TItem GenericGet<TItem, TSkin>()
where TItem : SkinItem
where TSkin : Skin
{
TItem res = PoloniumRegistry.Asset<TItem>.Get();
res.Reset();
res.Skin = GetSkin<TSkin>();
Modify(res);
return res;
}
protected virtual void GenericReturn<TItem, TSkin>(TItem item)
where TItem : SkinItem
where TSkin : Skin
{
ReturnSkin(item.Skin as TSkin);
item.Skin = null;
PoloniumRegistry.Asset<TItem>.Return(item);
}
protected virtual TSkin GetSkin<TSkin>()
where TSkin : Skin
{
TSkin res = PoloniumRegistry.Asset<TSkin>.Get();
res.Init();
return res;
}
protected virtual void ReturnSkin<TSkin>(TSkin skin)
where TSkin : Skin
{
PoloniumRegistry.Asset<TSkin>.Return(skin);
}
}
}

View File

@@ -32,6 +32,39 @@ public class PoloniumRegistry
// ReSharper disable once CollectionNeverQueried.Global
[RegistryPassThrough(true)]
public HashSet<ITimeConsumer> TimeConsumers { get; } = new();
public static class Action<TAction>
where TAction : AgentAction
{
// ReSharper disable once StaticMemberInGenericType
public static string Name { get; set; }
}
public static class SkinRegistry<TSkin>
where TSkin : Polonium.SkinManagers.Skin
{
// ReSharper disable once StaticMemberInGenericType
public static Color[] PaletteRoots { get; set; }
// ReSharper disable once StaticMemberInGenericType
public static Dictionary<Color, Color[]> PaletteMap { get; set; }
}
public static class Asset<TAsset>
where TAsset : Node
{
private static readonly Queue<TAsset> Pool = new();
public static PackedScene Scene { get; set; }
private static TAsset Instance => Scene.Instantiate<TAsset>();
public static TAsset Get() => Pool.Count > 0 ? Pool.Dequeue() : Instance;
public static void Return(TAsset asset)
{
if (Pool.Count < 10)
Pool.Enqueue(asset);
else
asset.QueueFree();
}
}
}

7
src/PoloniumTemplate.cs Normal file
View File

@@ -0,0 +1,7 @@
namespace Polonium;
public abstract class PoloniumTemplate<TObj>
{
public abstract TObj Get { get; }
public abstract void Modify(TObj obj);
}

View File

@@ -6,7 +6,7 @@ namespace Polonium.Resources;
public abstract partial class SignalHeader : Resource
{
public abstract Agent[] ResolveReceivers();
public abstract IEnumerable<Agent> ResolveReceivers();
public abstract Agent ResolveSender();
public abstract void SetSender(Agent agent);

View File

@@ -0,0 +1,8 @@
namespace Polonium.SkinManagers.Generic;
public abstract partial class AutoPaletteSkin<TSkin> : Skin<TSkin>
where TSkin : AutoPaletteSkin<TSkin>
{
protected override int Columns => 1;
protected override int Rows => 1;
}

View File

@@ -0,0 +1,12 @@
using Godot;
namespace Polonium.SkinManagers.Generic;
public abstract partial class Skin<TSkin> : Skin
where TSkin : Skin<TSkin>
{
protected override int RootCount => PaletteRoots.Length;
protected override Color[] PaletteRoots => PoloniumRegistry.SkinRegistry<TSkin>.PaletteRoots;
protected override Dictionary<Color, Color[]> PaletteMap => PoloniumRegistry.SkinRegistry<TSkin>.PaletteMap;
}

112
src/SkinManagers/Skin.cs Normal file
View File

@@ -0,0 +1,112 @@
using Godot;
namespace Polonium.SkinManagers;
public abstract partial class Skin : Sprite2D
{
protected abstract int Columns { get; }
protected abstract int Rows { get; }
protected virtual string ShaderMaterialPath => "res://embedded/Miscs/SkinDyeMaterial.tres";
protected ShaderMaterial ShaderMaterial
{
get => Material as ShaderMaterial;
set => Material = value;
}
protected abstract Color[] PaletteRoots { get; }
protected abstract Dictionary<Color, Color[]> PaletteMap { get; }
protected Color[] DyeMap { get; set; }
protected int[] GlowingMap { get; set; }
protected abstract int RootCount { get; }
private struct DyeInfo
{
public Color[] ColorFrom;
public Color[] ColorTo;
public int[] Glowing;
}
private DyeInfo FullDyePair
{
get
{
List<Color> colorFrom = new List<Color>();
List<Color> colorTo = new List<Color>();
List<int> glowing = new List<int>();
for (int i = 0; i < PaletteRoots.Length; i++)
{
Color rootSource = PaletteRoots[i];
Color rootTarget = DyeMap[i];
int isGlowing = GlowingMap[i];
foreach (Color c in PaletteMap[rootSource])
{
colorFrom.Add(c);
float h = c.H + rootTarget.H - rootSource.H;
if (h > 0)
h -= 1;
if (h < 0)
h += 1;
float s = rootSource.S > 0
? Math.Max(0, Math.Min(1, c.S * (rootTarget.S / rootSource.S)))
: Math.Max(0, Math.Min(1, rootTarget.S + (c.S - rootSource.S)));
float v = rootSource.V > 0
? Math.Max(0, Math.Min(1, c.V * (rootTarget.V / rootSource.V)))
: Math.Max(0, Math.Min(1, rootTarget.V + (c.V - rootSource.V)));
colorTo.Add(Color.FromHsv(h, s, v, rootTarget.A));
glowing.Add(isGlowing);
}
}
while (colorFrom.Count < 32)
{
colorFrom.Add(Color.Color8(1, 2, 3, 4));
colorTo.Add(Color.Color8(1, 2, 3, 4));
glowing.Add(0);
}
return new DyeInfo(){ ColorFrom = colorFrom.ToArray(), ColorTo = colorTo.ToArray(), Glowing = glowing.ToArray() };
}
}
public virtual void Init()
{
Hframes = Columns;
Vframes = Rows;
if (DyeMap is null || DyeMap.Length != PaletteRoots.Length)
{
DyeMap = new Color[PaletteRoots.Length];
GlowingMap = new int[PaletteRoots.Length];
for (int i = 0; i < PaletteRoots.Length; i++)
{
DyeMap[i] = PaletteRoots[i];
GlowingMap[i] = 0;
}
}
ShaderMaterial = ResourceLoader
.Load<ShaderMaterial>(ShaderMaterialPath)
.Duplicate() as ShaderMaterial;
Rerender();
}
private void Rerender()
{
DyeInfo info = FullDyePair;
ShaderMaterial.SetShaderParameter("old_palette", info.ColorFrom);
ShaderMaterial.SetShaderParameter("new_palette", info.ColorTo);
ShaderMaterial.SetShaderParameter("glowing", info.Glowing);
}
public void Dye(int rootIdx, Color target, bool glowing = false)
{
DyeMap[rootIdx] = target;
GlowingMap[rootIdx] = glowing ? 1 : 0;
Rerender();
}
}

View File

@@ -0,0 +1,43 @@
using Godot;
namespace Polonium.SkinManagers;
public abstract partial class SkinPacker : Node2D
{
public Node2D SkinCollector { get; set; }
protected Skin[] CollectedSkins => SkinCollector
.GetChildren()
.OfType<Skin>()
.ToArray();
private Vector2I BackingFrameCoords { get; set; }
[Export]
protected Vector2I FrameCoords
{
get => BackingFrameCoords;
set
{
BackingFrameCoords = value;
UpdateSkins();
}
}
private void UpdateSkins()
{
if (SkinCollector is not null)
foreach(Skin skin in CollectedSkins)
skin.FrameCoords = FrameCoords;
}
public AnimationPlayer SyncPlayer { get; set; }
public override void _Ready()
{
base._Ready();
SyncPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
SkinCollector = GetNode<Node2D>("SkinCollector");
}
}