Fixed crash
This commit is contained in:
@@ -1,14 +1,53 @@
|
||||
from editortool.map.chunk import Tile
|
||||
import json
|
||||
import os
|
||||
from dusk.event import Event
|
||||
|
||||
class Chunk:
|
||||
def __init__(self, x=0, y=0, z=0, size_x=1, size_y=1, size_z=1):
|
||||
def __init__(self, map, x, y, z):
|
||||
self.map = map
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.z = z
|
||||
self.data = [[[Tile() for _ in range(size_z)] for _ in range(size_y)] for _ in range(size_x)]
|
||||
self.current = {}
|
||||
self.original = {}
|
||||
self.onChunkData = Event()
|
||||
|
||||
def draw(self):
|
||||
for x in range(len(self.data)):
|
||||
for y in range(len(self.data[x])):
|
||||
for z in range(len(self.data[x][y])):
|
||||
self.data[x][y][z].draw(x, y, z)
|
||||
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.map or not hasattr(self.map, 'getMapDirectory'):
|
||||
return None
|
||||
dir_path = self.map.getMapDirectory()
|
||||
if dir_path is None:
|
||||
return None
|
||||
return f"{dir_path}/{self.x}_{self.y}_{self.z}.json"
|
||||
|
||||
Reference in New Issue
Block a user