Return to repo list

heart-of-gold

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

commit 1ba503cd4550b0515aa9bf16360d21510dcad040
parent f12233fd6de3394a046c34c09c5bb337b73c8ed3
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Fri, 11 Sep 2020 01:32:34 -0500

SheetManager now implemented and loads fine, plus new json defs

Diffstat:
Adata/json/anims.json | 14++++++++++++++
Adata/json/sheets.json | 7+++++++
Mdata/map/testmap1.tmx | 10+++++-----
Msrc/board.py | 2++
Msrc/constants.py | 1+
Msrc/game.py | 6+++++-
Msrc/images.py | 65++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
7 files changed, 94 insertions(+), 11 deletions(-)

diff --git a/data/json/anims.json b/data/json/anims.json @@ -0,0 +1,14 @@ +{ + "jisella_1" : { + "stand_L" : [ + { "sprite" : [0, 0], "timer" : 4 }, + { "sprite" : [1, 0], "timer" : 4 }, + { "sprite" : [2, 0], "timer" : 4 }, + { "sprite" : [3, 0], "timer" : 4 }, + { "sprite" : [0, 1], "timer" : 4 }, + { "sprite" : [1, 1], "timer" : 4 }, + { "sprite" : [2, 1], "timer" : 4 }, + { "sprite" : [3, 1], "timer" : 4 } + ] + } +} diff --git a/data/json/sheets.json b/data/json/sheets.json @@ -0,0 +1,7 @@ +{ + "jisella_1" : { + "filename" : "jisella_1.png", + "dimensions" : [64, 64], + "total_sprites" : 72 + } +} diff --git a/data/map/testmap1.tmx b/data/map/testmap1.tmx @@ -14,11 +14,11 @@ 1,1,2,1,1,1,3,3,4,4,4,4,4,3,3,1, 1,2,2,1,1,1,1,3,3,3,3,3,3,3,1,1, 2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1, -2,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1, -1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1, -1,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1, -1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,1, -1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1 +2,1,1,2,2,1,1,1,1,1,1,1,1,3,3,3, +1,1,1,1,2,2,1,1,1,1,1,1,3,3,4,4, +1,1,1,1,1,2,2,1,1,1,1,3,3,4,4,4, +1,1,1,1,1,1,2,2,1,1,3,3,4,4,4,4, +1,1,1,1,1,1,1,2,1,1,3,4,4,4,4,4 </data> </layer> </map> diff --git a/src/board.py b/src/board.py @@ -14,6 +14,8 @@ from .constants import BOARD_PATH # Section 1 - BoardManager Object # ################################### +# TODO: Should BoardManager maintain a surface/rect to draw the Board onto, then +# pass that to Game? class BoardManager(manager.Manager): """ BoardManager handles loading and managing Board objects. It diff --git a/src/constants.py b/src/constants.py @@ -28,4 +28,5 @@ 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") diff --git a/src/game.py b/src/game.py @@ -1,5 +1,5 @@ import pygame -from . import board, vgo +from . import images, board, vgo from .constants import SCREEN_WIDTH, SCREEN_HEIGHT, FRAMERATE ########### @@ -39,10 +39,13 @@ class Game(object): self.interface = GameInterface(self) # Managers + self.sheet_manager = images.SheetManager(self) self.board_manager = board.BoardManager(self) # Setup (This is WIP) # TODO: Fix this up, will see lots of revision + self.sheet_manager.load_sheets("sheets.json") + self.sheet_manager.load_animations("anims.json") self.board_manager.load_board_from_file("testmap1.tmx") self.board_manager.switch_to_board("testmap1.tmx") @@ -62,6 +65,7 @@ class Game(object): self.screen.fill((0, 0, 0)) # Next, update all the subsurfaces/game objects and draw them + # TODO: This is WIP and will change self.board_manager.update_board(self.screen) # Last, update the screen diff --git a/src/images.py b/src/images.py @@ -1,5 +1,6 @@ -import pygame, os -from .constants import IMAGE_PATH, COLORKEY +import pygame, os, json +from . import manager +from .constants import IMAGE_PATH, JSON_PATH, COLORKEY ############# # images.py # @@ -7,7 +8,8 @@ from .constants import IMAGE_PATH, COLORKEY # This file contains: # 1. Important environment values and functions for loading images -# 2. The 'Sheet' class, which is used to represent enumerated spritesheets +# 2. The 'SheetManager' class, which is a Manager object that handles sheets (e.g. loading) +# 3. The 'Sheet' class, which is used to represent enumerated spritesheets ##################################################### # Section 1 - Environment Values and Image Loading # @@ -15,12 +17,65 @@ from .constants import IMAGE_PATH, COLORKEY def load_image(filename): """ - Load an image file as a PyGame image. + Load an image file as a PyGame image. This is generic + and can function even outside of the normal sheet + dynamic that the game uses. """ return pygame.image.load(os.path.join(IMAGE_PATH, filename)).convert() +################################### +# Section 2 - SheetManager Object # +################################### + +class SheetManager(manager.Manager): + """ + SheetManager handles loading and managing sheets. It is + talked to in order to get loaded sheets. SheetManager also + knows all animations and is talked to to get info about + them. + """ + + def __init__(self, game): + + super().__init__(game) + + # Important values + self.sheets_def = {} + self.loaded_sheets = {} + self.animations = {} + + def load_sheets(self, sheetjson): + """ + Load sheets from the given definition JSON file. + """ + self.sheets_def = json.load(open(os.path.join(JSON_PATH, sheetjson))) + for j in self.sheets_def: + self.loaded_sheets[j] = Sheet(self.sheets_def[j]["filename"], tuple(self.sheets_def[j]["dimensions"]), self.sheets_def[j]["total_sprites"]) + + def load_animations(self, animjson): + """ + Load animations from the given definition JSON file. + """ + self.animations = json.load(open(os.path.join(JSON_PATH, animjson))) + + # Make sure the sheet subimage coords are in tuple form + for c in self.animations: + for a in self.animations[c]: + for i in range(0, len(self.animations[c][a])): + for v in self.animations[c][a][i]: + if type(self.animations[c][a][i][v]) == list: + self.animations[c][a][i][v] = tuple(self.animations[c][a][i][v]) + + def get_sheet(self, name): + if name in self.loaded_sheets.keys(): + return self.loaded_sheets[name] + + def get_animation(self, name): + if name in self.loaded_animations.keys(): + return self.loaded_animations[name] + ############################ -# Section 2 - Sheet Object # +# Section 3 - Sheet Object # ############################ class Sheet(object):