Return to repo list

tzed

Simple story-driven open world 2D CRPG.
Return to HMagellan.com

commit cb3bda8f9e7ae5dce0339ae649421d06ba919300
parent 3e4a6f09dc9a7d3e0a9a5b4053a3ae6cf21776f5
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Mon, 14 Jun 2021 22:35:21 -0500

overlays and the overworld

Diffstat:
Adata/boards/test_ow/entities.json | 2++
Adata/boards/test_ow/overlay.json | 2++
Mdata/boards/test_town/entities.json | 2+-
Adata/boards/test_town/location.json | 3+++
Msrc/board.py | 9++++++++-
Msrc/entity.py | 47+++++++++++++++++++++++++++++++++++++++++++++--
Msrc/envvars.py | 1+
Msrc/game.py | 11+++++++++++
8 files changed, 73 insertions(+), 4 deletions(-)

diff --git a/data/boards/test_ow/entities.json b/data/boards/test_ow/entities.json @@ -0,0 +1,2 @@ +{ +} diff --git a/data/boards/test_ow/overlay.json b/data/boards/test_ow/overlay.json @@ -0,0 +1,2 @@ +{ +} diff --git a/data/boards/test_town/entities.json b/data/boards/test_town/entities.json @@ -1,7 +1,7 @@ { "player": { "type" : "playerspawn", - "location" : [0, 5], + "location" : [4, 5], "data" : {} }, "npc1" : { diff --git a/data/boards/test_town/location.json b/data/boards/test_town/location.json @@ -0,0 +1,3 @@ +{ + "overworld_tile_pos" : [10, 10] +} diff --git a/src/board.py b/src/board.py @@ -1,4 +1,4 @@ -import pygame, pytmx, os +import pygame, pytmx, os, json from . import manager, entity from .envvars import * @@ -152,6 +152,13 @@ class Board(object): self.board_dimensions = (self.tmx_data.width, self.tmx_data.height) self.pixel_dimensions = (self.tmx_data.width * BASE_TILE_WIDTH, self.tmx_data.height * BASE_TILE_HEIGHT) + # Optional values + self.overworld_tile_pos = None + if os.path.exists(os.path.join(BOARD_PATH, self.boardname, "location.json")): + with open(os.path.join(BOARD_PATH, self.boardname, "location.json")) as f: locdat = json.load(f) + if "overworld_tile_pos" in locdat.keys(): + self.overworld_tile_pos = tuple(locdat["overworld_tile_pos"]) + def draw_board(self, surface = None): """ Draw the tiles of the board onto the provided PyGame diff --git a/src/entity.py b/src/entity.py @@ -34,6 +34,7 @@ class EntityManager(manager.Manager): # Other values self.player = None self.board_overlay = None + self.previous_overworld_tile_pos = None def load_board_entities_from_file(self, current_board_name): """ @@ -57,6 +58,25 @@ class EntityManager(manager.Manager): self.system_bus.perform_camera_follow_entity(self.player, False) self.draw_group.add(self.player) + def place_player_on_overworld(self, tile_pos = None): + """ + Place the player on a given tile_pos in the + overworld, or in the last location if none is + passed. Works only in Overworld mode. + """ + sh = self.system_bus.fetch("sheet_system", "sheets") + if self.game.state_mode == STATE_MODES.Overworld_Mode: + self.player = PlayerEntity(sh["chars1"], (0, 0)) + self.draw_group.add(self.player) + if tile_pos != None: + self.player.assign_tile(tile_pos, self.bus.perform_board_manager_get_tile_at_tile_pos(tile_pos)[2]) + self.player.snap_to_tile() + self.system_bus.perform_camera_follow_entity(self.player, False) + elif self.previous_overworld_tile_pos != None: + self.player.assign_tile(self.previous_overworld_tile_pos, self.bus.perform_board_manager_get_tile_at_tile_pos(self.previous_overworld_tile_pos)[2]) + self.player.snap_to_tile() + self.system_bus.perform_camera_follow_entity(self.player, False) + def load_board_overlay_from_file(self, current_board_name): """ Load an overlay from file. @@ -73,11 +93,21 @@ class EntityManager(manager.Manager): for t in overlay_data[oc]: for o in overlay_data[oc][t]: for oo in overlay_data[oc][t][o]: - noe = OverlayEntity(sh["overlay"], d[o], None, False, "exit_" + o) + noe = OverlayEntity(sh["overlay"], d[o], None, False, "exit_" + o, destination = t) noe.assign_tile(tuple(oo), self.bus.perform_board_manager_get_tile_at_tile_pos(tuple(oo))) noe.snap_to_tile() self.board_overlay.add(noe) + def trigger_overlay_entity(self, entity): + """ + Trigger the effect of a given overlay entity. + """ + # NOTE: The 'exit_*' types only occur in locations and lead to the overworld. + # The 'traverse_*' types occur in locations and lead to locations (data passed in kwarg expansion) + cb = self.bus.fetch("board_manager", "current_board") + if entity.overlay_type == "exit_north": + self.game.switch_mode(STATE_MODES.Overworld_Mode, {"overworld_tile_pos" : cb.overworld_tile_pos, "direction" : (0, -1)}) + def move_player_by_tile_offset(self, tile_offset): """ Move a player entity by relative tile offset. @@ -94,6 +124,12 @@ class EntityManager(manager.Manager): Update currently managed entities. """ if surface != None: + # Check collisions + for o in self.board_overlay: + if self.player.tile_pos == o.tile_pos: + self.trigger_overlay_entity(o) + + # Drawing self.draw_group.update(surface) self.board_overlay.update(surface) @@ -309,9 +345,16 @@ class OverlayEntity(Entity): and other features. """ - def __init__(self, sheet, sprite = (0, 0), animation = None, animated = False, ov_type = None): + def __init__(self, sheet, sprite = (0, 0), animation = None, animated = False, ov_type = None, **kwargs): # Parent initialization super().__init__(sheet, sprite, animation, animated) + # Overlay values/defaults self.overlay_type = ov_type + self.destination = None + + # kwarg expansion + if "destination" in kwargs: + self.destination = kwargs["destination"] + diff --git a/src/envvars.py b/src/envvars.py @@ -17,6 +17,7 @@ import os, enum, json, pathlib SCREEN_WIDTH = 1024 SCREEN_HEIGHT = 768 FRAMERATE = 60 +OVERWORLD_BOARD = "test_ow" # Tile settings BASE_TILE_WIDTH = 16 diff --git a/src/game.py b/src/game.py @@ -73,6 +73,17 @@ class Game(object): self.camera.load_camera_surface(VIEWPORT_SIZE, True) self.entity_manager.load_board_entities_from_file(data["boardname"]) self.entity_manager.load_board_overlay_from_file(data["boardname"]) + elif new_mode == STATE_MODES.Overworld_Mode: + self.board_manager.load_board(OVERWORLD_BOARD) + self.camera.load_camera_surface(VIEWPORT_SIZE, True) + self.entity_manager.load_board_entities_from_file(OVERWORLD_BOARD) + if "overworld_tile_pos" in data.keys(): + self.entity_manager.place_player_on_overworld(data["overworld_tile_pos"]) + else: + self.entity_manager.place_player_on_overworld() + if "direction" in data.keys(): + self.entity_manager.move_player_by_tile_offset(data["direction"]) + self.entity_manager.load_board_overlay_from_file(OVERWORLD_BOARD) def shift_frames(self, framerate = FRAMERATE): """