This commit is contained in:
2021-05-26 16:36:59 -04:00
parent 3fff7ef498
commit d05eec6c03
5 changed files with 98 additions and 96 deletions

View File

@@ -1,90 +1,18 @@
from map_objects import *
from random import randint
import numpy as np
from tcod.console import Console
import tile_types
class GameMap:
def __init__(self, width, height):
self.width = width
self.height = height
self.tiles = self.initialize_tiles()
def __init__(self, width: int, height: int):
self.width, self.height = width, height
self.tiles = np.full((width,height), fill_value=tile_types.floor, order="F")
def initialize_tiles(self):
tiles = [[Tile(True) for y in range(self.height)] for x in range(self.width)]
self.tiles[30:33, 22] = tile_types.wall
return tiles
def in_bounds(self, x: int, y: int) -> bool:
"""Return True if x and y are inside the bounds of this map"""
return 0 <= x < self.width and 0 <= y < self.height
def is_blocked(self, x, y):
if self.tiles[x][y].blocked:
return True
return False
def create_room(self, room):
# go through the tiles in the rectangle and make them passable
for x in range(room.x1 + 1, room.x2):
for y in range(room.y1 +1, room.y2):
self.tiles[x][y].blocked = False
self.tiles[x][y].block_sight = False
def create_h_tunnel(self, x1, x2, y):
for x in range(min(x1, x2), max(x1, x2) + 1):
self.tiles[x][y].blocked = False
self.tiles[x][y].block_sight = False
def create_v_tunnel(self, y1, y2, x):
for y in range(min(y1, y2), max(y1, y2) + 1):
self.tiles[x][y].blocked = False
self.tiles[x][y].block_sight = False
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player):
rooms = []
num_rooms = 0
for r in range(max_rooms):
# print(rooms)
# print(num_rooms)
# random width and height
w = randint(room_min_size, room_max_size)
h = randint(room_min_size, room_max_size)
# random position without going out of bounds
x = randint(0, map_width - w - 1)
y = randint(0, map_height - h - 1)
#call Rect Class
new_room = Rect(x, y, w, h)
# if rooms == []:
# print('first')
# rooms.append(new_room)
#run through other rooms to check for overlap
for other_room in rooms:
if new_room.intersect(other_room):
break
else:
# no overlap, room is valid
# paint to map's tiles
self.create_room(new_room)
#center coords of new room
(new_x, new_y) = new_room.center()
if num_rooms == 0:
#first room where player starts
player.x = new_x
player.y = new_y
else:
# all rooms after the first are connected
# center coords of previous room
(prev_x, prev_y) = rooms[num_rooms - 1].center()
if randint(0, 1) == 1:
#first horizontally then vertically
self.create_h_tunnel(prev_x, new_x, prev_y)
self.create_v_tunnel(prev_y, new_y, new_x)
else:
#first vertically then horizontally
self.create_v_tunnel(prev_y, new_y, prev_x)
self.create_h_tunnel(prev_x, new_x, new_y)
rooms.append(new_room)
num_rooms += 1
def render(self, console: Console) -> None:
console.tiles_rgb[0:self.width, 0:self.height] = self.tiles["dark"]