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

@@ -0,0 +1,9 @@
[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://cegp8r6cj3jqj"]
[ext_resource type="Shader" path="res://embedded/Miscs/SkinDyeShader.gdshader" id="1_31kjt"]
[resource]
shader = ExtResource("1_31kjt")
shader_parameter/old_palette = null
shader_parameter/new_palette = null
shader_parameter/glowing = null

View File

@@ -0,0 +1,50 @@
shader_type canvas_item;
uniform vec4 old_palette[32];
uniform vec4 new_palette[32];
uniform int glowing[32];
const float pe = 0.004;
struct DyeResult{
vec4 color;
int index;
};
DyeResult dye(vec4 color){
for(int i = 0; i<32; i++){
if(distance(color, old_palette[i]) < pe){
return DyeResult(new_palette[i], i);
}
}
return DyeResult(color, -1);
}
vec4 rgb2hsv(vec4 inp){
vec3 c = inp.rgb;
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec4(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x, inp.a);
}
vec4 hsv2rgb(vec4 inp){
vec3 c = inp.xyz;
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return vec4( c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y), inp.a);
}
void fragment() {
DyeResult dy = dye(texture(TEXTURE, UV));
if(dy.index != -1 && glowing[dy.index] == 1){
vec4 hsv = rgb2hsv(dy.color);
hsv.z += sin(TIME*1.5)*0.1;
COLOR = hsv2rgb(hsv);
}
else{
COLOR = dy.color;
}
}

View File

@@ -8,6 +8,7 @@ using Polonium.Attributes;
using Polonium; using Polonium;
using Polonium.Interfaces; using Polonium.Interfaces;
using System.Collections.Generic; using System.Collections.Generic;
using Polonium.Agents;
// ReSharper disable once CheckNamespace // ReSharper disable once CheckNamespace
public static partial class GlobalRegistry public static partial class GlobalRegistry
@@ -28,21 +29,18 @@ public static partial class GlobalRegistry
public static class Asset<T> where T : Node public static class Asset<T> where T : Node
{ {
private static readonly Queue<T> Pool = new();
// ReSharper disable once StaticMemberInGenericType // ReSharper disable once StaticMemberInGenericType
// ReSharper disable once UnusedAutoPropertyAccessor.Global // ReSharper disable once UnusedAutoPropertyAccessor.Global
public static PackedScene Scene { get; set; } public static PackedScene Scene => PoloniumRegistry.Asset<T>.Scene;
private static T Instance => Scene.Instantiate<T>(); public static T Get() => PoloniumRegistry.Asset<T>.Get();
public static T Get() => Pool.Count > 0 ? Pool.Dequeue() : Instance; public static void Return(T obj) => PoloniumRegistry.Asset<T>.Return(obj);
public static void Return(T obj)
{
if(Pool.Count < 10)
Pool.Enqueue(obj);
else
obj.QueueFree();
}
} }
public static partial class Action<TAction> where TAction : AgentAction
{
public static string Name => PoloniumRegistry.Action<TAction>.Name;
}
public static PoloniumRegistry PoloniumRegistry => PoloniumRegistry.Instance; public static PoloniumRegistry PoloniumRegistry => PoloniumRegistry.Instance;
public static bool Paused { get; set; } public static bool Paused { get; set; }
public static void Prepare() => PoloniumRegistry.Prepare(); public static void Prepare() => PoloniumRegistry.Prepare();

View File

