From f3c9e221022cc97271f5d7fcc088365301dd7e89 Mon Sep 17 00:00:00 2001 From: Cameron Smart Date: Sun, 19 Apr 2020 15:59:52 -0700 Subject: [PATCH] Levels can be combined together in dungeon --- triple-dungeon/config.py | 6 +- triple-dungeon/main.py | 8 +- triple-dungeon/map.py | 108 +++++++++++++----- triple-dungeon/resources/levels/center1.json | 2 +- .../resources/levels/map1/center.json | 18 +++ .../resources/levels/map1/room.json | 18 +++ .../resources/levels/map1/room1.json | 19 +++ 7 files changed, 144 insertions(+), 35 deletions(-) create mode 100644 triple-dungeon/resources/levels/map1/center.json create mode 100644 triple-dungeon/resources/levels/map1/room.json create mode 100644 triple-dungeon/resources/levels/map1/room1.json diff --git a/triple-dungeon/config.py b/triple-dungeon/config.py index 5366d99..313b102 100644 --- a/triple-dungeon/config.py +++ b/triple-dungeon/config.py @@ -11,8 +11,8 @@ class Config(object): """ # Constants - SCREEN_WIDTH = 1000 - SCREEN_HEIGHT = 650 + SCREEN_WIDTH = 1650 + SCREEN_HEIGHT = 1000 SCREEN_TITLE = "Triple Dungeon" TILE_WIDTH = 63 @@ -21,7 +21,7 @@ class Config(object): TILE_SCALING = 2 # Movement speed of player, in pixels per frame - PLAYER_MOVEMENT_SPEED = 5 + PLAYER_MOVEMENT_SPEED = 7 # How many pixels to keep as a minimum margin between the character # and the edge of the screen. diff --git a/triple-dungeon/main.py b/triple-dungeon/main.py index 635f3b7..ff0841c 100644 --- a/triple-dungeon/main.py +++ b/triple-dungeon/main.py @@ -7,7 +7,7 @@ Holds the main game window, as well as manages basic functions for organizing th import arcade from config import Config -from map import Level +from map import Dungeon from mobs import Player from mobs import Enemy @@ -54,8 +54,10 @@ class Game(arcade.Window): # Set up the player, specifically placing it at these coordinates. Player.setup(self) - # Create the level - self.floor_list, self.wall_list = Level.load_file('resources/levels/test1.json') + # Create the dungeon + dungeon = Dungeon() + self.floor_list, self.wall_list = dungeon.get_lists() + # Create monsters self.enemy_list.append(Enemy("resources/images/monsters/ghost/ghost1.png", 200, 200).get_enemy()) diff --git a/triple-dungeon/map.py b/triple-dungeon/map.py index ab6399f..c61304a 100644 --- a/triple-dungeon/map.py +++ b/triple-dungeon/map.py @@ -9,6 +9,7 @@ from config import Config import arcade import json +import numpy as np class Dungeon(object): """ @@ -22,9 +23,52 @@ class Dungeon(object): :param level_count: The number of Active Levels that should be stored within the Dungeon. :param size: The diameter of the dungeon. Allows for a total of size^2 slots for levels. """ + # setup + self.floor_list = arcade.SpriteList() + self.wall_list = arcade.SpriteList() + level_size = 10 * Config.TILE_SCALING * Config.TILE_WIDTH - self.level_count, self.size = level_count, size - self.levels = [[None for y in range(size)] for x in range(size)] # array[x][y] style access + # get center level + + center = Level() + center.load_file('resources/levels/map1/center.json') + center.render() + center_floor, center_wall = center.get_lists() + self.floor_list.extend(center_floor) + self.wall_list.extend(center_wall) + + + # get a side room + room = Level() + room.load_file('resources/levels/map1/room.json') + room.rotate_level(2) + room.render() + room_floor, room_wall = room.get_lists() + room_floor.move(level_size, 0) + room_wall.move(level_size, 0) + self.floor_list.extend(room_floor) + self.wall_list.extend(room_wall) + + # get a side room + room = Level() + room.load_file('resources/levels/map1/room.json') + room.render() + room_floor, room_wall = room.get_lists() + room_floor.move(-level_size, 0) + room_wall.move(-level_size, 0) + self.floor_list.extend(room_floor) + self.wall_list.extend(room_wall) + + + #self.level_count, self.size = level_count, size + #self.levels = [[None for y in range(size)] for x in range(size)] # array[x][y] style access + + def get_lists(self): + return (self.floor_list, self.wall_list) + + def add_level(self, sprit_list): + for x in sprit_list: + self.levels.append(x) def render(self) -> None: """ @@ -37,14 +81,14 @@ class Dungeon(object): level.render() -class Level(object): +class Level: """ A 10x10 space holding wall and background sprites, enemies, items and so forth. Should be loaded from """ - def __init__(self, level_x: int, level_y: int) -> None: + def __init__(self, level_x: int = 0, level_y: int = 0) -> None: """ Initializes the level class. Defaults with no sprites, and no background. @@ -53,56 +97,64 @@ class Level(object): """ self.x, self.y = level_x, level_y - self.sprites = arcade.SpriteList() + self.sprites = [] + self.level = [] - # Tuples containing the Node positions of where walls, air and entrances are. + # Tuples containing the Node positions of where walls, floor and entrances are. # All positions are generated based on the level's X and Y position, so that all points within # the dungeon can be mapped by a proper pathfinding system. - self.walls = [] - self.air = [] - self.entrances = [] + self.floor_list = [] + self.wall_list = [] + #self.entrances = [] - @staticmethod - def load_file(path: str) -> Level: + #@staticmethod + def load_file(self, path: str) -> Level: """ Builds a Level from a given file path. :param path: Path to the Level file. :return: The new generated Level file. """ - floor_list = arcade.SpriteList() - wall_list = arcade.SpriteList() - x = 0 - y = 0 + self.floor_list = arcade.SpriteList() + self.wall_list = arcade.SpriteList() + with open(path) as file: level = json.load(file) - elements = level['elements'] - structure = level['structure'] - + self.sprites = level['elements'] + self.level = level['structure'] + + def render(self) -> None: + """ + Calls render on all sprites. + """ + x = 0 + y = 0 level_size = 10 * Config.TILE_SCALING * Config.TILE_WIDTH # Create the level # This shows using a loop to place multiple sprites horizontally and vertically for y_pos in range(0, level_size , 63 * Config.TILE_SCALING): for x_pos in range(0, level_size, 63 * Config.TILE_SCALING): - cur_tile = structure[y][x] - sprite = elements[cur_tile] + cur_tile = self.level[y][x] + sprite = self.sprites[cur_tile] floor = arcade.Sprite(sprite, Config.TILE_SCALING) floor.center_x = x_pos floor.center_y = y_pos if(cur_tile == ' '): - floor_list.append(floor) + self.floor_list.append(floor) elif(cur_tile == 'w'): - wall_list.append(floor) + self.wall_list.append(floor) x += 1 x = 0 y += 1 - return (floor_list, wall_list) + def get_lists(self): + return (self.floor_list, self.wall_list) - def render(self) -> None: - """ - Calls render on all sprites. - """ - pass \ No newline at end of file + def rotate_level(self, times_rotated): + m = np.array(self.level) + print(m) + for i in range(0, times_rotated): + m = np.rot90(m) + self.level = m.tolist() \ No newline at end of file diff --git a/triple-dungeon/resources/levels/center1.json b/triple-dungeon/resources/levels/center1.json index ab537a5..0bcd270 100644 --- a/triple-dungeon/resources/levels/center1.json +++ b/triple-dungeon/resources/levels/center1.json @@ -2,7 +2,7 @@ "elements" : { "w" : "resources/images/tiles/wall_tile.png", " " : "resources/images/tiles/floor_tile.png", - "e" : "" + "e" : "resources/images/tiles/floor_tile.png" }, "structure" : [ ["w", "w", "w", "w", "e", "e", "w", "w", "w", "w"], diff --git a/triple-dungeon/resources/levels/map1/center.json b/triple-dungeon/resources/levels/map1/center.json new file mode 100644 index 0000000..751a3f4 --- /dev/null +++ b/triple-dungeon/resources/levels/map1/center.json @@ -0,0 +1,18 @@ +{ + "elements" : { + "w" : "resources/images/tiles/wall_tile.png", + " " : "resources/images/tiles/floor_tile.png" + }, + "structure" : [ + ["w", "w", "w", "w", " ", " ", "w", "w", "w", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "], + [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", "w", "w", "w", " ", " ", "w", "w", "w", "w"] + ] +} \ No newline at end of file diff --git a/triple-dungeon/resources/levels/map1/room.json b/triple-dungeon/resources/levels/map1/room.json new file mode 100644 index 0000000..b070da4 --- /dev/null +++ b/triple-dungeon/resources/levels/map1/room.json @@ -0,0 +1,18 @@ +{ + "elements" : { + "w" : "resources/images/tiles/wall_tile.png", + " " : "resources/images/tiles/floor_tile.png" + }, + "structure" : [ + ["w", "w", "w", "w", "w", "w", "w", "w", "w", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", "w", "w", " ", " ", " ", " "], + ["w", " ", " ", " ", "w", "w", " ", " ", " ", " "], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", "w", "w", "w", "w", "w", "w", "w", "w", "w"] + ] +} \ No newline at end of file diff --git a/triple-dungeon/resources/levels/map1/room1.json b/triple-dungeon/resources/levels/map1/room1.json new file mode 100644 index 0000000..41559cc --- /dev/null +++ b/triple-dungeon/resources/levels/map1/room1.json @@ -0,0 +1,19 @@ +{ + "elements" : { + "w" : "resources/images/tiles/wall_tile.png", + " " : "resources/images/tiles/floor_tile.png", + "e" : "resources/images/tiles/floor_tile.png" + }, + "structure" : [ + ["w", "w", "w", "w", "w", "w", "w", "w", "w", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["e", " ", " ", " ", "w", "w", " ", " ", " ", "w"], + ["e", " ", " ", " ", "w", "w", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", " ", " ", " ", " ", " ", " ", " ", " ", "w"], + ["w", "w", "w", "w", "w", "w", "w", "w", "w", "w"] + ] +} \ No newline at end of file