Simplify Energy Stuff

This commit is contained in:
2026-06-24 14:39:02 +02:00
parent bfee5f4025
commit 390ac95638
14 changed files with 78 additions and 136 deletions

View File

@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cgameplay_005Ccomponents_005Cenergy/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@@ -1,44 +0,0 @@
using Godot;
using Slimepire.Gameplay.Components;
namespace Slimepire.Core;
[Singleton]
public partial class EnergyManager
{
private List<EnergyProducer> _energyProducers = [];
private List<EnergyStorage> _energyStorages = [];
public void RegisterEnergyProducer(EnergyProducer energyProducer)
{
_energyProducers.Add(energyProducer);
}
public void RemoveEnergyProducer(EnergyProducer energyProducer)
{
_energyProducers.Remove(energyProducer);
}
public void RegisterEnergyStorage(EnergyStorage energyStorage)
{
_energyStorages.Add(energyStorage);
}
public void RemoveEnergyStorage(EnergyStorage energyStorage)
{
_energyStorages.Remove(energyStorage);
}
public void TransferEnergy(float energy)
{
foreach (var energyStorage in _energyStorages)
{
if (energyStorage.Energy + energy <= energyStorage.MaxEnergy)
{
energyStorage.AcceptEnergy(energy);
}
}
}
public void RequestEnergy(float energy) { }
}

View File

@@ -1 +0,0 @@
uid://boaiqjlh7qq3o

View File

@@ -0,0 +1,17 @@
using Godot;
namespace Slimepire.Gameplay.Components;
public partial class EnergyProducer : Node
{
[Export]
public EnergyStorage EnergyStorage = null!;
[Export]
public float ProductionRate = 1;
public override void _Process(double delta)
{
EnergyStorage.Energy += ProductionRate * (float)delta;
}
}

View File

@@ -1,6 +1,6 @@
[gd_scene format=3 uid="uid://t0gdf172f65v"] [gd_scene format=3 uid="uid://t0gdf172f65v"]
[ext_resource type="Script" uid="uid://bku1vey63ew65" path="res://src/Gameplay/Components/EnergyProducer.cs" id="1_4q7h4"] [ext_resource type="Script" uid="uid://bku1vey63ew65" path="res://src/Gameplay/Components/Energy/EnergyProducer.cs" id="1_4q7h4"]
[node name="EnergyProducer" type="Node" unique_id=1184182162] [node name="EnergyProducer" type="Node" unique_id=1184182162]
script = ExtResource("1_4q7h4") script = ExtResource("1_4q7h4")

View File

@@ -0,0 +1,47 @@
using Godot;
namespace Slimepire.Gameplay.Components;
[GlobalClass]
public partial class EnergyStorage : Node
{
[Export]
public Label EnergyLabel = null!;
[Export]
public float MaxEnergy = 100;
public float Energy
{
get;
set
{
if (field + value > MaxEnergy)
{
field = MaxEnergy;
}
else
{
field += value;
}
field = value;
UpdateLabel();
}
}
public override void _Ready()
{
UpdateLabel();
}
public override void _ExitTree()
{
// EnergyManager.Instance.RemoveEnergyStorage(this);
}
private void UpdateLabel()
{
EnergyLabel.Text = $"{Energy:0}/{MaxEnergy}";
}
}

View File

@@ -1,6 +1,6 @@
[gd_scene format=3 uid="uid://by6iw05ovnhq8"] [gd_scene format=3 uid="uid://by6iw05ovnhq8"]
[ext_resource type="Script" uid="uid://dgkeo1wsicv13" path="res://src/Gameplay/Components/EnergyStorage.cs" id="1_vetx7"] [ext_resource type="Script" uid="uid://dgkeo1wsicv13" path="res://src/Gameplay/Components/Energy/EnergyStorage.cs" id="1_vetx7"]
[node name="EnergyStorage" type="Node" unique_id=1765924301] [node name="EnergyStorage" type="Node" unique_id=1765924301]
script = ExtResource("1_vetx7") script = ExtResource("1_vetx7")

View File

@@ -1,32 +0,0 @@
using Godot;
using Slimepire.Core;
namespace Slimepire.Gameplay.Components;
public partial class EnergyProducer : Node
{
[Export]
public float ProductionRate = 1;
private float _energy;
public override void _Ready()
{
EnergyManager.Instance.RegisterEnergyProducer(this);
}
public override void _Process(double delta)
{
_energy += ProductionRate * (float)delta;
if (_energy > 1)
{
EnergyManager.Instance.TransferEnergy(_energy);
_energy = 0;
}
}
public override void _ExitTree()
{
EnergyManager.Instance.RemoveEnergyProducer(this);
}
}

View File

@@ -1,28 +0,0 @@
using Godot;
using Slimepire.Core;
namespace Slimepire.Gameplay.Components;
public partial class EnergyStorage : Node
{
[Export]
public float MaxEnergy = 100;
public float Energy { get; private set; }
public override void _Ready()
{
EnergyManager.Instance.RegisterEnergyStorage(this);
}
public override void _ExitTree()
{
EnergyManager.Instance.RemoveEnergyStorage(this);
}
public float AcceptEnergy(float energy)
{
Energy += energy;
return Energy;
}
}

View File

