Try load chunk data.
All checks were successful
Build Dusk / build-linux (push) Successful in 53s
Build Dusk / build-psp (push) Successful in 56s

This commit is contained in:
2025-11-16 15:02:18 -06:00
parent 750e8840f0
commit 9a59c22288
6 changed files with 129 additions and 12 deletions

View File

@@ -0,0 +1,53 @@
import json
import os
from dusk.event import Event
class ChunkData:
def __init__(self, mapData, x, y, z):
self.mapData = mapData
self.x = x
self.y = y
self.z = z
self.current = {}
self.original = {}
self.onChunkData = Event()
def load(self):
fname = self.getFilename()
if not fname or not os.path.exists(fname):
self.new()
return
try:
with open(fname, 'r') as f:
self.current = json.load(f)
self.original = json.loads(json.dumps(self.current)) # Deep copy
self.onChunkData.invoke(self.current)
except Exception as e:
raise RuntimeError(f"Failed to load chunk file: {e}")
def save(self):
fname = self.getFilename()
if not fname:
raise ValueError("No filename specified for saving chunk.")
try:
with open(fname, 'w') as f:
json.dump(self.current, f, indent=2)
self.original = json.loads(json.dumps(self.current)) # Deep copy
except Exception as e:
raise RuntimeError(f"Failed to save chunk file: {e}")
def new(self):
self.current = {}
self.original = {}
self.onChunkData.invoke(self.current)
def isDirty(self):
return json.dumps(self.current, sort_keys=True) != json.dumps(self.original, sort_keys=True)
def getFilename(self):
if not self.mapData or not hasattr(self.mapData, 'getMapDirectory'):
return None
dir_path = self.mapData.getMapDirectory()
if dir_path is None:
return None
return f"{dir_path}/{self.x}_{self.y}_{self.z}.json"