实现存档
定义快照
创建实现 IData 的快照,把恢复局面需要的值保存在其中。示例保存玩家、敌人、计时器和随机状态。
csharp
// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using System;
using System.Collections.Generic;
using DodgeTheCreeps.Gameplay;
using GFramework.Game.Abstractions.Content;
using GFramework.Game.Abstractions.Data;
namespace DodgeTheCreeps;
/// <summary>
/// Portable snapshot of one paused survival board. No native nodes, resources or engine vectors are serialized.
/// Version two preserves enemies, fractional timers and the RNG state rather than generating a new board on continue.
/// </summary>
public sealed class DodgeProgress : IData
{
/// <summary>Gets the complete-board format written by this game.</summary>
public const int CurrentVersion = 2;
/// <summary>Bounds native allocations before restoring an externally supplied snapshot.</summary>
public const int MaxSavedEnemies = 512;
/// <summary>Gets or sets the time this portable board was captured.</summary>
public DateTime LastModified { get; set; } = DateTime.UtcNow;
/// <summary>Gets or sets completed survival seconds.</summary>
public int Score
{
get; set;
}
/// <summary>Gets or sets logical horizontal position.</summary>
public float X { get; set; } = 240;
/// <summary>Gets or sets logical vertical position.</summary>
public float Y { get; set; } = 580;
/// <summary>Gets or sets the stable arena ID instead of an engine resource path.</summary>
public string ArenaId { get; set; } = "base:yard";
/// <summary>Gets or sets the player's last heading, clockwise from east in sectors zero through seven.</summary>
public int PlayerFacing
{
get; set;
}
/// <summary>Gets or sets the unscored fraction of one survival second.</summary>
public double ScoreElapsed
{
get; set;
}
/// <summary>Gets or sets the exact time until the next enemy spawn.</summary>
public double SpawnRemaining { get; set; } = 1.5;
/// <summary>Gets or sets remaining collision grace; exact restoration never grants additional grace.</summary>
public double GraceRemaining { get; set; } = 1.5;
/// <summary>Gets or sets the RNG seed, restored before its state.</summary>
public ulong RandomSeed { get; set; } = 1;
/// <summary>Gets or sets the RNG state immediately before the next random draw.</summary>
public ulong? RandomState
{
get; set;
}
/// <summary>Gets or sets every living enemy in the world's stable iteration order.</summary>
public IList<DodgeMobProgress> Enemies { get; set; } = [];
/// <summary>Rejects unsupported or dangerous state before allocating native enemies or modifying a live board.</summary>
/// <param name="save">Snapshot to validate; this method has no engine or persistence side effects.</param>
/// <exception cref="InvalidOperationException">The format, values, enemy count or identity syntax is invalid.</exception>
public static void ValidateSnapshot(DodgeProgress save)
{
ArgumentNullException.ThrowIfNull(save);
ValidateLegacyValues(save);
if (save.X is < 26 or > 454 || save.Y is < 82 or > 686 ||
save.RandomState is null || save.PlayerFacing is < 0 or > 7 || !double.IsFinite(save.ScoreElapsed) || save.ScoreElapsed is < 0 or >= 1 ||
!double.IsFinite(save.SpawnRemaining) || save.SpawnRemaining is < 0 or > 10 ||
!double.IsFinite(save.GraceRemaining) || save.GraceRemaining is < 0 or > 1.5 ||
save.Enemies is null || save.Enemies.Count > MaxSavedEnemies)
throw new InvalidOperationException("The save has invalid timers, facing or enemy count.");
foreach (var enemy in save.Enemies)
{
if (enemy is null)
throw new InvalidOperationException("The save contains an empty enemy record.");
enemy.Validate();
}
}
/// <summary>Checks the limited fields actually recorded by old sparse saves without inventing missing board state.</summary>
/// <param name="save">Sparse or complete portable data.</param>
internal static void ValidateLegacyValues(DodgeProgress save)
{
if (save.Score < 0 || !float.IsFinite(save.X) || !float.IsFinite(save.Y) ||
save.X is < 0 or > 480 || save.Y is < 0 or > 720)
throw new InvalidOperationException("The save has invalid score or player coordinates.");
_ = new ContentId(save.ArenaId);
}
}创建仓库并保存
使用 GodotFileStorage 和 user:// 保存玩家文件。配置游戏身份、槽位路径和当前数据版本,再用 SaveRepository<T> 保存。保存完成后更新继续游戏按钮。
csharp
/// <summary>Writes one explicit save slot, preserving the previous file on failure.</summary>
internal async Task SaveGameAsync()
{
if (_world is null)
return;
var saved = _world.CaptureProgress();
await _saves.SaveDocumentAsync(1, saved, _modState, _lifetime.Token).ConfigureAwait(true);
_saveRetired = false;
_savedPlayerPosition = new Vector2(saved.X, saved.Y);
_dirty = false;
_menus.SetStatus(() => Text.Get("run_saved"));
}加载、恢复和清档
用 PrepareLoadAsync 准备需要转换的文档,按需让玩家确认,成功提交后恢复世界。保留内容的稳定键并使用独立迁移规则。游戏结束时按游戏规则退役当前局的存档。
从原生自检运行持久化流程,并用首轮标识验证新进程继续。