@@ -37,19 +37,11 @@ animations = [{
"speed": 2.0 "speed": 2.0
}] }]
[sub_resource type="LabelSettings" id="LabelSettings_x44k2"] [node name="BasicNodule" type="Node2D" unique_id=1637542829 node_paths=PackedStringArray("_tubeConnectionArea", "_gfx")]
font_size = 12
outline_size = 3
outline_color = Color(0, 0, 0, 1)
shadow_size = 3
shadow_color = Color(0, 0, 0, 1)
[node name="BasicNodule" type="Node2D" unique_id=1637542829 node_paths=PackedStringArray("_tubeConnectionArea", "_gfx", "_energyLabel")]
z_index = 1 z_index = 1
script = ExtResource("1_wbqr8") script = ExtResource("1_wbqr8")
_tubeConnectionArea = NodePath("TubeConnectionArea") _tubeConnectionArea = NodePath("TubeConnectionArea")
_gfx = NodePath("Gfx") _gfx = NodePath("Gfx")
_energyLabel = NodePath("EnergyLabel")
[node name="TubeConnectionArea" parent="." unique_id=1821668398 instance=ExtResource("2_dehe6")] [node name="TubeConnectionArea" parent="." unique_id=1821668398 instance=ExtResource("2_dehe6")]
@@ -64,18 +56,3 @@ texture_filter = 1
sprite_frames = SubResource("SpriteFrames_xyjsl") sprite_frames = SubResource("SpriteFrames_xyjsl")
autoplay = "default" autoplay = "default"
frame_progress = 0.120879106 frame_progress = 0.120879106
[node name="EnergyLabel" type="Label" parent="." unique_id=466024399]
unique_name_in_owner = true
anchors_preset = 5
anchor_left = 0.5
anchor_right = 0.5
offset_left = -20.0
offset_top = 14.0
offset_right = 20.0
offset_bottom = 31.0
grow_horizontal = 2
text = "5"
label_settings = SubResource("LabelSettings_x44k2")
horizontal_alignment = 1
vertical_alignment = 1

View File

@@ -11,9 +11,6 @@ public partial class Nodule : Node2D
[Export] [Export]
private AnimatedSprite2D _gfx = null!; private AnimatedSprite2D _gfx = null!;
[Export]
private Label _energyLabel = null!;
public List<Nodule> Connections { get; } = []; public List<Nodule> Connections { get; } = [];
protected AnimatedSprite2D Gfx => _gfx; protected AnimatedSprite2D Gfx => _gfx;

View File

@@ -2,6 +2,8 @@
[ext_resource type="PackedScene" uid="uid://bjesfu1mfx8l5" path="res://src/Gameplay/Components/TubeConnectionArea.tscn" id="1_b03xe"] [ext_resource type="PackedScene" uid="uid://bjesfu1mfx8l5" path="res://src/Gameplay/Components/TubeConnectionArea.tscn" id="1_b03xe"]
[ext_resource type="Script" uid="uid://y4cpi50hsaj4" path="res://src/Gameplay/Nodules/RootNodule/RootNodule.cs" id="1_root"] [ext_resource type="Script" uid="uid://y4cpi50hsaj4" path="res://src/Gameplay/Nodules/RootNodule/RootNodule.cs" id="1_root"]
[ext_resource type="PackedScene" uid="uid://t0gdf172f65v" path="res://src/Gameplay/Components/Energy/EnergyProducer.tscn" id="2_cy5pc"]
[ext_resource type="PackedScene" uid="uid://by6iw05ovnhq8" path="res://src/Gameplay/Components/Energy/EnergyStorage.tscn" id="2_gke5o"]
[ext_resource type="PackedScene" uid="uid://3x5pqm4t5cbl" path="res://src/Gameplay/Components/GrowArea.tscn" id="2_tg6hi"] [ext_resource type="PackedScene" uid="uid://3x5pqm4t5cbl" path="res://src/Gameplay/Components/GrowArea.tscn" id="2_tg6hi"]
[ext_resource type="Texture2D" uid="uid://tomd3kojw0qy" path="res://assets/root_nodule.png" id="3_b03xe"] [ext_resource type="Texture2D" uid="uid://tomd3kojw0qy" path="res://assets/root_nodule.png" id="3_b03xe"]
@@ -26,12 +28,17 @@ outline_color = Color(0, 0, 0, 1)
shadow_size = 3 shadow_size = 3
shadow_color = Color(0, 0, 0, 1) shadow_color = Color(0, 0, 0, 1)
[node name="RootNodule" type="Node2D" unique_id=1797926942 node_paths=PackedStringArray("_tubeConnectionArea", "_gfx", "_energyLabel")] [node name="RootNodule" type="Node2D" unique_id=1797926942 node_paths=PackedStringArray("_tubeConnectionArea", "_gfx")]
z_index = 1 z_index = 1
script = ExtResource("1_root") script = ExtResource("1_root")
_tubeConnectionArea = NodePath("TubeConnectionArea") _tubeConnectionArea = NodePath("TubeConnectionArea")
_gfx = NodePath("Gfx") _gfx = NodePath("Gfx")
_energyLabel = NodePath("EnergyLabel")
[node name="EnergyProducer" parent="." unique_id=1184182162 node_paths=PackedStringArray("EnergyStorage") instance=ExtResource("2_cy5pc")]
EnergyStorage = NodePath("../EnergyStorage")
[node name="EnergyStorage" parent="." unique_id=1765924301 node_paths=PackedStringArray("EnergyLabel") instance=ExtResource("2_gke5o")]
EnergyLabel = NodePath("../EnergyLabel")
[node name="TubeConnectionArea" parent="." unique_id=1821668398 instance=ExtResource("1_b03xe")] [node name="TubeConnectionArea" parent="." unique_id=1821668398 instance=ExtResource("1_b03xe")]