@@ -0,0 +1,151 @@
using System.Reflection;
using Godot;
using Polonium;
using Polonium.Attributes;
using Polonium.SkinManagers.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Skin = Polonium.SkinManagers.Skin;
[AutoRegister]
// ReSharper disable once CheckNamespace
public static class SkinPaletteRegister
{
public static void Register()
{
Type gr = typeof(PoloniumRegistry.SkinRegistry<>);
Assembly assembly = Assembly.GetExecutingAssembly();
IEnumerable<Type> skinTypes = assembly
.GetTypes()
.Where(t => t.IsSubclassOf(typeof(Skin)) && !t.IsAbstract)
.Where(t => t.GetCustomAttributes(typeof(SkinInfo), true).Any());
foreach (Type skinType in skinTypes)
{
Type ist = gr.MakeGenericType(skinType);
string pth = skinType.GetCustomAttribute<SkinInfo>()!.Path;
int k = skinType.GetCustomAttribute<SkinInfo>()!.Roots;
PropertyInfo paletteRoots = ist.GetProperty("PaletteRoots");
PropertyInfo paletteMap = ist.GetProperty("PaletteMap");
Color[] roots;
Dictionary<Color, Color[]> map;
if (IsAutoPalette(skinType))
(roots, map) = KMean(ExtractColors(pth), k);
else
(roots, map) = GetPalette(pth);
paletteRoots!.SetValue(null, roots);
paletteMap!.SetValue(null, map);
}
}
private static bool IsAutoPalette(Type type)
{
Type x = type;
while (x != typeof(object) && x != null)
{
if (x.IsGenericType && x.GetGenericTypeDefinition() == typeof(AutoPaletteSkin<>))
return true;
x = x.BaseType;
}
return false;
}
private static HashSet<Color> ExtractColors(string path)
{
HashSet<Color> colors = new();
CompressedTexture2D texture = ResourceLoader.Load<CompressedTexture2D>(path);
Image image = texture.GetImage();
for (int x = 0; x < image.GetWidth(); x++)
for (int y = 0; y < image.GetHeight(); y++)
{
Color px = image.GetPixel(x, y);
if (px.A8 > 0)
colors.Add(px);
}
return colors;
}
private static (Color[], Dictionary<Color, Color[]>) GetPalette(string path)
{
//HashSet<Color> colors = ExtractColors(path);
HashSet<Color> colors = new();
Dictionary<Color, HashSet<Color>> map = new();
CompressedTexture2D texture = ResourceLoader.Load<CompressedTexture2D>(path);
Image image = texture.GetImage();
int height = image.GetHeight();
int width = image.GetWidth();
for (int y = height - 32; y < height; y++)
{
Color px0 = image.GetPixel(width-32, y);
if (px0.A8 == 0)
break;
colors.Add(px0);
map[px0] = new HashSet<Color> { px0 };
for (int x = width - 32; x < width; x++)
{
Color px = image.GetPixel(x, y);
if (px.A8 == 0)
break;
map[px0].Add(px);
}
}
return (colors.ToArray(), map.ToDictionary(p => p.Key, p => p.Value.ToArray()));
}
private static (Color[], Dictionary<Color, Color[]>) KMean(HashSet<Color> colors, int k)
{
var colorPoints = colors.ToList();
var random = new Random();
Color[] centroids = colorPoints.OrderBy(x => random.Next()).Take(k).ToArray();
bool hasConverged = false;
Dictionary<Color, List<Color>> clusters = new();
while (!hasConverged)
{
clusters = centroids.ToDictionary(c => c, c => new List<Color>());
foreach (Color color in colorPoints)
{
Color nearestCentroid = centroids.OrderBy(c => c.DistanceTo(color)).First();
clusters[nearestCentroid].Add(color);
}
hasConverged = true;
for (int i = 0; i < centroids.Length; i++)
{
if (clusters[centroids[i]].Count == 0) continue;
Color newCentroid = clusters[centroids[i]].AverageColor();
if (!newCentroid.Equals(centroids[i]))
{
centroids[i] = newCentroid;
hasConverged = false;
}
}
}
Dictionary<Color, Color[]> resultMap = clusters.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value.ToArray()
);
return (centroids, resultMap);
}
private static Vector4 ToVector(this Color a) => new(a.R, a.G, a.B, a.A);
private static double DistanceTo(this Color a, Color b) => a.ToVector().DistanceTo(b.ToVector());
private static Color ToColor(this Vector4 a) => new Color(a.X, a.Y, a.Z, a.W);
private static Color AverageColor(this IEnumerable<Color> a)
{
Vector4[] aa = a.Select(x => x.ToVector()).ToArray();
Vector4 res = Vector4.Zero;
foreach (Vector4 c in aa)
res += c;
return (res / aa.Length).ToColor();
}
}

View File

@@ -5,9 +5,9 @@ namespace Polonium.Agents;
public abstract partial class ActionSet : Node 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"); Knowledge = GetNode<Knowledge>("Knowledge");
ActionSet = GetNode<ActionSet>("ActionSet"); ActionSet = GetNode<ActionSet>("ActionSet");
ActionSet.ActionSubmitted += ExecuteAction;
} }
public sealed override void _Ready() public sealed override void _Ready()
@@ -34,6 +33,14 @@ public abstract partial class Agent : Node
header.SetSender(this); header.SetSender(this);
EmitSignalActionExecuted(header, actionBody); 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 Godot;
using Polonium.Resources;
namespace Polonium.Agents; namespace Polonium.Agents;
public abstract partial class Knowledge : Node 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 public abstract partial class World : Node
{ {
private WorldModel Model { get; set; } protected WorldModel Model { get; set; }
public HashSet<Agent> Agents { get; } = new(); public HashSet<Agent> Agents { get; } = new();
public Knowledge CommonKnowledge { get; set; } public Knowledge CommonKnowledge { get; set; }

View File

@@ -11,7 +11,7 @@ public abstract partial class WorldModel : Node
[Signal] [Signal]
public delegate void PrivateKnowledgeUpdatedEventHandler(SignalHeader header, KnowledgePatch update); public delegate void PrivateKnowledgeUpdatedEventHandler(SignalHeader header, KnowledgePatch update);
private Knowledge WorldKnowledge { get; set; } protected Knowledge WorldKnowledge { get; set; }
public override void _Ready() 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 // ReSharper disable once CollectionNeverQueried.Global
[RegistryPassThrough(true)] [RegistryPassThrough(true)]
public HashSet<ITimeConsumer> TimeConsumers { get; } = new(); 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 partial class SignalHeader : Resource
{ {
public abstract Agent[] ResolveReceivers(); public abstract IEnumerable<Agent> ResolveReceivers();
public abstract Agent ResolveSender(); public abstract Agent ResolveSender();
public abstract void SetSender(Agent agent); 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");
}
}