Pickups + Save-diff

This commit is contained in:
anonoe 2026-02-10 15:18:49 +01:00
parent b49c0ee55f
commit bf874ba633
Signed by: anonoe
SSH key fingerprint: SHA256:OnAs6gNQelOnDiY5tBpDYKQiuTgBvnmIdMo5P09cdqg
11 changed files with 133 additions and 16 deletions

View file

@ -0,0 +1,36 @@
using Godot;
using System.Text.Json;
public partial class SaveManager : Node
{
public SaveState State { get; private set; } = new();
private const string SavePath = "user://save.json";
public override void _Ready()
{
Load();
}
public void Load()
{
if (!FileAccess.FileExists(SavePath))
{
GD.Print("No save found, starting fresh.");
State = new SaveState();
return;
}
var json = FileAccess.GetFileAsString(SavePath);
State = JsonSerializer.Deserialize<SaveState>(json) ?? new SaveState();
GD.Print($"Loaded save. CollectedPickups={State.CollectedPickups.Count}");
}
public void Save()
{
var json = JsonSerializer.Serialize(State, new JsonSerializerOptions { WriteIndented = true });
using var f = FileAccess.Open(SavePath, FileAccess.ModeFlags.Write);
f.StoreString(json);
GD.Print("Saved.");
}
}

View file

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

View file

@ -0,0 +1,9 @@
using System.Collections.Generic;
//using Godot;
public class SaveState
{
public HashSet<string> CollectedPickups { get; set; } = new();
//public Vector2I? LastPlayerChunk { get; set; } // optional debug, requires "using Godot;"
//public int[]? LastPlayerChunk { get; set; } // [x,y] same debug as above, does not require Godot
}

View file

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

View file

@ -8,10 +8,11 @@ public partial class ChunkManager : Node2D
[Export] public NodePath PlayerPath { get; set; }
[Export] public NodePath PackManagerPath { get; set; }
[Export] public int LoadRadius { get; set; } = 1; // 1 => 3x3
[Export] public PackedScene PickupMarkerScene { get; set; }
[Export] public NodePath SaveManagerPath { get; set; }
private SaveManager? _save;
private CharacterBody2D? _player;
private PackManager? _packs;
private WorldIndex? _world;
private Dictionary<(int x, int y), string> _chunkMap = new();
private readonly Dictionary<(int x, int y), Node2D> _loaded = new();
@ -31,7 +32,10 @@ public partial class ChunkManager : Node2D
GD.PrintErr("ChunkManager: PackManagerPath not set or invalid.");
return;
}
_save = GetNodeOrNull<SaveManager>(SaveManagerPath);
if (_save == null) GD.PrintErr("ChunkManager: SaveManagerPath not set or invalid.");
LoadWorldIndex();
}
@ -154,21 +158,33 @@ public partial class ChunkManager : Node2D
return n;
}
private static void AddChunkMarkers(Node2D chunkNode, ChunkData data)
private void AddChunkMarkers(Node2D chunkNode, ChunkData data)
{
// Pickups: small green squares
if (data.pickups != null)
if (_save == null || PickupMarkerScene == null) return;
if (data.pickups != null && _save != null)
{
foreach (var p in data.pickups)
{
var r = new ColorRect
if (_save.State.CollectedPickups.Contains(p.id))
continue; // already collected
var marker = (PickupMarker)PickupMarkerScene.Instantiate();
marker.Position = new Vector2(p.pos[0], p.pos[1]);
marker.PickupId = p.id;
marker.ItemId = p.item;
marker.PickedUp += (pickupId, itemId) =>
{
Size = new Vector2(10, 10),
Color = new Color(0.2f, 1.0f, 0.2f, 0.9f),
MouseFilter = Control.MouseFilterEnum.Ignore,
Position = new Vector2(p.pos[0] - 5, p.pos[1] - 5)
GD.Print($"Picked up {itemId} ({pickupId})");
_save.State.CollectedPickups.Add(pickupId);
_save.Save();
marker.QueueFree();
};
chunkNode.AddChild(r);
chunkNode.AddChild(marker);
}
}

View file

@ -0,0 +1,23 @@
using Godot;
public partial class PickupMarker : Area2D
{
public string PickupId { get; set; } = "";
public string ItemId { get; set; } = "";
public override void _Ready()
{
BodyEntered += OnBodyEntered;
}
private void OnBodyEntered(Node2D body)
{
if (body is not CharacterBody2D) return;
// Defer to avoid freeing during signal processing edge cases
EmitSignal(SignalName.PickedUp, PickupId, ItemId);
}
[Signal]
public delegate void PickedUpEventHandler(string pickupId, string itemId);
}

View file

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

View file

@ -0,0 +1,11 @@
using Godot;
public partial class PickupVisual : Node2D
{
public override void _Draw()
{
DrawRect(new Rect2(-5, -5, 10, 10), new Color(0.2f, 1f, 0.2f, 0.9f));
}
public override void _Ready() => QueueRedraw();
}

View file

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