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

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