Compare commits

...

15 Commits

Author SHA1 Message Date
f7d1c5a66b add: README 2025-03-07 16:31:40 +00:00
1456f5dd7d update: upgrade to godot 4.4.0 2025-03-04 12:26:15 +00:00
93fbe0869b add: Patchable items and frames 2025-03-04 11:58:14 +00:00
684763f0cc add: MessageBus 2025-03-03 11:08:25 +00:00
d2f0ab8153 add: Item Manager 2025-02-28 08:49:32 +00:00
e90f701bd5 redesign: Agents 2025-02-27 15:39:28 +00:00
8df4ebb6d5 add: Selectable Tile Map Layer/Mouse Controlled Camera 2025-02-26 20:17:37 +00:00
92770d3425 fix: disabled button turns into focused after click 2025-02-22 05:19:16 +00:00
50ca3f75ec add: Template Define 2025-02-22 03:56:27 +00:00
0e00bacfeb test: vcs.xml 2025-02-21 04:56:53 +00:00
ee4e489219 improve: clean up 2025-02-20 10:42:46 +00:00
d720b03480 add: registry pass through 2025-02-19 17:09:07 +00:00
2c32af6b42 improve: better version control 2025-02-18 21:22:47 +00:00
b21023ba24 fix: missing type 2025-02-18 16:05:37 +00:00
2d0b25f65c Merge pull request 'draft_texture_button' (#2) from draft_texture_button into master
Reviewed-on: #2
2025-02-18 14:29:28 +00:00
66 changed files with 1392 additions and 225 deletions

8
.gitignore vendored
View File

@@ -2,4 +2,10 @@ bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
/_ReSharper.Caches/
Package/embedded/polonium_templates/
Package/embedded/Patches/RegistryPassThrough.p.cs
VersionInfo.props
Polonium.sln.DotSettings.user
.godot
.idea

View File

@@ -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

View File

@@ -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>

View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@@ -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>

View File

@@ -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">
@@ -40,9 +41,17 @@
<ItemGroup>
<EmbeddedFiles Include="$(MSBuildThisFileDirectory)embedded/**/*.*"/>
</ItemGroup>
<Copy
SourceFiles="@(EmbeddedFiles)"
DestinationFolder="$(ProjectDir)embedded/%(RecursiveDir)"
<Copy
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/**/*.*" />

View File

@@ -0,0 +1,2 @@
[embedded/*]
generated_code = true

1
Package/build/gitignore Normal file
View File

@@ -0,0 +1 @@
*

View File

@@ -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;

View File

@@ -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;
}
}
}

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -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)
{
}
}

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.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 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 HashSet<ITimeConsumer> TimeConsumers { get; } = [];
public static void Prepare() => PoloniumRegistry.Prepare();
// ReSharper disable once PartialTypeWithSinglePart

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

@@ -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,16 +25,24 @@
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" />
<Compile Remove="Package/**/*.*"/>
<None Include="README.md" />
<Compile Remove="Package/**/*.*" />
</ItemGroup>
<ItemGroup>
<None Include="Package/build/editorconfig" />
</ItemGroup>
<Target Name="CleanPreviousPackages" BeforeTargets="Build">

70
README.md Normal file
View 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 Godots 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 games 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 Poloniums 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 Poloniums 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 Poloniums own code generation for its embedded templates; end users typically dont 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
View File

@@ -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
View 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
View 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);
}

View File

@@ -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()
public sealed override void _Ready()
{
if(Level < MaxLevel)
Level++;
Knowledge = GetNode<Knowledge>("Knowledge");
ActionSet = GetNode<ActionSet>("ActionSet");
__Ready();
base._Ready();
}
public void Act<TAction>(AgentAction.Parameter parameter)
where TAction : AgentAction
{
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();
}
}
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 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();
}
public Agent(string name, TAgentDecisionMaker decisionMaker, TAgentKnowledge knowledge) : base(name)
{
DecisionMaker = decisionMaker;
Knowledge = knowledge;
}
}

View File

@@ -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 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)
{
}
}
}
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 bool Execute(ActionParameter parameter)
{
if (Disabled || CoolDown > 0)
return false;
CoolDownTime = CoolDown;
return true;
}
private double CoolDownTime { get; set; }
public bool Disabled { get; set; }
private double CoolDown { get; }
public void Process(double delta)
{
if (Disabled || CoolDownTime <= 0)
return;
CoolDownTime -= delta;
if (CoolDownTime <= 0)
CoolDownTime = 0;
}
}

View File

@@ -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)
{
}
}

View File

@@ -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);
}

View File

@@ -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
View 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);
}

View 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
View 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
View 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();
}
}

View File

@@ -0,0 +1,6 @@
namespace Polonium.Attributes;
[AttributeUsage(AttributeTargets.Interface)]
public class AutoPatch : Attribute
{
}

View File

@@ -0,0 +1,6 @@
namespace Polonium.Attributes;
[AttributeUsage(AttributeTargets.Property)]
public class PatchableProperty : Attribute
{
}

View File

@@ -0,0 +1,6 @@
namespace Polonium.Attributes;
[AttributeUsage(AttributeTargets.Property)]
public class RegistryPassThrough(bool getterOnly = false) : Attribute
{
public bool GetterOnly { get; set; } = getterOnly;
}

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,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;
}

View 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;
}

View 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;
}

View 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();
}

View 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();
}

View File

@@ -0,0 +1,9 @@
namespace Polonium.DataStructures.PatchableItems;
public enum UpdateMethods
{
None = 0,
Update = 1,
Replace = 2,
Custom = 3,
}

View 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];
}
}

View 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)
{
}
}

View File

@@ -0,0 +1,6 @@
namespace Polonium.Interfaces;
public interface IKnowledge : IPatchableFrame
{
}

View 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);
}

View File

@@ -0,0 +1,5 @@
namespace Polonium.Interfaces;
public interface IPatchableFrame
{
}

View File

@@ -0,0 +1,9 @@
using Polonium.MessageManager;
using Polonium.Resources;
namespace Polonium.Interfaces;
public interface IPoloniumFactory
{
public MessageHeader CreateMessageHeader();
}

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

@@ -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);
}
}
}
}

View 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;
}

View File

@@ -0,0 +1,8 @@
using Godot;
namespace Polonium.MessageManager;
public abstract partial class PoloniumMessage : Resource
{
public MessageHeader Header { get; set; }
}

View File

@@ -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();
public HashSet<ITimeConsumer> TimeConsumers { get; set; } = new();
// ReSharper disable once CollectionNeverQueried.Global
[RegistryPassThrough(true)]
public Dictionary<string, Agent> Agents { get; } = 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
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

@@ -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);
}

View 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);
}
}

View 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;
}
}

View File

@@ -0,0 +1,7 @@
namespace Polonium.Resources.FramePatches.Generic;
public abstract partial class FramePatch<TUpdate> : FramePatch
{
public TUpdate Updates { get; set; } = default(TUpdate);
}

View File

@@ -0,0 +1,6 @@
namespace Polonium.Resources.FramePatches.Generic;
public abstract partial class HashSetFramePatch<TUpdate> : FramePatch
{
public HashSet<TUpdate> Updates { get; set; } = new ();
}

View 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);
}
}

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");
}
}

View File

@@ -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