small racing game im working on
Diffstat (limited to 'classes/resources/ghost_data.gd')
| -rw-r--r-- | classes/resources/ghost_data.gd | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/classes/resources/ghost_data.gd b/classes/resources/ghost_data.gd index 7be0426..8ad0915 100644 --- a/classes/resources/ghost_data.gd +++ b/classes/resources/ghost_data.gd @@ -3,6 +3,7 @@ extends Resource var time: float var checkpoints: Array +var snapped_checkpoints: Array # i hope this is performant var snaps: Array#[Dictionary] var snap_count := 0 @@ -16,14 +17,18 @@ func load_snap(i: int) -> Dictionary: return snaps[i] func save(path: String) -> void: - GhostData._save_file(path, {checkpoints = checkpoints, time = time, snaps = snaps, snap_count = snap_count}) + GhostData._save_file(path, {checkpoints = checkpoints, snapped_checkpoints = snapped_checkpoints, time = time, snaps = snaps, snap_count = snap_count}) + +func mkarr(size: int) -> Array: + var arr := [] + arr.resize(size) + arr.fill(-1) + return arr func _init(num_checkpoints := 0, laps := 0) -> void: for i in laps: - var arr := [] - arr.resize(num_checkpoints + 1) - arr.fill(-1) - checkpoints.append(arr) + checkpoints.append(mkarr(num_checkpoints + 1)) + snapped_checkpoints.append(mkarr(num_checkpoints + 1)) func clear() -> void: for lap in checkpoints: @@ -34,11 +39,10 @@ func clear() -> void: func collect(lap: int, cp: int, now: float) -> void: now = snappedf(now, .001) # 3dec precision + checkpoints[lap][cp] = now + snapped_checkpoints[lap][cp] = snap_count if lap == len(checkpoints) - 1 && cp == -1: - checkpoints[lap][cp] = now time = now - else: - checkpoints[lap][cp] = now func has_collected(lap: int, cp: int) -> bool: return checkpoints[lap][cp] != -1 @@ -50,19 +54,20 @@ static func from_d(d: Dictionary) -> GhostData: var obj := GhostData.new() obj.time = d.time obj.checkpoints = d.checkpoints + obj.snapped_checkpoints = d.snapped_checkpoints obj.snap_count = d.snap_count obj.snaps = d.snaps return obj ## Saves a basic dictionary to a path. static func _save_file(path: String, data: Dictionary) -> void: - var file := FileAccess.open_compressed(path, FileAccess.WRITE, FileAccess.COMPRESSION_FASTLZ) + var file := FileAccess.open_compressed(path, FileAccess.WRITE, FileAccess.COMPRESSION_ZSTD) file.store_buffer(var_to_bytes(data)) ## Loads a basic dictionary out of a file. static func _load_file(path: String) -> Dictionary: if FileAccess.file_exists(path): - var file := FileAccess.open_compressed(path, FileAccess.READ, FileAccess.COMPRESSION_FASTLZ) + var file := FileAccess.open_compressed(path, FileAccess.READ, FileAccess.COMPRESSION_ZSTD) var text := file.get_buffer(file.get_length()) var dict := {} if text: |