Jam10/Scripts/ReplayRecording.gd

54 lines
1.0 KiB
GDScript

extends Object
class_name ReplayRecording
var time_per_frame: float
var frames: int = 0
var positions: Array
var sprites: Array
func load_from(file: File):
time_per_frame = file.get_real()
frames = file.get_32()
positions = []
sprites = []
for i in range(frames):
var x: float = file.get_real()
var y: float = file.get_real()
positions.append(Vector2(x, y))
sprites.append(file.get_8())
func load_from_uri(uri: String):
var file: File = File.new()
file.open(uri, File.READ)
load_from(file)
file.close()
func save_to(file: File):
file.store_real(time_per_frame)
file.store_32(frames)
for i in range(frames):
file.store_real(positions[i].x)
file.store_real(positions[i].y)
file.store_8(sprites[i])
func save_to_uri(uri: String):
var file: File = File.new()
file.open(uri, File.WRITE)
save_to(file)
file.close()
func position(frame: int) -> Vector2:
if frame < frames:
return positions[frame]
else:
return positions[-1]
func sprite(frame: int) -> int:
if frame < frames:
return sprites[frame]
else:
return sprites[-1]