Return to repo list

tzed

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

commit 88dd4602f0b18641f9aa186aa295ade774cd0807
parent 00161bf6cec802552de80acb033e09a17539c2cf
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Mon,  4 Oct 2021 15:09:15 -0500

enemy encounters now trigger

Diffstat:
Mdata/dungeons/testdun1/encounters.json | 16++++++++--------
Mdata/etc/sheets.json | 3++-
Msrc/battle.py | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Msrc/dungeon.py | 2+-
Msrc/game.py | 9++++++++-
5 files changed, 76 insertions(+), 17 deletions(-)

diff --git a/data/dungeons/testdun1/encounters.json b/data/dungeons/testdun1/encounters.json @@ -5,14 +5,14 @@ "factor" : 10, "rate" : 17, "parties" : { - "party1" : [ - { + "Rat Party" : { + "OneBigRat" : { "name" : "Big Rat", "sheet" : "enemies_battle1", "sprite" : [0, 0], "color" : 9, "row" : 0, - "position" : 0, + "position" : 1, "level" : 1, "xp" : 0, "hp" : 100, @@ -24,21 +24,21 @@ "chr" : 2, "luk" : 1 } - ] + } } }, { "factor" : 10, "rate" : 1, "parties" : { - "party1" : [ - { + "Rat Party" : { + "OneBigRat" : { "name" : "Big Rat", "sheet" : "enemies_battle1", "sprite" : [0, 0], "color" : 9, "row" : 0, - "position" : 0, + "position" : 1, "level" : 1, "xp" : 0, "hp" : 100, @@ -50,7 +50,7 @@ "chr" : 2, "luk" : 1 } - ] + } } } ] diff --git a/data/etc/sheets.json b/data/etc/sheets.json @@ -17,5 +17,6 @@ "dungeon1_w" : [220, 512, false], "dungeon1_h" : [242, 182, false], "dungeon1_cd" : [32, 32, false], - "compass" : [192, 48, false] + "compass" : [192, 48, false], + "enemies_battle1" : [256, 256, false] } diff --git a/src/battle.py b/src/battle.py @@ -29,18 +29,69 @@ class Battle(object): def __init__(self, game, player_party, enemy_party): # Saved vals + self.game = game self.player_party_definition = player_party self.enemy_party_definition = enemy_party # Groups and sprites - self.draw_group = pygame.sprite.Group() - self.player_party_group = pygame.sprite.Group() - self.enemy_party_group = pygame.sprite.Group() + self.player_group = pygame.sprite.Group() + self.enemy_group = pygame.sprite.Group() # Initialize the group members + # TODO: Once again, hardcoded positions for p in self.player_party_definition: - pg = entity.CustomSprite(self.game.sheets[self.player_party_definition[p]["party_sheet"]].sprites[tuple(self.player_party_definition[p]["party_sprite"])], (self.game.viewport_rect.left + (256 * self.player_party_definition[p]["position"]), self.game.viewport_rect.center[1] + (48 * self.player_party_definition[p]["row"])), self.player_party_definition[p]["color"]) - self.player_party_group.add(pg) + pg = BattleSprite(self, self.player_party_definition[p], self.game.sheets[self.player_party_definition[p]["party_sheet"]].sprites[tuple(self.player_party_definition[p]["party_sprite"])], (self.game.viewport_rect.left + (256 * self.player_party_definition[p]["position"]), self.game.viewport_rect.center[1] + (48 * self.player_party_definition[p]["row"])), self.player_party_definition[p]["color"]) + self.player_group.add(pg) for e in self.enemy_party_definition: - eg = entity.CustomSprite(self.game.sheets[self.enemy_party_definition[p]["sheet"]].sprites[tuple(self.enemy_party_definition[p]["sprite"])], (self.game.viewport_rect.left + (256 * self.enemy_party_definition[p]["position"]), self.enemy.viewport_rect.center[1] + (48 * self.enemy_party_definition[p]["row"])), self.enemy_party_definition[p]["color"]) + eg = BattleSprite(self, self.enemy_party_definition[e], self.game.sheets[self.enemy_party_definition[e]["sheet"]].sprites[tuple(self.enemy_party_definition[e]["sprite"])], (self.game.viewport_rect.left + (256 * self.enemy_party_definition[e]["position"]), self.game.viewport_rect.center[1] - 196 - (128 * self.enemy_party_definition[e]["row"])), self.enemy_party_definition[e]["color"]) + self.enemy_group.add(eg) + def get_enemy_forces(self): + """ + Returns a string indicating the names + and numbers of enemies in the current + enemy party. + """ + names = {} + for e in self.enemy_group: + if e.name in names.keys(): + names[e.name] += 1 + else: + names[e.name] = 1 + ns = "" + for n in names: + ns += ", " + n + " x " + str(names[n]) + ns = ns[2:len(ns)] + return ns + + def update_battle(self, surface = None): + """ + Update the battle logic and all battle + graphics. + """ + if surface != None: + self.enemy_group.update(surface) + self.player_group.update(surface) + +################################## +# Section 2 - BattleSprite class # +################################## + +class BattleSprite(entity.CustomSprite): + """ + Simple extension of the CustomSprite class + to hold some party information related to + battle participants. + """ + + def __init__(self, battle, stats, image, pos, pixcolor = None): + + # Parent initialization + super().__init__(image, pos, pixcolor) + + # Saved values + self.battle = battle + self.stats = stats + + # Derived vals + self.name = stats["name"] diff --git a/src/dungeon.py b/src/dungeon.py @@ -126,7 +126,7 @@ class Dungeon(object): workenc = self.floor_encounters[self.current_floor] if not self.safe and self.floor_factor_counters[self.current_floor] < workenc["factor"]: if random.randint(1, 20) >= workenc["rate"]: - self.game.trigger_battle(workenc[tuple(workenc.keys())[random.randint(0, len(workenc["parties"]))]]) + self.game.trigger_battle(workenc["parties"][tuple(workenc["parties"].keys())[random.randint(0, len(workenc["parties"]) - 1)]]) def take_dungeon_turn(self): """ diff --git a/src/game.py b/src/game.py @@ -71,6 +71,7 @@ class Game(object): self.loaded_dungeons = {} self.current_board = None self.current_dungeon = None + self.current_battle = None self.gamedata = {} self.gametime = 0 self.old_gametime = -1 @@ -342,7 +343,10 @@ class Game(object): Trigger a battle with the given party definition. """ - pass + self.current_battle = battle.Battle(self, self.party, partydef) + self.collect_game_data() + self.post_message("Joe encounters $CURRENTBATTLEFORCES") + self.switch_mode(STATE_MODES.Battle_Mode) def update_dynamic_ui(self, surface = None): """ @@ -430,6 +434,7 @@ class Game(object): self.gamedata["$PLAYERNAME"] = self.player_name self.gamedata["$CURRENTBOARDNAME"] = self.current_board.display_name if self.current_board != None else "!NOWHERE!" self.gamedata["$CURRENTDUNGEONNAME"] = self.current_dungeon.display_name if self.current_dungeon != None else "!NODUNGEON!" + self.gamedata["$CURRENTBATTLEFORCES"] = self.current_battle.get_enemy_forces() if self.current_battle != None else "!NOBATTLE!" def save_game(self, savename): """ @@ -533,6 +538,8 @@ class Game(object): elif self.state_mode in PARTY_MODES: if self.state_mode == STATE_MODES.Dungeon_Mode: self.current_dungeon.update_dungeon(self.screen) + elif self.state_mode == STATE_MODES.Battle_Mode: + self.current_battle.update_battle(self.screen) self.party_group.update(self.screen) self.ui_group.update(self.screen) self.message_board.update_message_board(self.screen)