Browse Source

Map editor gui cleaned up. Save & load maps.

master
rf 1 year ago
parent
commit
752e5cd78d
4 changed files with 47 additions and 5 deletions
  1. +5
    -0
      edit_tiles.json
  2. +14
    -4
      editor.py
  3. +5
    -0
      map0.json
  4. +23
    -1
      util.py

+ 5
- 0
edit_tiles.json View File

@@ -0,0 +1,5 @@
[
"0010700o00r00",
"fe53npikml42j",
"a9bd68suhgqct"
]

+ 14
- 4
editor.py View File

@@ -1,6 +1,7 @@
from util import *

current_tile = None
mapfile = "map0.json"

WIDTH = 800
HEIGHT = 800
@@ -33,15 +34,18 @@ arrow_gui = Map.from_str("""\
.b.""", arrows)
arrow_gui.pos = (5, 25 + tileindex.tileh * tileindex.wrap)

map = Map([list("0"*30)], tilemap)
map.pos = (200, 200)
tile_gui = Map.load("edit_tiles.json", tilemap)
tile_gui.pos = (5, 25)

map = Map.load(mapfile, tilemap)
map.pos = (30 + arrow_gui.tilew * arrow_gui.w, 50 + tileindex.tileh * tileindex.wrap)

mousepos = Pos()
mousetup = mousepos.tuple()

def draw():
screen.clear()
tileindex.draw(screen)
tile_gui.draw(screen)
arrow_gui.draw(screen)
if current_tile in tilemap.tiles:
tilemap.draw(screen, arrow_gui.pos.x + arrow_gui.tilew, arrow_gui.pos.y + arrow_gui.tileh, current_tile)
@@ -70,8 +74,14 @@ def on_mouse_down(pos, button):
if tile != None:
current_tile = tile

def on_mouse_move(pos):
def on_mouse_move(pos, buttons):
global mousetup
global mousepos
mousetup = pos
mousepos = Pos(*pos)
if mouse.LEFT in buttons:
on_mouse_down(pos, mouse.LEFT)

def on_key_down(key):
if key == keys.S:
map.save(mapfile)

+ 5
- 0
map0.json View File

@@ -0,0 +1,5 @@
[
"000000000000",
"000000000000",
"000000000000"
]

+ 23
- 1
util.py View File

@@ -1,4 +1,5 @@
from pygame import Rect
import json

class Size:
def __init__(self, w=0, h=0):
@@ -123,7 +124,15 @@ class TileIndex:
class Map:

def __init__(self, arr, tilemap, pos=(0, 0)):
self.map = arr
self.map = []
if len(arr) == 0:
self.map.append([])
else:
if isinstance(arr[0], str):
for line in arr:
self.map.append(list(line))
else:
self.map = arr
self.tilemap = tilemap
self.h = len(arr)
self.w = len(arr[0])
@@ -151,6 +160,19 @@ class Map:
a.append(list(line))
return Map(a, tilemap)

def load(filename, tilemap):
map = None
with open(filename, "r") as fd:
map = Map(json.load(fd), tilemap)
return map

def save(self, filename):
arr = []
for line in self.map:
arr.append("".join(line))
with open(filename, "w") as fd:
json.dump(arr, fd, indent=4)

def draw(self, screen):
for y in range(self.h):
for x in range(self.w):


Loading…
Cancel
Save