Compare commits
15 Commits
draft_text
...
f7d1c5a66b
| Author | SHA1 | Date | |
|---|---|---|---|
| f7d1c5a66b | |||
| 1456f5dd7d | |||
| 93fbe0869b | |||
| 684763f0cc | |||
| d2f0ab8153 | |||
| e90f701bd5 | |||
| 8df4ebb6d5 | |||
| 92770d3425 | |||
| 50ca3f75ec | |||
| 0e00bacfeb | |||
| ee4e489219 | |||
| d720b03480 | |||
| 2c32af6b42 | |||
| b21023ba24 | |||
| 2d0b25f65c |
6
.gitignore
vendored
6
.gitignore
vendored
@@ -3,3 +3,9 @@ obj/
|
||||
/packages/
|
||||
riderModule.iml
|
||||
/_ReSharper.Caches/
|
||||
Package/embedded/polonium_templates/
|
||||
Package/embedded/Patches/RegistryPassThrough.p.cs
|
||||
VersionInfo.props
|
||||
Polonium.sln.DotSettings.user
|
||||
.godot
|
||||
.idea
|
||||
13
.idea/.idea.Polonium/.idea/.gitignore
generated
vendored
13
.idea/.idea.Polonium/.idea/.gitignore
generated
vendored
@@ -1,13 +0,0 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Rider ignored files
|
||||
/.idea.Hangman.SDK.iml
|
||||
/modules.xml
|
||||
/projectSettingsUpdater.xml
|
||||
/contentModel.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
4
.idea/.idea.Polonium/.idea/encodings.xml
generated
4
.idea/.idea.Polonium/.idea/encodings.xml
generated
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
|
||||
</project>
|
||||
8
.idea/.idea.Polonium/.idea/indexLayout.xml
generated
8
.idea/.idea.Polonium/.idea/indexLayout.xml
generated
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="UserContentModel">
|
||||
<attachedFolders />
|
||||
<explicitIncludes />
|
||||
<explicitExcludes />
|
||||
</component>
|
||||
</project>
|
||||
7
.idea/.idea.Polonium/.idea/vcs.xml
generated
7
.idea/.idea.Polonium/.idea/vcs.xml
generated
@@ -1,7 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/../Polonium.Generators" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -9,6 +9,7 @@
|
||||
<Compile Remove="script_templates/**/*.*"/>
|
||||
<Compile Remove="embedded/polonium_templates/**/*.cs"/>
|
||||
<AdditionalFiles Include="Assets/**/*.tscn"/>
|
||||
<AdditionalFiles Include="Data/**/*" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PoloniumPrepare" BeforeTargets="PoloniumClean">
|
||||
@@ -44,6 +45,14 @@
|
||||
SourceFiles="@(EmbeddedFiles)"
|
||||
DestinationFolder="$(ProjectDir)embedded/%(RecursiveDir)"
|
||||
/>
|
||||
<Copy
|
||||
SourceFiles="$(MSBuildThisFileDirectory)editorconfig"
|
||||
DestinationFiles="$(ProjectDir).editorconfig"
|
||||
/>
|
||||
<Copy
|
||||
SourceFiles="$(MSBuildThisFileDirectory)gitignore"
|
||||
DestinationFiles="$(ProjectDir)embedded/.gitignore"
|
||||
/>
|
||||
<ItemGroup>
|
||||
<PoloniumTemplateFiles Include="$(MSBuildThisFileDirectory)embedded/polonium_templates/**/*.*" />
|
||||
</ItemGroup>
|
||||
|
||||
2
Package/build/editorconfig
Normal file
2
Package/build/editorconfig
Normal file
@@ -0,0 +1,2 @@
|
||||
[embedded/*]
|
||||
generated_code = true
|
||||
1
Package/build/gitignore
Normal file
1
Package/build/gitignore
Normal file
@@ -0,0 +1 @@
|
||||
*
|
||||
@@ -17,6 +17,17 @@ public partial class PoloniumTextureButton : TextureButton
|
||||
|
||||
private GlobalRegistry.TextureSetName PrivateTextureSetName { get; set; }
|
||||
|
||||
public bool ButtonDisabled
|
||||
{
|
||||
get => Disabled;
|
||||
set
|
||||
{
|
||||
Disabled = value;
|
||||
FocusMode = Disabled ? FocusMode = Control.FocusModeEnum.None : FocusMode = Control.FocusModeEnum.All;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Export]
|
||||
public GlobalRegistry.TextureSetName TextureSetName
|
||||
{
|
||||
@@ -24,7 +35,7 @@ public partial class PoloniumTextureButton : TextureButton
|
||||
set
|
||||
{
|
||||
PrivateTextureSetName = value;
|
||||
t = GlobalRegistry.TextureSetMap.GetValueOrDefault(value, null);
|
||||
TextureSet t = GlobalRegistry.TextureSetMap.GetValueOrDefault(value, null);
|
||||
if (t is not null)
|
||||
{
|
||||
TextureNormal = t.Normal;
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
|
||||
using Godot;
|
||||
using Polonium.Attributes;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace GlobalClasses;
|
||||
[ProxyNode]
|
||||
[GlobalClass]
|
||||
[Tool]
|
||||
public partial class MouseControlledCamera : Camera2D
|
||||
{
|
||||
private Vector2 DragStartPosition { get; set; }
|
||||
private bool IsDragging { get; set; }
|
||||
[Signal]
|
||||
public delegate void ZoomInEventHandler();
|
||||
[Signal]
|
||||
public delegate void ZoomOutEventHandler();
|
||||
|
||||
[Export]
|
||||
public MouseButton DragButton { get; set; } = MouseButton.Middle;
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (@event is InputEventMouseButton mouseButtonEvent)
|
||||
{
|
||||
if(mouseButtonEvent.ButtonIndex == MouseButton.WheelUp)
|
||||
EmitSignalZoomIn();
|
||||
else if(mouseButtonEvent.ButtonIndex == MouseButton.WheelDown)
|
||||
EmitSignalZoomOut();
|
||||
else if (mouseButtonEvent.ButtonIndex == DragButton)
|
||||
{
|
||||
IsDragging = mouseButtonEvent.Pressed;
|
||||
if(IsDragging)
|
||||
DragStartPosition = GetGlobalMousePosition();
|
||||
}
|
||||
}
|
||||
else if (@event is InputEventMouseMotion mouseMotionEvent && IsDragging)
|
||||
{
|
||||
Vector2 mousePos = GetGlobalMousePosition();
|
||||
Vector2 offset = DragStartPosition - mousePos;
|
||||
Position += offset;
|
||||
DragStartPosition = mousePos;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,12 +7,12 @@ namespace GlobalClasses;
|
||||
[Tool]
|
||||
public partial class CameraScene : Scene
|
||||
{
|
||||
private Camera2D Camera { get; set; }
|
||||
|
||||
protected Camera2D Camera { get; set; }
|
||||
[Export]
|
||||
public float MaxZoom { get; set; }
|
||||
|
||||
[Export]
|
||||
public float MinZoom { get; set; }
|
||||
|
||||
[Export]
|
||||
public float ZoomRate { get; set; }
|
||||
|
||||
private float Zoom
|
||||
@@ -26,13 +26,18 @@ public partial class CameraScene : Scene
|
||||
{
|
||||
}
|
||||
|
||||
public sealed override void _Ready()
|
||||
protected virtual void _Ready_()
|
||||
{
|
||||
Camera = GetNode<Camera2D>("Camera");
|
||||
}
|
||||
|
||||
public sealed override void _Ready()
|
||||
{
|
||||
_Ready_();
|
||||
__Ready();
|
||||
base._Ready();
|
||||
}
|
||||
protected void ZoomIn() => Zoom = Mathf.Max(Zoom * (1 + ZoomRate), MaxZoom);
|
||||
protected void ZoomOut() => Zoom = Mathf.Min(Zoom * (1 - ZoomRate), MinZoom);
|
||||
protected void ZoomIn() => Zoom = Mathf.Min(Zoom * (1 + ZoomRate), MaxZoom);
|
||||
protected void ZoomOut() => Zoom = Mathf.Max(Zoom * (1 - ZoomRate), MinZoom);
|
||||
protected void ZoomAt(Vector2 pos) => Camera.Position = pos;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
using Godot;
|
||||
using Polonium.Attributes;
|
||||
// ReSharper disable once CheckNamespace
|
||||
|
||||
namespace GlobalClasses;
|
||||
[ProxyNode]
|
||||
[GlobalClass]
|
||||
[Tool]
|
||||
public partial class MouseControlledCameraScene : CameraScene
|
||||
{
|
||||
protected new MouseControlledCamera Camera
|
||||
{
|
||||
get => base.Camera as MouseControlledCamera;
|
||||
set => base.Camera = value;
|
||||
}
|
||||
|
||||
protected sealed override void _Ready_()
|
||||
{
|
||||
Camera = GetNode<MouseControlledCamera>("Camera");
|
||||
Camera.ZoomIn += ZoomIn;
|
||||
Camera.ZoomOut += ZoomOut;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Godot;
|
||||
using Polonium.Attributes;
|
||||
|
||||
namespace GlobalClasses;
|
||||
[ProxyNode]
|
||||
[GlobalClass]
|
||||
[Tool]
|
||||
public partial class SelectableTileMapLayer : TileMapLayer
|
||||
{
|
||||
[Signal]
|
||||
public delegate void CellSelectedEventHandler(Vector2I pos);
|
||||
|
||||
[Signal]
|
||||
public delegate void CellEnteredEventHandler(Vector2I pos);
|
||||
private Vector2I HoveredCell { get; set; } = new Vector2I(-1, -1);
|
||||
|
||||
public sealed override void _Process(double delta)
|
||||
{
|
||||
|
||||
Vector2 mousePos = GetGlobalMousePosition();
|
||||
Vector2 localMousePos = ToLocal(mousePos);
|
||||
Vector2I cell = LocalToMap(localMousePos);
|
||||
if(cell != HoveredCell)
|
||||
{
|
||||
EmitSignalCellEntered(cell);
|
||||
HoveredCell = cell;
|
||||
}
|
||||
__Process(delta);
|
||||
base._Process(delta);
|
||||
}
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (@event is InputEventMouseButton mouseEvent)
|
||||
if (mouseEvent.ButtonIndex == MouseButton.Left && mouseEvent.Pressed)
|
||||
EmitSignalCellSelected(HoveredCell);
|
||||
}
|
||||
[ProxyMethod]
|
||||
public virtual void __Process(double delta)
|
||||
{
|
||||
}
|
||||
}
|
||||
9
Package/embedded/Miscs/SkinDyeMaterial.tres
Normal file
9
Package/embedded/Miscs/SkinDyeMaterial.tres
Normal 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
|
||||
50
Package/embedded/Miscs/SkinDyeShader.gdshader
Normal file
50
Package/embedded/Miscs/SkinDyeShader.gdshader
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,24 +29,20 @@ 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 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 void Return(T obj)
|
||||
public static partial class Action<TAction> where TAction : AgentAction
|
||||
{
|
||||
if(Pool.Count < 10)
|
||||
Pool.Enqueue(obj);
|
||||
else
|
||||
obj.QueueFree();
|
||||
}
|
||||
public static string Name => PoloniumRegistry.Action<TAction>.Name;
|
||||
}
|
||||
|
||||
public static PoloniumRegistry PoloniumRegistry => PoloniumRegistry.Instance;
|
||||
public static bool Paused { get; set; }
|
||||
public static HashSet<ITimeConsumer> TimeConsumers { get; } = [];
|
||||
public static void Prepare() => PoloniumRegistry.Prepare();
|
||||
|
||||
// ReSharper disable once PartialTypeWithSinglePart
|
||||
|
||||
151
Package/embedded/Patches/SkinPaletteRegister.p.cs
Normal file
151
Package/embedded/Patches/SkinPaletteRegister.p.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
<Project Sdk="Godot.NET.Sdk/4.4.0-beta.3">
|
||||
|
||||
<Project Sdk="Godot.NET.Sdk/4.4.0">
|
||||
<Import Project="VersionInfo.props" Condition="Exists('VersionInfo.props')" />
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
@@ -8,12 +8,14 @@
|
||||
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
|
||||
<PackageId>Polonium</PackageId>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Version>0.1.1-x</Version>
|
||||
<Version>$(PoloniumVersion)</Version>
|
||||
<Authors>Hangman</Authors>
|
||||
<DisableImplicitRestore>true</DisableImplicitRestore>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Polonium.Tasks" Version="0.1.1-x" />
|
||||
<PackageReference Include="Polonium.Tasks" Version="$(PoloniumTasksVersion)" />
|
||||
<None Include="summerizer" />
|
||||
<None Include="VersionInfo.props" Pack="true" PackagePath="build" Condition="Exists('VersionInfo.props')" />
|
||||
</ItemGroup>
|
||||
<Target Name="Prepare" BeforeTargets="BeforeBuild">
|
||||
<RemoveDir Directories="$(ProjectDir)Package/embedded/polonium_templates" Condition="Exists('$(ProjectDir)Package/polonium_templates')"/>
|
||||
@@ -23,18 +25,26 @@
|
||||
TemplateDirectory="$(ProjectDir)Package/embedded/polonium_templates"
|
||||
AttributeName="ProxyNode"
|
||||
/>
|
||||
<Delete
|
||||
Files="$(ProjectDir)Package/embedded/RegistryPassThrough.p.cs"
|
||||
Condition="Exists('$(ProjectDir)Package/embedded/RegistryPassThrough.p.cs')"
|
||||
/>
|
||||
<GenerateRegistryPassThrough ProjectDir="$(ProjectDir)" />
|
||||
</Target>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="NuGet.config" />
|
||||
<None Include="Package/build/$(AssemblyName).targets" Pack="true" PackagePath="build" />
|
||||
<None Include="Package/build/$(AssemblyName).props" Pack="true" PackagePath="build" />
|
||||
<None Include="Package/build/**/*" Pack="true" PackagePath="build" />
|
||||
<None Include="Package/embedded/**/*.*" Pack="true" PackagePath="build/embedded" />
|
||||
<None Include="publish" />
|
||||
<None Include="build" />
|
||||
<None Include="README.md" />
|
||||
<Compile Remove="Package/**/*.*" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="Package/build/editorconfig" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="CleanPreviousPackages" BeforeTargets="Build">
|
||||
<Message Text="Cleaning Previous Packages -------------------------" Importance="high" />
|
||||
<Message Text="Cleaning --------------------------------------------" Importance="high" />
|
||||
|
||||
70
README.md
Normal file
70
README.md
Normal file
@@ -0,0 +1,70 @@
|
||||
# Polonium
|
||||
|
||||
**Project Description**
|
||||
Polonium is a C# library distilled from numerous personal Godot development experiences. By generalizing common patterns across projects, Polonium aims to streamline game development workflows and reduce repetitive boilerplate code. From agent-based AI to automated texture configuration, it offers a cohesive set of features that integrate smoothly with Godot’s C# environment.
|
||||
|
||||
---
|
||||
|
||||
## Key Features
|
||||
|
||||
1. **Agent-Based AI Framework**
|
||||
Polonium provides an `Agent / Action / World` system that supports building complex AI behaviors in a structured manner. Agents can execute actions defined in `ActionSet`s, and the `World` manages overarching states and knowledge.
|
||||
|
||||
2. **Global Registry**
|
||||
Centralizes data such as configs, saves, and references to nodes or scenes. `GlobalRegistry` provides an entry point for everything from your game’s assets to the paused state.
|
||||
|
||||
3. **Message Bus & Patch System**
|
||||
- **Message Bus**: Enables publish-subscribe communication among different parts of your game. This decouples your components and improves maintainability.
|
||||
- **PatchableItem**: Allows sending patches (e.g., `FramePatch`) to `Knowledge` nodes, updating game state without direct references. This pattern is extensible and can be applied to various synchronous or asynchronous data updates.
|
||||
|
||||
4. **PoloniumTextureButton** & Automatic Textures
|
||||
- **PoloniumTextureButton**: A specialized `TextureButton` that automatically loads **Normal**, **Hover**, **Pressed**, **Disabled**, and **Focused** textures from a predefined directory structure.
|
||||
- **AnimatedTexture Builder**: Automatically converts folders ending in `.at_dir` under `Resources` into `AnimatedTexture` resources, speeding up the creation of animated UI elements or sprites.
|
||||
|
||||
5. **Asset Management & Resource Pool**
|
||||
- **Instance by Class**: You can instantiate scenes purely by type (`GlobalRegistry.Asset<TAsset>.Get()`), bypassing manual path references. Scenes and scripts must align under `Assets` for this to work.
|
||||
- **Reusable Pool**: The same system offers an object pool for frequently used scenes, improving performance by reducing repeated instantiation.
|
||||
|
||||
6. **Skins & Recoloring**
|
||||
Provides an easy-to-use skin system with dynamic color palettes (roots and maps), plus the ability to `Dye` or glow specific palette entries in real-time.
|
||||
|
||||
7. **Attribute-Driven Code Generation**
|
||||
- **[AutoRegister]**, **[ProxyNode]**, **[RegistryPassThrough]**: Primarily used in Polonium’s embedded code. They automate tasks like bridging registry properties, generating proxy classes, etc.
|
||||
- **[AutoPatch] + [PatchableProperty]**: If an interface is marked with `[AutoPatch]` and contains at most one property marked `[PatchableProperty]` (with getter and setter), Polonium.Generators will automatically create a matching `Patch` class for updating classes that implement this interface.
|
||||
- **[RegistryEntity]**: Automatically generates a matching property in `GlobalRegistry` for the annotated class, providing easy, game-wide access.
|
||||
- **[SkinInfo]**: When applied to a `Skin` subclass, Polonium can automatically extract a color palette from a PNG file, generating code to handle recoloring logic.
|
||||
|
||||
---
|
||||
|
||||
## Installation
|
||||
1. Clone or download this repository.
|
||||
2. Reference `Polonium.dll` in your Godot C# or other .NET project.
|
||||
3. Run `dotnet restore` to resolve dependencies.
|
||||
|
||||
---
|
||||
|
||||
## Quick Start Example
|
||||
```csharp
|
||||
public partial class Root : Node
|
||||
{
|
||||
public override void _Ready()
|
||||
{
|
||||
GlobalRegistry.Prepare();
|
||||
GlobalRegistry.Start();
|
||||
}
|
||||
}
|
||||
```
|
||||
This initializes Polonium’s registry and automatically registers classes marked with `[AutoRegister]`. Alternatively, inherit from `RootScene` and set it as your main scene in the Godot editor.
|
||||
|
||||
---
|
||||
|
||||
## Usage Notes
|
||||
- **Attributes for Polonium-Embedded Code**: `[ProxyNode]` and `[RegistryPassThrough]` are primarily used in Polonium’s own code generation for its embedded templates; end users typically don’t apply these in their own scripts.
|
||||
- **AutoPatch & PatchableProperty**: The generator can create specialized Patch classes that help automatically update the data in classes implementing `[AutoPatch]` interfaces.
|
||||
- **RegistryEntity**: Declaring a class with `[RegistryEntity]` ensures a globally accessible property is created in `GlobalRegistry`.
|
||||
|
||||
---
|
||||
|
||||
## License
|
||||
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).
|
||||
|
||||
17
build
17
build
@@ -1,17 +0,0 @@
|
||||
#! /bin/bash
|
||||
rm -rf ~/.nuget/packages/polonium*
|
||||
cd ../Polonium.Tasks
|
||||
dotnet clean Polonium.Tasks.csproj
|
||||
dotnet restore Polonium.Tasks.csproj
|
||||
dotnet build Polonium.Tasks.csproj
|
||||
|
||||
cd ../Polonium.Generators
|
||||
dotnet clean Polonium.Generators.csproj
|
||||
dotnet restore Polonium.Generators.csproj
|
||||
dotnet build Polonium.Generators.csproj
|
||||
|
||||
cd ../Polonium
|
||||
rm -rf ~/.nuget/packages/polonium*
|
||||
dotnet clean Polonium.csproj
|
||||
dotnet restore Polonium.csproj
|
||||
dotnet build Polonium.csproj
|
||||
13
publish
Normal file → Executable file
13
publish
Normal file → Executable file
@@ -1,2 +1,13 @@
|
||||
#!/bin/bash
|
||||
dotnet nuget push "$(ls -t ./.godot/mono/temp/bin/Debug/Polonium.*.nupkg | head -n 1)" --source hangman-lab
|
||||
|
||||
SCRIPT_DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")/
|
||||
LATEST_PACKAGE=$(ls -t "${SCRIPT_DIR}".godot/mono/temp/bin/Debug/Polonium.*.nupkg 2>/dev/null | head -n 1)
|
||||
|
||||
if [[ -z "$LATEST_PACKAGE" ]]; then
|
||||
echo "❌ Error: No .nupkg file found in ${SCRIPT_DIR}/bin/Debug/"
|
||||
exit 1
|
||||
fi
|
||||
echo "🚀 Pushing NuGet package: $LATEST_PACKAGE"
|
||||
dotnet nuget push "$LATEST_PACKAGE" --source hangman-lab --skip-duplicate
|
||||
|
||||
|
||||
|
||||
13
src/Agents/ActionSet.cs
Normal file
13
src/Agents/ActionSet.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Godot;
|
||||
using Polonium.Resources;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract partial class ActionSet : Node
|
||||
{
|
||||
|
||||
public HashSet<AgentAction.Template> Templates { get; set; } = new();
|
||||
public virtual void Learn(AgentAction.Template template) => Templates.Add(template);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,51 +1,38 @@
|
||||
using Godot;
|
||||
using Polonium.Resources;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
|
||||
public abstract class Agent
|
||||
public abstract partial class Agent : Node
|
||||
{
|
||||
protected static int ComputerCount = 0;
|
||||
public Knowledge Knowledge { get; set; }
|
||||
public ActionSet ActionSet { get; set; }
|
||||
|
||||
public string Name { get; init; }
|
||||
|
||||
public Agent(string name)
|
||||
public virtual void __Ready()
|
||||
{
|
||||
Name = name;
|
||||
PoloniumRegistry.Instance.Agents[name] = this;
|
||||
}
|
||||
|
||||
public int Level { get; private set; }
|
||||
public abstract int MaxLevel { get; }
|
||||
|
||||
public virtual void LevelUp()
|
||||
{
|
||||
if(Level < MaxLevel)
|
||||
Level++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public abstract class Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction> : Agent
|
||||
where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentAction : AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
public sealed override void _Ready()
|
||||
{
|
||||
|
||||
public Dictionary<string, TAgentAction> Actions { get; } = new();
|
||||
|
||||
public TAgentDecisionMaker DecisionMaker { get; init; }
|
||||
public TAgentKnowledge Knowledge { get; init; }
|
||||
|
||||
|
||||
public Agent() : base($"Computer {ComputerCount++}")
|
||||
{
|
||||
DecisionMaker = new TAgentDecisionMaker();
|
||||
Knowledge = new TAgentKnowledge();
|
||||
Knowledge = GetNode<Knowledge>("Knowledge");
|
||||
ActionSet = GetNode<ActionSet>("ActionSet");
|
||||
__Ready();
|
||||
base._Ready();
|
||||
}
|
||||
|
||||
public Agent(string name, TAgentDecisionMaker decisionMaker, TAgentKnowledge knowledge) : base(name)
|
||||
public void Act<TAction>(AgentAction.Parameter parameter)
|
||||
where TAction : AgentAction
|
||||
{
|
||||
DecisionMaker = decisionMaker;
|
||||
Knowledge = knowledge;
|
||||
AgentAction.Template template = ActionSet.Templates
|
||||
.FirstOrDefault(t => t.ActionName == PoloniumRegistry.Action<TAction>.Name);
|
||||
if(template is null)
|
||||
return;
|
||||
AgentAction act = template.Get;
|
||||
act.SetParameter(parameter);
|
||||
act.Execute();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,60 +1,60 @@
|
||||
using Polonium.Interfaces;
|
||||
using Godot;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract class AgentAction
|
||||
public abstract partial class AgentAction : Node
|
||||
{
|
||||
public abstract class Parameter
|
||||
{
|
||||
}
|
||||
|
||||
public abstract class AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction> : AgentAction, ITimeConsumer
|
||||
where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentAction : AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
{
|
||||
|
||||
public int Level { get; private set; }
|
||||
|
||||
public abstract int MaxLevel { get; }
|
||||
|
||||
public virtual void LevelUp()
|
||||
{
|
||||
if(Level < MaxLevel)
|
||||
Level++;
|
||||
}
|
||||
|
||||
public AgentAction(double coolDown)
|
||||
{
|
||||
CoolDown = coolDown;
|
||||
PoloniumRegistry.Instance.TimeConsumers.Add(this);
|
||||
}
|
||||
|
||||
public abstract class ActionParameter
|
||||
public virtual void SetParameter(Parameter p)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual bool Execute(ActionParameter parameter)
|
||||
public abstract void Execute();
|
||||
|
||||
public abstract void Finish();
|
||||
|
||||
public abstract void Reset();
|
||||
|
||||
public abstract class Template : PoloniumTemplate<AgentAction>
|
||||
{
|
||||
if (Disabled || CoolDown > 0)
|
||||
return false;
|
||||
CoolDownTime = CoolDown;
|
||||
return true;
|
||||
public abstract string ActionName { get; }
|
||||
public abstract Template Copy { get; }
|
||||
}
|
||||
|
||||
|
||||
private double CoolDownTime { get; set; }
|
||||
public bool Disabled { get; set; }
|
||||
|
||||
private double CoolDown { get; }
|
||||
|
||||
public void Process(double delta)
|
||||
public class Template<TAction> : Template
|
||||
where TAction : AgentAction
|
||||
{
|
||||
public override string ActionName => PoloniumRegistry.Action<TAction>.Name;
|
||||
|
||||
if (Disabled || CoolDownTime <= 0)
|
||||
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;
|
||||
CoolDownTime -= delta;
|
||||
if (CoolDownTime <= 0)
|
||||
CoolDownTime = 0;
|
||||
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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract class AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>(DecisionMakerType type)
|
||||
where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentAction : AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
{
|
||||
|
||||
|
||||
|
||||
public DecisionMakerType Type { get; init; } = type;
|
||||
|
||||
public AgentDecisionMaker() : this(DecisionMakerType.Computer)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract class AgentKnowledge
|
||||
{
|
||||
}
|
||||
|
||||
public abstract class AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction> : AgentKnowledge
|
||||
where TAgent : Agent<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentDecisionMaker : AgentDecisionMaker<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentKnowledge : AgentKnowledge<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
where TAgentAction : AgentAction<TAgent, TAgentDecisionMaker, TAgentKnowledge, TAgentAction>, new()
|
||||
{
|
||||
public AgentKnowledge()
|
||||
{
|
||||
}
|
||||
|
||||
public abstract void Update(TAgentKnowledge newKnowledge);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public enum DecisionMakerType
|
||||
{
|
||||
None = 0,
|
||||
Player = 1,
|
||||
Computer = 2,
|
||||
Global = 3,
|
||||
}
|
||||
22
src/Agents/Knowledge.cs
Normal file
22
src/Agents/Knowledge.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Godot;
|
||||
using Polonium.Interfaces;
|
||||
using Polonium.MessageManager;
|
||||
using Polonium.Resources.FramePatches;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract partial class Knowledge : Node, IMessageClient
|
||||
{
|
||||
public abstract void UpdateKnowledge(FramePatch update);
|
||||
|
||||
public string PostCode { get; set; }
|
||||
public virtual void ReceiveMessage(PoloniumMessage msg)
|
||||
{
|
||||
if(msg is FramePatch update)
|
||||
UpdateKnowledge(update);
|
||||
}
|
||||
|
||||
public event IMessageClient.MessageSentEventHandler MessageSent;
|
||||
public void SendMessage(PoloniumMessage msg) => MessageSent?.Invoke(msg);
|
||||
}
|
||||
|
||||
21
src/Agents/KnowledgeRender.cs
Normal file
21
src/Agents/KnowledgeRender.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Godot;
|
||||
using Polonium.Interfaces;
|
||||
using Polonium.MessageManager;
|
||||
using Polonium.Resources.FramePatches;
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract partial class KnowledgeRender : Node, IMessageClient
|
||||
{
|
||||
public abstract void RenderKnowledgePatch(FramePatch update);
|
||||
public string PostCode { get; set; }
|
||||
public virtual void ReceiveMessage(PoloniumMessage msg)
|
||||
{
|
||||
if(msg is FramePatch p)
|
||||
RenderKnowledgePatch(p);
|
||||
}
|
||||
|
||||
public event IMessageClient.MessageSentEventHandler MessageSent;
|
||||
public void SendMessage(PoloniumMessage msg) => MessageSent?.Invoke(msg);
|
||||
}
|
||||
|
||||
|
||||
37
src/Agents/World.cs
Normal file
37
src/Agents/World.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Polonium.MessageManager;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract partial class World : MessageBus
|
||||
{
|
||||
protected WorldModel Model { get; set; }
|
||||
public Knowledge CommonKnowledge { get; set; }
|
||||
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Model = GetNode<WorldModel>("WorldModel");
|
||||
Model.WorldKnowledge.PostCode = "WorldKnowledge";
|
||||
Register(Model.WorldKnowledge);
|
||||
CommonKnowledge = GetNode<Knowledge>("CommonKnowledge");
|
||||
CommonKnowledge.PostCode = "CommonKnowledge";
|
||||
Register(CommonKnowledge);
|
||||
base._Ready();
|
||||
}
|
||||
|
||||
public void Register(Agent agent)
|
||||
{
|
||||
Register(agent.Knowledge);
|
||||
}
|
||||
|
||||
public virtual void Enter()
|
||||
{
|
||||
PoloniumRegistry.Instance.CurrentWorld = this;
|
||||
}
|
||||
|
||||
public virtual void Exit()
|
||||
{
|
||||
PoloniumRegistry.Instance.CurrentWorld = null;
|
||||
}
|
||||
|
||||
}
|
||||
17
src/Agents/WorldModel.cs
Normal file
17
src/Agents/WorldModel.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Godot;
|
||||
using Polonium.Resources;
|
||||
|
||||
namespace Polonium.Agents;
|
||||
|
||||
public abstract partial class WorldModel : Node
|
||||
{
|
||||
public Knowledge WorldKnowledge { get; set; }
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
WorldKnowledge = GetNode<Knowledge>("WorldKnowledge");
|
||||
base._Ready();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
6
src/Attributes/AutoPatch.cs
Normal file
6
src/Attributes/AutoPatch.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Polonium.Attributes;
|
||||
[AttributeUsage(AttributeTargets.Interface)]
|
||||
public class AutoPatch : Attribute
|
||||
{
|
||||
|
||||
}
|
||||
6
src/Attributes/PatchableProperty.cs
Normal file
6
src/Attributes/PatchableProperty.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Polonium.Attributes;
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class PatchableProperty : Attribute
|
||||
{
|
||||
|
||||
}
|
||||
6
src/Attributes/RegistryPassThrough.cs
Normal file
6
src/Attributes/RegistryPassThrough.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Polonium.Attributes;
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class RegistryPassThrough(bool getterOnly = false) : Attribute
|
||||
{
|
||||
public bool GetterOnly { get; set; } = getterOnly;
|
||||
}
|
||||
8
src/Attributes/SkinInfo.cs
Normal file
8
src/Attributes/SkinInfo.cs
Normal 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;
|
||||
|
||||
}
|
||||
9
src/Attributes/TemplateDefines/TemplateBlock.cs
Normal file
9
src/Attributes/TemplateDefines/TemplateBlock.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Polonium.Attributes.TemplateDefines;
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
|
||||
public class TemplateBlock(string signature, string template, string dataFile, string deps="") : Attribute
|
||||
{
|
||||
public string Signature { get; set; } = signature;
|
||||
public string Template { get; set; } = template;
|
||||
public string DataFile { get; set; } = dataFile;
|
||||
public string Deps { get; set; } = deps;
|
||||
}
|
||||
9
src/Attributes/TemplateDefines/TemplateInline.cs
Normal file
9
src/Attributes/TemplateDefines/TemplateInline.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Polonium.Attributes.TemplateDefines;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
|
||||
public class TemplateInline(string template, string dataFile, string deps="") : Attribute
|
||||
{
|
||||
public string Template { get; set; } = template;
|
||||
public string DataFile { get; set; } = dataFile;
|
||||
public string Deps { get; set; } = deps;
|
||||
}
|
||||
42
src/DataStructures/EventDictionary.cs
Normal file
42
src/DataStructures/EventDictionary.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
namespace Polonium.DataStructures;
|
||||
|
||||
public class EventDictionary<TKey, TValue> : Dictionary<TKey, TValue>
|
||||
{
|
||||
public new TValue this[TKey a]
|
||||
{
|
||||
get => base[a];
|
||||
set
|
||||
{
|
||||
base[a] = value;
|
||||
Edited?.Invoke(a, value);
|
||||
Updated?.Invoke(a);
|
||||
}
|
||||
}
|
||||
|
||||
public new void Remove(TKey key)
|
||||
{
|
||||
base.Remove(key);
|
||||
Removed?.Invoke(key);
|
||||
Updated?.Invoke(key);
|
||||
}
|
||||
|
||||
public new void Clear()
|
||||
{
|
||||
base.Clear();
|
||||
Cleared?.Invoke();
|
||||
}
|
||||
|
||||
|
||||
public delegate void EditedEventHandler(TKey key, TValue value);
|
||||
public event EditedEventHandler Edited;
|
||||
|
||||
public delegate void RemovedEventHandler(TKey key);
|
||||
public event RemovedEventHandler Removed;
|
||||
|
||||
public delegate void ClearedEventHandler();
|
||||
public event ClearedEventHandler Cleared;
|
||||
|
||||
public delegate void UpdatedEventHandler(TKey key);
|
||||
public event UpdatedEventHandler Updated;
|
||||
|
||||
}
|
||||
31
src/DataStructures/PatchableItems/PatchableDictionary.cs
Normal file
31
src/DataStructures/PatchableItems/PatchableDictionary.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Polonium.DataStructures.PatchableItems;
|
||||
|
||||
public class PatchableDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
|
||||
{
|
||||
public Dictionary<TKey, TValue> Data { get; set; } = new();
|
||||
public int Count => Data.Count;
|
||||
public void Add(TKey key, TValue value) => Data.Add(key, value);
|
||||
public void Remove(TKey key) => Data.Remove(key);
|
||||
public void Clear() => Data.Clear();
|
||||
public bool ContainsKey(TKey key) => Data.ContainsKey(key);
|
||||
public bool TryGetValue(TKey key, out TValue value) => Data.TryGetValue(key, out value);
|
||||
public bool ContainsValue(TValue value) => Data.ContainsValue(value);
|
||||
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get => Data[key];
|
||||
set => Data[key] = value;
|
||||
}
|
||||
public IEnumerable<TKey> Keys => Data.Keys;
|
||||
public IEnumerable<TValue> Values => Data.Values;
|
||||
|
||||
public UpdateMethods UpdateMethod { get; set; } = UpdateMethods.Update;
|
||||
public delegate void CustomUpdatedEventHandler(Dictionary<TKey, TValue> set);
|
||||
public event CustomUpdatedEventHandler CustomUpdated;
|
||||
public void CustomUpdate(Dictionary<TKey, TValue> set) => CustomUpdated?.Invoke(set);
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => Data.GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
23
src/DataStructures/PatchableItems/PatchableHashSet.cs
Normal file
23
src/DataStructures/PatchableItems/PatchableHashSet.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Polonium.DataStructures.PatchableItems;
|
||||
|
||||
public class PatchableHashSet<T> : IEnumerable<T>
|
||||
{
|
||||
public HashSet<T> Data { get; set; } = new();
|
||||
public void Add(T item) => Data.Add(item);
|
||||
public void Remove(T item) => Data.Remove(item);
|
||||
public void Clear() => Data.Clear();
|
||||
public bool Contains(T item) => Data.Contains(item);
|
||||
public int Count => Data.Count;
|
||||
|
||||
|
||||
public UpdateMethods UpdateMethod { get; set; } = UpdateMethods.Update;
|
||||
public delegate void CustomUpdatedEventHandler(HashSet<T> set);
|
||||
public event CustomUpdatedEventHandler CustomUpdated;
|
||||
public void CustomUpdate(HashSet<T> set) => CustomUpdated?.Invoke(set);
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => Data.GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
9
src/DataStructures/PatchableItems/UpdateMethods.cs
Normal file
9
src/DataStructures/PatchableItems/UpdateMethods.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Polonium.DataStructures.PatchableItems;
|
||||
|
||||
public enum UpdateMethods
|
||||
{
|
||||
None = 0,
|
||||
Update = 1,
|
||||
Replace = 2,
|
||||
Custom = 3,
|
||||
}
|
||||
12
src/Extensions/LinqExtensions.cs
Normal file
12
src/Extensions/LinqExtensions.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Godot;
|
||||
|
||||
namespace Polonium.Extensions;
|
||||
|
||||
public static class LinqExtensions
|
||||
{
|
||||
public static T RandomSelect<T>(this IEnumerable<T> source)
|
||||
{
|
||||
T[] s = source.ToArray();
|
||||
return s[GD.Randi() % s.Length];
|
||||
}
|
||||
}
|
||||
12
src/Extensions/MessageClientExtension.cs
Normal file
12
src/Extensions/MessageClientExtension.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Polonium.Interfaces;
|
||||
using Polonium.MessageManager;
|
||||
|
||||
namespace Polonium.Extensions;
|
||||
|
||||
public static class MessageClientExtension
|
||||
{
|
||||
public static void SendMessage(this IMessageClient client, PoloniumMessage msg)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
6
src/Interfaces/IKnowledge.cs
Normal file
6
src/Interfaces/IKnowledge.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Polonium.Interfaces;
|
||||
|
||||
public interface IKnowledge : IPatchableFrame
|
||||
{
|
||||
|
||||
}
|
||||
15
src/Interfaces/IMessageClient.cs
Normal file
15
src/Interfaces/IMessageClient.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Godot;
|
||||
using Polonium.MessageManager;
|
||||
|
||||
namespace Polonium.Interfaces;
|
||||
|
||||
public interface IMessageClient
|
||||
{
|
||||
string PostCode { get; set; }
|
||||
void ReceiveMessage(PoloniumMessage msg);
|
||||
|
||||
delegate void MessageSentEventHandler(PoloniumMessage msg);
|
||||
event MessageSentEventHandler MessageSent;
|
||||
|
||||
public void SendMessage(PoloniumMessage msg);
|
||||
}
|
||||
5
src/Interfaces/IPatchableFrame.cs
Normal file
5
src/Interfaces/IPatchableFrame.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace Polonium.Interfaces;
|
||||
|
||||
public interface IPatchableFrame
|
||||
{
|
||||
}
|
||||
9
src/Interfaces/IPoloniumFactory.cs
Normal file
9
src/Interfaces/IPoloniumFactory.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Polonium.MessageManager;
|
||||
using Polonium.Resources;
|
||||
|
||||
namespace Polonium.Interfaces;
|
||||
|
||||
public interface IPoloniumFactory
|
||||
{
|
||||
public MessageHeader CreateMessageHeader();
|
||||
}
|
||||
33
src/ItemManagers/Generic/SkinItem.cs
Normal file
33
src/ItemManagers/Generic/SkinItem.cs
Normal 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
36
src/ItemManagers/Item.cs
Normal 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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
53
src/ItemManagers/SkinItem.cs
Normal file
53
src/ItemManagers/SkinItem.cs
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
61
src/MessageManager/MessageBus.cs
Normal file
61
src/MessageManager/MessageBus.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Godot;
|
||||
using Polonium.Interfaces;
|
||||
|
||||
namespace Polonium.MessageManager;
|
||||
|
||||
public abstract partial class MessageBus : Node
|
||||
{
|
||||
public Dictionary<string, HashSet<IMessageClient>> Clients { get; } = new();
|
||||
|
||||
public abstract string NewPostCode();
|
||||
|
||||
public void Register(IMessageClient client)
|
||||
{
|
||||
if(!Clients.ContainsKey(client.PostCode))
|
||||
Clients[client.PostCode] = new HashSet<IMessageClient>();
|
||||
Clients[client.PostCode].Add(client);
|
||||
client.MessageSent += Collect;
|
||||
}
|
||||
public Queue<PoloniumMessage> Messages { get; } = new();
|
||||
public Queue<PoloniumMessage> DeadMessages { get; } = new();
|
||||
public void Collect(PoloniumMessage message) => Messages.Enqueue(message);
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
PoloniumRegistry.Instance.MessageBus = this;
|
||||
base._Ready();
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if(DeadMessages.Count > 0)
|
||||
Messages.Enqueue(DeadMessages.Dequeue());
|
||||
if (Messages.Count == 0)
|
||||
return;
|
||||
PoloniumMessage msg = Messages.Dequeue();
|
||||
HashSet<string> errored = new();
|
||||
foreach (string postcode in msg.Header.Receivers)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
foreach(IMessageClient client in Clients[postcode])
|
||||
client.ReceiveMessage(msg);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
errored.Add(postcode);
|
||||
}
|
||||
}
|
||||
if (errored.Count > 0)
|
||||
{
|
||||
msg.Header.Retried++;
|
||||
if(msg.Header.Retried <= msg.Header.MaxRetries)
|
||||
{
|
||||
msg.Header.Receivers = errored;
|
||||
DeadMessages.Enqueue(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
12
src/MessageManager/MessageHeader.cs
Normal file
12
src/MessageManager/MessageHeader.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Godot;
|
||||
|
||||
namespace Polonium.MessageManager;
|
||||
|
||||
public abstract partial class MessageHeader : Resource
|
||||
{
|
||||
public HashSet<string> Receivers { get; set; }
|
||||
public string Sender { get; set; }
|
||||
|
||||
public int Retried { get; set; } = 0;
|
||||
public int MaxRetries { get; set; } = 0;
|
||||
}
|
||||
8
src/MessageManager/PoloniumMessage.cs
Normal file
8
src/MessageManager/PoloniumMessage.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Godot;
|
||||
|
||||
namespace Polonium.MessageManager;
|
||||
|
||||
public abstract partial class PoloniumMessage : Resource
|
||||
{
|
||||
public MessageHeader Header { get; set; }
|
||||
}
|
||||
@@ -1,25 +1,73 @@
|
||||
using Godot;
|
||||
using Polonium.Agents;
|
||||
using Polonium.Attributes;
|
||||
using Polonium.Interfaces;
|
||||
using Polonium.MessageManager;
|
||||
using Polonium.Resources;
|
||||
|
||||
namespace Polonium;
|
||||
|
||||
public class PoloniumRegistry
|
||||
{
|
||||
public static IGlobalRegistry GlobalRegistry { get; set; }
|
||||
private static PoloniumRegistry InternalInstance { get; set; }
|
||||
public static PoloniumRegistry Instance => InternalInstance ??= new PoloniumRegistry();
|
||||
[RegistryPassThrough]
|
||||
public Config Config { get; set; }
|
||||
[RegistryPassThrough]
|
||||
public Save Save { get; set; }
|
||||
[RegistryPassThrough]
|
||||
public World CurrentWorld { get; set; }
|
||||
|
||||
[RegistryPassThrough]
|
||||
public IPoloniumFactory PoloniumFactory { get; set; }
|
||||
[RegistryPassThrough]
|
||||
public MessageBus MessageBus { get; set; }
|
||||
|
||||
|
||||
public static void Prepare()
|
||||
{
|
||||
DirAccess.MakeDirAbsolute("user://saves");
|
||||
}
|
||||
public Dictionary<string, Agent> Agents { get; set; } = new();
|
||||
// ReSharper disable once CollectionNeverQueried.Global
|
||||
[RegistryPassThrough(true)]
|
||||
public Dictionary<string, Agent> Agents { get; } = new();
|
||||
|
||||
public HashSet<ITimeConsumer> TimeConsumers { get; set; } = new();
|
||||
// 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
7
src/PoloniumTemplate.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Polonium;
|
||||
|
||||
public abstract class PoloniumTemplate<TObj>
|
||||
{
|
||||
public abstract TObj Get { get; }
|
||||
public abstract void Modify(TObj obj);
|
||||
}
|
||||
9
src/Resources/FramePatches/FramePatch.cs
Normal file
9
src/Resources/FramePatches/FramePatch.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Polonium.Interfaces;
|
||||
using Polonium.MessageManager;
|
||||
|
||||
namespace Polonium.Resources.FramePatches;
|
||||
|
||||
public abstract partial class FramePatch : PoloniumMessage
|
||||
{
|
||||
public abstract void Patch(IPatchableFrame receiver);
|
||||
}
|
||||
13
src/Resources/FramePatches/FramePatchCollection.cs
Normal file
13
src/Resources/FramePatches/FramePatchCollection.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Polonium.Interfaces;
|
||||
|
||||
namespace Polonium.Resources.FramePatches;
|
||||
|
||||
public partial class FramePatchCollection : FramePatches.FramePatch
|
||||
{
|
||||
public HashSet<FramePatches.FramePatch> Updates { get; set; } = new();
|
||||
public override void Patch(IPatchableFrame receiver)
|
||||
{
|
||||
foreach (FramePatches.FramePatch p in Updates)
|
||||
p.Patch(receiver);
|
||||
}
|
||||
}
|
||||
21
src/Resources/FramePatches/Generic/DictionaryFramePatch.cs
Normal file
21
src/Resources/FramePatches/Generic/DictionaryFramePatch.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
namespace Polonium.Resources.FramePatches.Generic;
|
||||
|
||||
public abstract partial class DictionaryFramePatch<TKey, TValue> : FramePatch
|
||||
{
|
||||
public Dictionary<TKey, TValue> Updates { get; set; } = new ();
|
||||
|
||||
public Dictionary<TKey, TValue> Filter(HashSet<TKey> filter)
|
||||
{
|
||||
Dictionary<TKey, TValue> res = new();
|
||||
foreach (TKey key in filter)
|
||||
{
|
||||
if (!Updates.ContainsKey(key))
|
||||
continue;
|
||||
res[key] = Updates[key];
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
7
src/Resources/FramePatches/Generic/FramePatch.cs
Normal file
7
src/Resources/FramePatches/Generic/FramePatch.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Polonium.Resources.FramePatches.Generic;
|
||||
|
||||
public abstract partial class FramePatch<TUpdate> : FramePatch
|
||||
{
|
||||
public TUpdate Updates { get; set; } = default(TUpdate);
|
||||
|
||||
}
|
||||
6
src/Resources/FramePatches/Generic/HashSetFramePatch.cs
Normal file
6
src/Resources/FramePatches/Generic/HashSetFramePatch.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Polonium.Resources.FramePatches.Generic;
|
||||
|
||||
public abstract partial class HashSetFramePatch<TUpdate> : FramePatch
|
||||
{
|
||||
public HashSet<TUpdate> Updates { get; set; } = new ();
|
||||
}
|
||||
14
src/Resources/FramePatches/IndexedFramePatchCollection.cs
Normal file
14
src/Resources/FramePatches/IndexedFramePatchCollection.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Polonium.Interfaces;
|
||||
|
||||
namespace Polonium.Resources.FramePatches;
|
||||
|
||||
public partial class IndexedFramePatchCollection<TKey> : FramePatches.FramePatch
|
||||
{
|
||||
public Dictionary<TKey, FramePatches.FramePatch> Updates { get; set; } = new();
|
||||
|
||||
public override void Patch(IPatchableFrame receiver)
|
||||
{
|
||||
foreach (FramePatches.FramePatch p in Updates.Values)
|
||||
p.Patch(receiver);
|
||||
}
|
||||
}
|
||||
8
src/SkinManagers/Generic/AutoPaletteSkin.cs
Normal file
8
src/SkinManagers/Generic/AutoPaletteSkin.cs
Normal 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;
|
||||
}
|
||||
12
src/SkinManagers/Generic/Skin.cs
Normal file
12
src/SkinManagers/Generic/Skin.cs
Normal 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
112
src/SkinManagers/Skin.cs
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
43
src/SkinManagers/SkinPacker.cs
Normal file
43
src/SkinManagers/SkinPacker.cs
Normal 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");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -33,8 +33,13 @@ public static class Utils
|
||||
{
|
||||
if (!fname.EndsWith(".png"))
|
||||
continue;
|
||||
AnimatedTextureCache[path].SetFrameTexture(f, ResourceLoader.Load<Texture2D>(fname));
|
||||
AnimatedTextureCache[path].SetFrameTexture(f, ResourceLoader.Load<Texture2D>($"{path}/{fname}"));
|
||||
f += 1;
|
||||
}
|
||||
|
||||
AnimatedTextureCache[path].SetFrames(f + 1);
|
||||
AnimatedTextureCache[path].SetOneShot(false);
|
||||
AnimatedTextureCache[path].SetSpeedScale(4);
|
||||
}
|
||||
|
||||
return AnimatedTextureCache[path];
|
||||
@@ -44,6 +49,7 @@ public static class Utils
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#pragma warning restore CS0618
|
||||
#pragma warning restore CS0168
|
||||
Reference in New Issue
Block a user