Return to repo list

heart-of-gold

Tactical RPG written in python, using pygame.
Return to HMagellan.com

commit 627504e726f678a4e7ec3631d93c53f431cd8403
parent 1ba503cd4550b0515aa9bf16360d21510dcad040
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sat, 12 Sep 2020 14:50:18 -0500

Entity manager working, as well as animated entities

Diffstat:
Adata/json/ents/testmap1.json | 11+++++++++++
Msrc/constants.py | 3++-
Msrc/game.py | 3+++
Msrc/images.py | 1+
Msrc/vgo.py | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
5 files changed, 71 insertions(+), 13 deletions(-)

diff --git a/data/json/ents/testmap1.json b/data/json/ents/testmap1.json @@ -0,0 +1,11 @@ +{ + "testent1" : { + "sheet" : "jisella_1", + "position" : [0, 0], + "visible" : true, + "animation" : "stand_L", + "animated" : true, + "passable" : false, + "tile" : [3, 7] + } +} diff --git a/src/constants.py b/src/constants.py @@ -28,5 +28,6 @@ DATA_PATH = os.path.join(os.getcwd(), "data") IMAGE_PATH = os.path.join(DATA_PATH, "img") SOUND_PATH = os.path.join(DATA_PATH, "snd") FONT_PATH = os.path.join(DATA_PATH, "font") -JSON_PATH = os.path.join(DATA_PATH, "json") BOARD_PATH = os.path.join(DATA_PATH, "map") +JSON_PATH = os.path.join(DATA_PATH, "json") +ENTITY_JSON_PATH = os.path.join(JSON_PATH, "ents") diff --git a/src/game.py b/src/game.py @@ -41,6 +41,7 @@ class Game(object): # Managers self.sheet_manager = images.SheetManager(self) self.board_manager = board.BoardManager(self) + self.entity_manager = vgo.EntityManager(self) # Setup (This is WIP) # TODO: Fix this up, will see lots of revision @@ -48,6 +49,7 @@ class Game(object): self.sheet_manager.load_animations("anims.json") self.board_manager.load_board_from_file("testmap1.tmx") self.board_manager.switch_to_board("testmap1.tmx") + self.entity_manager.load_entities_from_json("testmap1.json") def shift_frames(self): """ @@ -67,6 +69,7 @@ class Game(object): # Next, update all the subsurfaces/game objects and draw them # TODO: This is WIP and will change self.board_manager.update_board(self.screen) + self.entity_manager.update_entities(self.screen) # Last, update the screen pygame.display.update() diff --git a/src/images.py b/src/images.py @@ -91,6 +91,7 @@ class Sheet(object): self.filename = filename self.sprite_dimensions = sprite_size self.total_sprites = total_sprites + self.sprites = {} # Try and load all sprites on the sheet, or log if we failed if not self.load_sheet(filename, sprite_size, total_sprites): diff --git a/src/vgo.py b/src/vgo.py @@ -1,4 +1,6 @@ -import pygame +import pygame, json, os +from . import manager +from .constants import ENTITY_JSON_PATH ########## # vgo.py # @@ -9,6 +11,7 @@ import pygame # 2. The Entity class that defines the objects that represent non-tile, non-UI-element game objects # 3. Various kinds of specialized entity classes # 4. The Tile class that defines the objects that make up the game board +# 5. Managers for VGO objects # TODO: This should eventually use LayeredDirty sprites for performance reasons @@ -23,10 +26,10 @@ class VisibleGameObject(pygame.sprite.Sprite): object. """ - def __init__(self, sheet, position, visible = True): + def __init__(self, sheet, position, visible = True, animation = None, animated = False): # Parent initialization - super().__init__(self) + super().__init__() # Saved values self.sheet = sheet @@ -40,6 +43,8 @@ class VisibleGameObject(pygame.sprite.Sprite): self.animation = {} self.animation_timer = 0 self.animation_frame = 0 + if animated: + self.set_animation(animation, animated) def set_animation(self, new_animation, play = False, init_frame = 0): """ @@ -49,8 +54,8 @@ class VisibleGameObject(pygame.sprite.Sprite): self.animation = new_animation self.animation_frame = init_frame self.animation_timer = new_animation[init_frame]["timer"] - self.image = self.sheet.sprites[new_anim[init_frame]["sprite"]] - self.play_animation = play + self.image = self.sheet.sprites[new_animation[init_frame]["sprite"]] + self.animated = play def animate(self): """ @@ -63,9 +68,9 @@ class VisibleGameObject(pygame.sprite.Sprite): if self.animation_frame < len(self.animation) - 1: self.animation_frame += 1 else: - self.animation_framer = 0 - self.animation_timer = self.animation[self.animation_framer]["timer"] - self.image = self.sheet.sprites[self.animation[self.animation_framer]["sprite"]] + self.animation_frame = 0 + self.animation_timer = self.animation[self.animation_frame]["timer"] + self.image = self.sheet.sprites[self.animation[self.animation_frame]["sprite"]] def act(self): """ @@ -83,7 +88,7 @@ class VisibleGameObject(pygame.sprite.Sprite): """ self.act() if surface != None: - if self.play_animation: + if self.animated: self.animate() surface.blit(self.image, self.rect) @@ -98,10 +103,10 @@ class Entity(VisibleGameObject): decorations/obstacles are entities. """ - def __init__(self, sheet, position, visible = True, passable = False, tile = None): + def __init__(self, sheet, position, visible = True, animation = None, animated = False, passable = False, tile = None): # Parent initialization - super().__init__(self, sheet, position, visible) + super().__init__(sheet, position, visible, animation, animated) # Saved values self.passable = passable @@ -128,7 +133,7 @@ class Tile(VisibleGameObject): def __init__(self, sheet, position, visible = True, board = None, gridpos = (0, 0), passable = True): # Parent initialization - super().__init__(self, sheet, position, visible) + super().__init__(sheet, position, visible) # Saved values self.board = board @@ -142,3 +147,40 @@ class Tile(VisibleGameObject): "top": None, "bottom": None } + +############################################## +# Section 5 - Managers for VGOs & Subclasses # +############################################## + +class EntityManager(manager.Manager): + """ + Manager for all entity objects. Handles creating them, placing + them on the screen, managing their updates, disposing of them + when need be, and transferring info to them for e.g. moving. + """ + + def __init__(self, game): + + super().__init__(game) + + # Entity values + self.loaded_entities = pygame.sprite.Group() + + def load_entities_from_json(self, entsjson): + """ + Load one or more entities from a JSON file. + """ + j = json.load(open(os.path.join(ENTITY_JSON_PATH, entsjson))) + + for e in j: + self.loaded_entities.add(Entity(self.game.sheet_manager.loaded_sheets[j[e]["sheet"]], + tuple(j[e]["position"]), j[e]["visible"], + self.game.sheet_manager.animations[j[e]["sheet"]][j[e]["animation"]], + j[e]["animated"], j[e]["passable"], j[e]["tile"])) + + def update_entities(self, surface = None): + """ + Update all loaded entities. + """ + if surface != None: + self.loaded_entities.update(surface)