- refactor seed generation

- add grid (G key)
This commit is contained in:
kleph 2017-10-16 02:03:22 +02:00
parent 46bec1c5d9
commit 4c8380b36e

View file

@ -93,7 +93,12 @@ class Room:
tilemap[self.x + x][self.y + self.height - 1] = Level.TILE_WALL tilemap[self.x + x][self.y + self.height - 1] = Level.TILE_WALL
def center(self): def center(self):
return round(self.x + self.width/2), round(self.y + self.height/2) cx = 0
cy = 0
cx = round(self.x + self.width / 2)
cy = round(self.y + self.height / 2)
return cx, cy
class Level: class Level:
@ -102,7 +107,7 @@ class Level:
TILE_GROUND = 2 TILE_GROUND = 2
TILE_HALLWAY = 3 TILE_HALLWAY = 3
def __init__(self, sizex, sizey, tile_file=None): def __init__(self, sizex, sizey, seed, tile_file=None):
self.sizex, self.sizey = sizex, sizey self.sizex, self.sizey = sizex, sizey
self.tileset = [] self.tileset = []
self.tilemap = [] self.tilemap = []
@ -112,6 +117,7 @@ class Level:
self.tree = None self.tree = None
self.rooms = [] self.rooms = []
self.regenerate() self.regenerate()
self.seed = seed
def load_tileset(self, tile_file): def load_tileset(self, tile_file):
resource_file = open(tile_file) resource_file = open(tile_file)
@ -125,7 +131,7 @@ class Level:
def generate_tree(self): def generate_tree(self):
# init tree # init tree
tree = [Leaf(0, 0, 30, 30, 0)] tree = [Leaf(0, 0, self.sizex, self.sizey, 0)]
# split leaves until none succeed # split leaves until none succeed
# Lists are ordered. Tree will be created and travel from left to right # Lists are ordered. Tree will be created and travel from left to right
@ -179,6 +185,16 @@ class Level:
for y in range(0, self.sizey): for y in range(0, self.sizey):
for x in range(0, self.sizex): for x in range(0, self.sizex):
self.tileset[self.tilemap[x][y]].blit(x*TILE_SIZE_X, y*TILE_SIZE_Y) self.tileset[self.tilemap[x][y]].blit(x*TILE_SIZE_X, y*TILE_SIZE_Y)
if self.grid:
for x in range(0, self.sizex):
pyglet.graphics.draw(2, pyglet.gl.GL_LINES,
('v2i', (x*TILE_SIZE_X, 0, x*TILE_SIZE_X, self.sizey*TILE_SIZE_Y)),
('c3f', (0, 0, 1)*2 ) )
for y in range(0, self.sizey):
pyglet.graphics.draw(2, pyglet.gl.GL_LINES,
('v2i', (0, y * TILE_SIZE_Y, self.sizex * TILE_SIZE_X, y * TILE_SIZE_Y)),
('c3f', (0, 0, 1)*2 ) )
def dump_tilemap(self): def dump_tilemap(self):
for y in range(0, self.sizey): for y in range(0, self.sizey):
@ -204,13 +220,8 @@ class Level:
(xa, ya) = room_a.center() (xa, ya) = room_a.center()
(xb, yb) = room_b.center() (xb, yb) = room_b.center()
# horizontal part
if xa < xb:
start_x = xa start_x = xa
end_x = xb end_x = xb
else:
start_x = xb
end_x = xa
print("h_hw form " + str(start_x) + " and " + str(end_x)) print("h_hw form " + str(start_x) + " and " + str(end_x))
for x in range(start_x, end_x): for x in range(start_x, end_x):
self.tilemap[x][ya] = self.TILE_ROAD self.tilemap[x][ya] = self.TILE_ROAD
@ -245,10 +256,13 @@ def on_key_press(symbol, modifiers):
sys.exit(0) sys.exit(0)
if symbol == key.R: if symbol == key.R:
print('Regeneration') print('Regeneration')
level.seed = init_random()
level.regenerate() level.regenerate()
window.invalid = True window.invalid = True
if symbol == key.D: if symbol == key.D:
level.dump_tilemap() level.dump_tilemap()
if symbol == key.G:
level.grid ^= 1
elif symbol == key.LEFT: elif symbol == key.LEFT:
print('Left arrow') print('Left arrow')
elif symbol == key.ENTER: elif symbol == key.ENTER:
@ -262,18 +276,21 @@ def on_draw():
label.draw() label.draw()
# init random def init_random():
if len(sys.argv) > 1: if len(sys.argv) > 1:
seed = int(sys.argv[1]) seed = int(sys.argv[1])
else: else:
seed = random.randint(0, sys.maxsize) seed = random.randint(0, sys.maxsize)
print("Using seed: " + str(seed)) print("Using seed: " + str(seed))
random.seed(seed) random.seed(seed)
return seed
label_txt = 'Plop World seed: ' + str(seed)
genseed = init_random()
label_txt = 'Plop World seed: ' + str(genseed)
label = pyglet.text.Label(label_txt, x=window.width*5/6, y=window.height*5/6, anchor_x='center', anchor_y='center', label = pyglet.text.Label(label_txt, x=window.width*5/6, y=window.height*5/6, anchor_x='center', anchor_y='center',
multiline=True, width=window.width - window.width*5/6) multiline=True, width=window.width - window.width*5/6)
level = Level(30, 30, 'tiles.txt') level = Level(30, 30, genseed, 'tiles.txt')
pyglet.app.run() pyglet.app.run()