Editor partially started.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import json
|
||||
import os
|
||||
from dusk.event import Event
|
||||
from editortool.map.mapdefs import CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH, CHUNK_VERTEX_COUNT_MAX
|
||||
from editortool.map.mapdefs import CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH, CHUNK_VERTEX_COUNT_MAX, TILE_SHAPE_NULL
|
||||
from editortool.map.tile import Tile
|
||||
from editortool.map.vertexbuffer import VertexBuffer
|
||||
from OpenGL.GL import *
|
||||
@@ -54,29 +54,41 @@ class Chunk:
|
||||
|
||||
self.tileUpdateVertices()
|
||||
self.dirty = False
|
||||
self.onChunkData.invoke(self.current)
|
||||
self.onChunkData.invoke(self)
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Failed to load chunk file: {e}")
|
||||
|
||||
def save(self):
|
||||
if not self.isDirty():
|
||||
return
|
||||
|
||||
dataOut = {
|
||||
'shapes': []
|
||||
}
|
||||
|
||||
for tile in self.tiles.values():
|
||||
dataOut['shapes'].append(tile.shape)
|
||||
|
||||
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)
|
||||
json.dump(dataOut, f)
|
||||
self.dirty = False
|
||||
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)
|
||||
for tile in self.tiles.values():
|
||||
tile.shape = TILE_SHAPE_NULL
|
||||
|
||||
self.tileUpdateVertices()
|
||||
self.dirty = False
|
||||
self.onChunkData.invoke(self)
|
||||
|
||||
def isDirty(self):
|
||||
return False
|
||||
# return json.dumps(self.current, sort_keys=True) != json.dumps(self.original, sort_keys=True)
|
||||
return self.dirty
|
||||
|
||||
def getFilename(self):
|
||||
if not self.map or not hasattr(self.map, 'getMapDirectory'):
|
||||
@@ -87,5 +99,4 @@ class Chunk:
|
||||
return f"{dir_path}/{self.x}_{self.y}_{self.z}.json"
|
||||
|
||||
def draw(self):
|
||||
glColor3f(0.0, 1.0, 1.0)
|
||||
self.vertexBuffer.draw()
|
||||
@@ -1,4 +1,5 @@
|
||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, QGridLayout, QTreeWidget, QTreeWidgetItem
|
||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, QGridLayout, QTreeWidget, QTreeWidgetItem, QComboBox
|
||||
from editortool.map.mapdefs import CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH, TILE_SHAPES
|
||||
|
||||
class ChunkPanel(QWidget):
|
||||
def __init__(self, parent):
|
||||
@@ -25,6 +26,12 @@ class ChunkPanel(QWidget):
|
||||
grid.addWidget(self.btnE, 1, 2)
|
||||
layout.addLayout(grid)
|
||||
|
||||
# Tile shape dropdown
|
||||
self.tileShapeDropdown = QComboBox()
|
||||
self.tileShapeDropdown.addItems(TILE_SHAPES.keys())
|
||||
self.tileShapeDropdown.setToolTip("Tile Shape")
|
||||
layout.addWidget(self.tileShapeDropdown)
|
||||
|
||||
# Add expandable tree list
|
||||
self.tree = QTreeWidget()
|
||||
self.tree.setHeaderLabel("Chunks")
|
||||
@@ -48,4 +55,30 @@ class ChunkPanel(QWidget):
|
||||
self.btnE.clicked.connect(lambda: self.parent.map.moveRelative(1, 0, 0))
|
||||
self.btnW.clicked.connect(lambda: self.parent.map.moveRelative(-1, 0, 0))
|
||||
self.btnUp.clicked.connect(lambda: self.parent.map.moveRelative(0, 0, 1))
|
||||
self.btnDown.clicked.connect(lambda: self.parent.map.moveRelative(0, 0, -1))
|
||||
self.btnDown.clicked.connect(lambda: self.parent.map.moveRelative(0, 0, -1))
|
||||
self.parent.map.onMapData.sub(self.onMapData)
|
||||
self.parent.map.onPositionChange.sub(self.onPositionChange)
|
||||
self.tileShapeDropdown.currentTextChanged.connect(self.onTileShapeChanged)
|
||||
|
||||
def onMapData(self, data):
|
||||
pass
|
||||
|
||||
def onPositionChange(self, pos):
|
||||
tile = self.parent.map.getTileAtWorldPos(*self.parent.map.position)
|
||||
if tile is None:
|
||||
return
|
||||
|
||||
key = "TILE_SHAPE_NULL"
|
||||
for k, v in TILE_SHAPES.items():
|
||||
if v != tile.shape:
|
||||
continue
|
||||
key = k
|
||||
break
|
||||
self.tileShapeDropdown.setCurrentText(key)
|
||||
|
||||
def onTileShapeChanged(self, shape_key):
|
||||
tile = self.parent.map.getTileAtWorldPos(*self.parent.map.position)
|
||||
|
||||
if tile is None or shape_key not in TILE_SHAPES:
|
||||
return
|
||||
tile.setShape(TILE_SHAPES[shape_key])
|
||||
@@ -4,7 +4,7 @@ from PyQt5.QtWidgets import QFileDialog, QMessageBox
|
||||
from PyQt5.QtCore import QTimer
|
||||
import os
|
||||
from editortool.map.chunk import Chunk
|
||||
from editortool.map.mapdefs import MAP_WIDTH, MAP_HEIGHT, MAP_DEPTH
|
||||
from editortool.map.mapdefs import MAP_WIDTH, MAP_HEIGHT, MAP_DEPTH, CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH
|
||||
import traceback
|
||||
|
||||
MAP_DEFAULT_PATH = os.path.join(os.path.dirname(__file__), '../../../assets/map/')
|
||||
@@ -139,4 +139,25 @@ class Map:
|
||||
|
||||
def draw(self):
|
||||
for chunk in self.chunks.values():
|
||||
chunk.draw()
|
||||
chunk.draw()
|
||||
|
||||
def getChunkAtWorldPos(self, x, y, z):
|
||||
chunkX = x // CHUNK_WIDTH
|
||||
chunkY = y // CHUNK_HEIGHT
|
||||
chunkZ = z // CHUNK_DEPTH
|
||||
for chunk in self.chunks.values():
|
||||
if chunk.x == chunkX and chunk.y == chunkY and chunk.z == chunkZ:
|
||||
return chunk
|
||||
return None
|
||||
|
||||
def getTileAtWorldPos(self, x, y, z):
|
||||
chunk = self.getChunkAtWorldPos(x, y, z)
|
||||
if not chunk:
|
||||
print("No chunk found at position:", (x, y, z))
|
||||
return None
|
||||
|
||||
tileX = x % CHUNK_WIDTH
|
||||
tileY = y % CHUNK_HEIGHT
|
||||
tileZ = z % CHUNK_DEPTH
|
||||
tileIndex = tileX + tileY * CHUNK_WIDTH + tileZ * CHUNK_WIDTH * CHUNK_HEIGHT
|
||||
return chunk.tiles.get(tileIndex)
|
||||
@@ -19,6 +19,8 @@ MAP_HEIGHT = 5
|
||||
MAP_DEPTH = 3
|
||||
MAP_CHUNK_COUNT = MAP_WIDTH * MAP_HEIGHT * MAP_DEPTH
|
||||
|
||||
TILE_SHAPES = {}
|
||||
for key in defs.keys():
|
||||
if key.startswith('TILE_SHAPE_'):
|
||||
globals()[key] = int(defs.get(key))
|
||||
globals()[key] = int(defs.get(key))
|
||||
TILE_SHAPES[key] = int(defs.get(key))
|
||||
@@ -23,6 +23,11 @@ class Tile:
|
||||
def load(self, chunkData):
|
||||
self.shape = getItem(chunkData['shapes'], self.index, TILE_SHAPE_NULL)
|
||||
|
||||
def setShape(self, shape):
|
||||
self.shape = shape
|
||||
self.chunk.dirty = True
|
||||
self.chunk.tileUpdateVertices()
|
||||
|
||||
def getBaseTileModel(self):
|
||||
vertices = []
|
||||
indices = []
|
||||
|
||||
Reference in New Issue
Block a user