Return to repo list

heart-of-gold

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

commit 9b828bf73173d524c52e229d870fb1d925841467
parent 2bd58550b78d351ac7a988f8e711213693cace27
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Thu, 19 Nov 2020 13:19:11 -0600

item database now a const, and stats now figure in equipment

Diffstat:
Mmain.py | 1-
Msrc/constants.py | 25+++++++++++++------------
Msrc/piece.py | 29++++++++++++++++++-----------
3 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/main.py b/main.py @@ -25,7 +25,6 @@ from src import game # 9. Animations should not be image-file-specific. Rather, a field should be added to sheets.json entries called "anim" which will point to a # specific entry in the anims.json file. These entries can then be generalized for entire classes of spritesheets (in particular for piece sprite # sheets). This will require a rewrite of anims.json and a modification to the sheet manager. -# 10. All places where 'database' is loaded on the fly need to be replaced with a single DATABASE const in constants.py that is always available. # Initialization of pygame and submodules pygame.mixer.pre_init(44100, -16, 4, 1024) diff --git a/src/constants.py b/src/constants.py @@ -1,4 +1,4 @@ -import os, enum +import os, enum, json ################ # constants.py # @@ -29,7 +29,19 @@ PIECE_MOVE_DELAY = PIECE_MOVE_SPEED * 4 SCROLL_SPEED = 4 UI_FONT = "ArchivoNarrow-Regular.otf" +# File paths +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") +BOARD_PATH = os.path.join(DATA_PATH, "board") +TILE_PATH = os.path.join(DATA_PATH, "tsx") +JSON_PATH = os.path.join(DATA_PATH, "json") +MENU_JSON_PATH = os.path.join(JSON_PATH, "menus") +SCENE_JSON_PATH = os.path.join(JSON_PATH, "scenes") + # Item constants +ITEMBASE = json.load(open(os.path.join(JSON_PATH, "items.json"))) WEAPON_TYPE_SOURCES = { "Dagger" : "ATK", "Sword" : "ATK", @@ -57,17 +69,6 @@ WEAPON_TYPE_BONUSES = { "Wand" : "INT" } -# File paths -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") -BOARD_PATH = os.path.join(DATA_PATH, "board") -TILE_PATH = os.path.join(DATA_PATH, "tsx") -JSON_PATH = os.path.join(DATA_PATH, "json") -MENU_JSON_PATH = os.path.join(JSON_PATH, "menus") -SCENE_JSON_PATH = os.path.join(JSON_PATH, "scenes") - # Enums STATE_MODES = enum.Enum('STATE_MODES', 'Main_Menu_Mode Battle_Mode Still_Scene_Mode') CTRL_MODES = enum.Enum('CTRL_MODES', 'No_Control Main_Menu_Normal Turn_Normal Turn_Select_Move Turn_Select_Attack Turn_Watch_Move Turn_Watch_Attack Still_Scene_Normal') diff --git a/src/piece.py b/src/piece.py @@ -283,20 +283,20 @@ class Piece(entity.Entity): single string or an iterable (list, tuple) of strings that match entries in the item database. """ - database = json.load(open(os.path.join(JSON_PATH, "items.json"))) if type(equipment) == str: - if (database[equipment]["slot"] != "weapon" or database[equipment]["type"] in self.active_stats["EXPERTISE"]): - self.equipment[database[equipment]["slot"]] = equipment + if (ITEMBASE[equipment]["slot"] != "weapon" or ITEMBASE[equipment]["type"] in self.active_stats["EXPERTISE"]): + self.equipment[ITEMBASE[equipment]["slot"]] = equipment else: print("Failed to equip " + equipment + " to " + self.name) elif type(equipment) in (list, tuple): for e in equipment: if e != None: - if database[e]["slot"] != "weapon" or database[e]["type"] in self.active_stats["EXPERTISE"]: - self.equipment[database[e]["slot"]] = e + if ITEMBASE[e]["slot"] != "weapon" or ITEMBASE[e]["type"] in self.active_stats["EXPERTISE"]: + self.equipment[ITEMBASE[e]["slot"]] = e else: print("Failed to equip " + e + " to " + self.name) self.derive_range() + self.derive_equipment_bonus() def unequip(self, slot): """ @@ -304,7 +304,6 @@ class Piece(entity.Entity): a single string corresponding to an equipment slot, or an iterable (list, tuple) of such strings. """ - database = json.load(open(os.path.join(JSON_PATH, "items.json"))) if type(slot) == str: self.equipment[slot] = None elif type(slot) in (list, tuple): @@ -316,12 +315,21 @@ class Piece(entity.Entity): """ Figure out this piece's range based on equipment. """ - database = json.load(open(os.path.join(JSON_PATH, "items.json"))) if self.equipment["weapon"] != None: - self.attack_range = database[self.equipment["weapon"]]["range"] + self.attack_range = ITEMBASE[self.equipment["weapon"]]["range"] else: self.attack_range = 0 + def derive_equipment_bonus(self): + """ + Figure out what are stats are after taking the + equipment into account. + """ + for i in self.equipment: + if self.equipment[i] != None: + for s in ITEMBASE[self.equipment[i]]["stats"]: + self.active_stats[s] += ITEMBASE[self.equipment[i]]["stats"][s] + def render_damage_number(self): """ Render the name font image for display. @@ -377,9 +385,8 @@ class Piece(entity.Entity): self.attack_anim_timer = 90 # Tell the other guy to be damaged - database = json.load(open(os.path.join(JSON_PATH, "items.json"))) - prim = self.active_stats[WEAPON_TYPE_SOURCES[database[self.equipment["weapon"]]["type"]]] - bon = WEAPON_TYPE_SOURCES[database[self.equipment["weapon"]]["type"]] + prim = self.active_stats[WEAPON_TYPE_SOURCES[ITEMBASE[self.equipment["weapon"]]["type"]]] + bon = WEAPON_TYPE_SOURCES[ITEMBASE[self.equipment["weapon"]]["type"]] raw_dam = max(round((prim * 1.8) + random.randint(-(self.active_stats["LUK"] // 2), self.active_stats["LUK"])), 0) if bon != None and raw_dam != 0: raw_dam += round((self.active_stats[bon] + random.randint(0, self.active_stats["LUK"] // 2)) * 0.2)