Preserve approved pixel tower prototype as development baseline
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
class_name SimulationSnapshot
|
||||
extends RefCounted
|
||||
|
||||
const SCHEMA_VERSION := 1
|
||||
|
||||
static func capture(clock: SimulationKernel, state: Node, extra := {}) -> Dictionary:
|
||||
var snapshot := {
|
||||
&"schema_version": SCHEMA_VERSION,
|
||||
&"clock": clock.clock_snapshot(),
|
||||
&"game_state": state.create_snapshot(),
|
||||
}
|
||||
if extra is Dictionary and not extra.is_empty():
|
||||
snapshot[&"systems"] = extra.duplicate(true)
|
||||
return snapshot
|
||||
|
||||
static func validate(snapshot: Dictionary) -> Dictionary:
|
||||
var errors: Array[String] = []
|
||||
if int(snapshot.get(&"schema_version", -1)) != SCHEMA_VERSION:
|
||||
errors.append("unsupported_schema_version")
|
||||
if not snapshot.get(&"clock", null) is Dictionary:
|
||||
errors.append("missing_clock")
|
||||
else:
|
||||
var clock_data: Dictionary = snapshot[&"clock"]
|
||||
if int(clock_data.get(&"absolute_minute", -1)) < 0:
|
||||
errors.append("invalid_absolute_minute")
|
||||
if int(clock_data.get(&"speed", 0)) not in [1, 2, 4]:
|
||||
errors.append("invalid_speed")
|
||||
if not snapshot.get(&"game_state", null) is Dictionary:
|
||||
errors.append("missing_game_state")
|
||||
return {
|
||||
&"ok": errors.is_empty(),
|
||||
&"errors": errors,
|
||||
}
|
||||
|
||||
static func restore(snapshot: Dictionary, clock: SimulationKernel, state: Node) -> bool:
|
||||
var validation := validate(snapshot)
|
||||
if not validation[&"ok"]:
|
||||
return false
|
||||
var previous_clock := clock.clock_snapshot()
|
||||
var previous_state: Dictionary = state.create_snapshot()
|
||||
if not clock.restore_clock(snapshot[&"clock"]):
|
||||
return false
|
||||
if not state.restore_snapshot(snapshot[&"game_state"]):
|
||||
clock.restore_clock(previous_clock)
|
||||
state.restore_snapshot(previous_state)
|
||||
return false
|
||||
return true
|
||||
Reference in New Issue
Block a user