add: Template for Save and config
This commit is contained in:
6
src/Attributes/RegistryEntity.cs
Normal file
6
src/Attributes/RegistryEntity.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Polonium.Attributes;
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class RegistryEntity : Attribute
|
||||
{
|
||||
|
||||
}
|
||||
19
src/PoloniumRegistry.cs
Normal file
19
src/PoloniumRegistry.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Godot;
|
||||
using Polonium.Resources;
|
||||
using Polonium.Scenes;
|
||||
|
||||
namespace Polonium;
|
||||
|
||||
public class PoloniumRegistry
|
||||
{
|
||||
private static PoloniumRegistry InternalInstance { get; set; }
|
||||
public static PoloniumRegistry Instance => InternalInstance ??= new PoloniumRegistry();
|
||||
public RootScene RootScene { get; set; }
|
||||
public Config Config { get; set; }
|
||||
public Save Save { get; set; }
|
||||
|
||||
public static void Prepare()
|
||||
{
|
||||
DirAccess.MakeDirAbsolute("user://saves");
|
||||
}
|
||||
}
|
||||
16
src/Resources/Config.cs
Normal file
16
src/Resources/Config.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Godot;
|
||||
|
||||
namespace Polonium.Resources;
|
||||
|
||||
public partial class Config: Resource
|
||||
|
||||
{
|
||||
|
||||
public void Save() => ResourceSaver.Save(this, "user://Config.tres");
|
||||
|
||||
public static void Load<T>()
|
||||
where T : Config, new()
|
||||
{
|
||||
PoloniumRegistry.Instance.Config = ResourceLoader.Load<T>("user://Config.tres") ?? new T();
|
||||
}
|
||||
}
|
||||
49
src/Resources/Save.cs
Normal file
49
src/Resources/Save.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Godot;
|
||||
|
||||
namespace Polonium.Resources;
|
||||
|
||||
public abstract partial class Save : Resource
|
||||
{
|
||||
[Export]
|
||||
public string Name { get; set; }
|
||||
|
||||
public abstract class Preview
|
||||
{
|
||||
}
|
||||
|
||||
public void SaveToUser() => ResourceSaver.Save(this, $"user://saves/{Name}.tres");
|
||||
|
||||
public static void LoadFromUser<T>(string name)
|
||||
where T : Save, new()
|
||||
{
|
||||
T res = ResourceLoader.Load<T>($"user://saves/{name}.tres");
|
||||
if (res is null)
|
||||
{
|
||||
res = new T();
|
||||
res.Name = name;
|
||||
}
|
||||
|
||||
PoloniumRegistry.Instance.Save = res;
|
||||
}
|
||||
|
||||
public void Load() => PoloniumRegistry.Instance.Save = this;
|
||||
|
||||
public static IEnumerable<T> ListSaves<T>()
|
||||
where T : Save
|
||||
{
|
||||
DirAccess dir = DirAccess.Open("user://saves");
|
||||
if(dir is null)
|
||||
{
|
||||
DirAccess.MakeDirRecursiveAbsolute("user://saves");
|
||||
yield break;
|
||||
}
|
||||
|
||||
foreach (string save in dir.GetFiles())
|
||||
{
|
||||
if(!save.EndsWith(".tres"))
|
||||
continue;
|
||||
yield return ResourceLoader.Load<T>($"user://saves/{save}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,18 @@
|
||||
using Godot;
|
||||
|
||||
namespace Polonium.Scenes;
|
||||
|
||||
public abstract partial class CameraScene : Scene
|
||||
[GlobalClass]
|
||||
[Tool]
|
||||
public partial class CameraScene : Scene
|
||||
{
|
||||
private Camera2D Camera { get; set; }
|
||||
protected abstract float MaxZoom { get; }
|
||||
protected abstract float MinZoom { get; }
|
||||
protected abstract float ZoomRate { get; }
|
||||
[Export(PropertyHint.Range)]
|
||||
public float MaxZoom { get; set; }
|
||||
[Export(PropertyHint.Range)]
|
||||
public float MinZoom { get; set; }
|
||||
[Export(PropertyHint.Range)]
|
||||
public float ZoomRate { get; set; }
|
||||
[Export(PropertyHint.Range)]
|
||||
private float Zoom
|
||||
{
|
||||
get => Camera.Zoom.X;
|
||||
@@ -15,11 +20,12 @@ public abstract partial class CameraScene : Scene
|
||||
}
|
||||
public override void _Ready()
|
||||
{
|
||||
Camera = GetNode<Camera2D>("Camera");
|
||||
Camera = new Camera2D();
|
||||
AddChild(Camera);
|
||||
base._Ready();
|
||||
}
|
||||
protected void ZoomIn() => Mathf.Max(Zoom * (1 + ZoomRate), MaxZoom);
|
||||
protected void ZoomOut() => Mathf.Min(Zoom * (1 - ZoomRate), MinZoom);
|
||||
protected void ZoomIn() => Zoom = Mathf.Max(Zoom * (1 + ZoomRate), MaxZoom);
|
||||
protected void ZoomOut() => Zoom = Mathf.Min(Zoom * (1 - ZoomRate), MinZoom);
|
||||
protected void ZoomAt(Vector2 pos) => Camera.Position = pos;
|
||||
|
||||
}
|
||||
|
||||
14
src/Scenes/LayerCameraScene.cs
Normal file
14
src/Scenes/LayerCameraScene.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Godot;
|
||||
|
||||
namespace Polonium.Scenes;
|
||||
[GlobalClass]
|
||||
public partial class LayerCameraScene : CameraScene
|
||||
{
|
||||
private Node2D LayerHolder { get; set; }
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
LayerHolder = new Node2D();
|
||||
base._Ready();
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,21 @@
|
||||
using Godot;
|
||||
|
||||
namespace Polonium.Scenes;
|
||||
|
||||
public abstract partial class RootScene : Node2D
|
||||
[GlobalClass]
|
||||
public partial class RootScene : Node2D
|
||||
{
|
||||
private Polonium.Scenes.Scene CurrentScene { get; set; }
|
||||
private Scene CurrentScene { get; set; }
|
||||
|
||||
public void SwitchScene(Polonium.Scenes.Scene scene)
|
||||
public void SwitchScene(Scene scene)
|
||||
{
|
||||
if (CurrentScene != null)
|
||||
RemoveChild(CurrentScene);
|
||||
AddChild(scene);
|
||||
CurrentScene = scene;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
PoloniumRegistry.Instance.RootScene = this;
|
||||
base._Ready();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user