Return to repo list

tzed

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

commit e6d79416946466810cf9e486920f1d984760cbac
parent 930b7a09cdc529b1558001faa75369f8004224b3
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Fri, 11 Jun 2021 18:02:17 -0500

added drawing player

Diffstat:
Mdata/boards/test_town/entities.json | 16++++++++++++++++
Msrc/board.py | 2+-
Msrc/bus.py | 4+++-
Msrc/camera.py | 2--
Msrc/entity.py | 24+++++++++++++++++++++---
Msrc/game.py | 2++
Dsrc/player.py | 29-----------------------------
7 files changed, 43 insertions(+), 36 deletions(-)

diff --git a/data/boards/test_town/entities.json b/data/boards/test_town/entities.json @@ -0,0 +1,16 @@ +{ + "player": { + "type" : "playerspawn", + "location" : [0, 5], + "data" : {} + }, + "npc1" : { + "type": "npc", + "location" : [4, 4], + "data" : { + "name" : "Testicus", + "sheet" : "chars1", + "sprite" : [0, 1] + } + } +} diff --git a/src/board.py b/src/board.py @@ -62,7 +62,7 @@ class BoardManager(manager.Manager): for layer in self.current_board.tmx_data.visible_layers: if isinstance(layer, pytmx.TiledTileLayer): for x, y, gid in layer: - if pos[0] >= x * w and pos[0] < (x + 1) * w and pos[1] >= y * w and pos[1] < (y + 1) * w: + if pos[0] >= x * w * TILE_SCALE_FACTOR and pos[0] < (x + 1) * w * TILE_SCALE_FACTOR and pos[1] >= y * w * TILE_SCALE_FACTOR and pos[1] < (y + 1) * w * TILE_SCALE_FACTOR: return (x, y) return None diff --git a/src/bus.py b/src/bus.py @@ -178,4 +178,6 @@ class ManagerBus(Bus): # Performs: All callable non-return behaviors of managers. def perform_board_manager_get_tile_at_position(self, mousepos): - self.managers["board_manager"].get_tile_at_position(mousepos) + return self.managers["board_manager"].get_tile_at_position(mousepos) + def perform_board_manager_get_tile_at_tile_pos(self, tilepos): + return self.managers["board_manager"].get_tile_at_tile_pos(tilepos) diff --git a/src/camera.py b/src/camera.py @@ -80,8 +80,6 @@ class GameCamera(subsystem.GameSubsystem): Update and draw the camera contents. """ if surface != None: - # NOTE: This probably won't work. Think of collision scaling issues... - # On the other hand, it would be ideal for keeping all resolutions in line... surface.blit(pygame.transform.scale(self.camera_surface, (self.camera_surface_base_dimensions[0] * TILE_SCALE_FACTOR, self.camera_surface_base_dimensions[1] * TILE_SCALE_FACTOR)), self.camera_surface_offset) def update_fetch_data(self): diff --git a/src/entity.py b/src/entity.py @@ -39,13 +39,20 @@ class EntityManager(manager.Manager): Load entities up for the current board from the entities.json in that board's dir. """ - # First, clear the draw group + # First, clear the draw group and other setup self.draw_group.empty() + sh = self.system_bus.fetch("sheet_system", "sheets") # Next, get the JSON as a dict with open(os.path.join(BOARD_PATH, current_board_name, "entities.json")) as f: ent_def = json.load(f) # Then, load the entities + for e in ent_def: + if ent_def[e]["type"] == "playerspawn": + self.player = PlayerEntity(sh["chars1"], (0, 0)) + self.player.assign_tile(tuple(ent_def[e]["location"]), self.bus.perform_board_manager_get_tile_at_tile_pos(tuple(ent_def[e]["location"]))[2]) + self.player.snap_to_tile() + self.draw_group.add(self.player) def update_managed(self, surface = None): """ @@ -211,7 +218,7 @@ class Entity(pygame.sprite.DirtySprite): Snap the Entity to its current tile. """ if self.tile_pos != (-1, -1): - self.set_position((self.tile_pos[0] * TILE_WIDTH, self.tile_pos[1] * TILE_HEIGHT)) + self.set_position((self.tile_pos[0] * BASE_TILE_WIDTH, self.tile_pos[1] * BASE_TILE_HEIGHT)) def assign_tile(self, tile_pos, tile_gid): """ @@ -246,4 +253,15 @@ class Entity(pygame.sprite.DirtySprite): # Section 3 - Various Entity subclasses # ######################################### -# TODO: +class PlayerEntity(Entity): + """ + The Player object is the entity that is controlled + when in Overworld mode or Location mode. The Player + can move between tiles and certain commands are + affected by where the Player is located. + """ + + def __init__(self, sheet, sprite = (0, 0), animation = None, animated = False): + + # Parent initialization + super().__init__(sheet, sprite, animation, animated) diff --git a/src/game.py b/src/game.py @@ -70,6 +70,7 @@ class Game(object): # State-specific logic if new_mode == STATE_MODES.Location_Mode: self.board_manager.load_board("test_town") + self.entity_manager.load_board_entities_from_file("test_town") self.camera.load_camera_surface(self.board_manager.current_board.pixel_dimensions) def shift_frames(self, framerate = FRAMERATE): @@ -93,6 +94,7 @@ class Game(object): # Manager updates if self.state_mode in (STATE_MODES.Overworld_Mode, STATE_MODES.Location_Mode): self.board_manager.update_managed(self.camera.camera_surface) + self.entity_manager.update_managed(self.camera.camera_surface) # Draw the camera self.camera.update_camera(self.screen) diff --git a/src/player.py b/src/player.py @@ -1,29 +0,0 @@ -import pygame -from . import entity -from .envvars import * - -############# -# player.py # -############# - -# This file contains: -# 1. The Player class, which represents the character that the player controls in the overworld and in locations - -############################ -# Section 1 - Player class # -############################ - -class Player(entity.Entity): - """ - The Player object is the entity that is controlled - when in Overworld mode or Location mode. The Player - can move between tiles and certain commands are - affected by where the Player is located. - """ - - def __init__(self, sheet, sprite = (0, 0), animation = None, animated = False): - - # Parent initialization - super().__init__(sheet, sprite, animation, animated) - - # Important values