basic natural respawn
This commit is contained in:
parent
bf874ba633
commit
63ca3220d1
3 changed files with 35 additions and 6 deletions
|
|
@ -3,7 +3,12 @@ using System.Collections.Generic;
|
|||
|
||||
public class SaveState
|
||||
{
|
||||
// Permanent one-time removals (e.g. chests, unique items)
|
||||
public HashSet<string> CollectedPickups { get; set; } = new();
|
||||
|
||||
// Respawnables: pickupId -> unixTimeSeconds when it becomes available again
|
||||
public Dictionary<string, long> PickupRespawnAt { 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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -158,16 +158,34 @@ public partial class ChunkManager : Node2D
|
|||
return n;
|
||||
}
|
||||
|
||||
private static long NowUnixSeconds()
|
||||
{
|
||||
return DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||
}
|
||||
|
||||
private void AddChunkMarkers(Node2D chunkNode, ChunkData data)
|
||||
{
|
||||
if (_save == null || PickupMarkerScene == null) return;
|
||||
|
||||
if (data.pickups != null && _save != null)
|
||||
{
|
||||
var now = NowUnixSeconds();
|
||||
|
||||
foreach (var p in data.pickups)
|
||||
{
|
||||
if (_save.State.CollectedPickups.Contains(p.id))
|
||||
continue; // already collected
|
||||
if (_save == null || PickupMarkerScene == null)
|
||||
return;
|
||||
|
||||
if (p.respawn_seconds <= 0)
|
||||
{
|
||||
if (_save.State.CollectedPickups.Contains(p.id))
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_save.State.PickupRespawnAt.TryGetValue(p.id, out var respawnAt) && now < respawnAt)
|
||||
continue;
|
||||
}
|
||||
|
||||
var marker = (PickupMarker)PickupMarkerScene.Instantiate();
|
||||
marker.Position = new Vector2(p.pos[0], p.pos[1]);
|
||||
|
|
@ -178,12 +196,18 @@ public partial class ChunkManager : Node2D
|
|||
{
|
||||
GD.Print($"Picked up {itemId} ({pickupId})");
|
||||
|
||||
_save.State.CollectedPickups.Add(pickupId);
|
||||
_save.Save();
|
||||
if (p.respawn_seconds <= 0)
|
||||
{
|
||||
_save.State.CollectedPickups.Add(pickupId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_save.State.PickupRespawnAt[pickupId] = NowUnixSeconds() + p.respawn_seconds;
|
||||
}
|
||||
|
||||
_save.Save();
|
||||
marker.QueueFree();
|
||||
};
|
||||
|
||||
chunkNode.AddChild(marker);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue