Return to repo list

tzed

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

commit bee56e9c4b291caab14e32721e636591f5057e2c
parent 30e78ca45b2ed27a2d7099495f3b26d563505f0d
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Tue, 31 Aug 2021 22:15:58 -0500

very basic turn taking

Diffstat:
Mdata/boards/testloc1/entities.json | 2+-
Mmain.py | 6++++++
Msrc/board.py | 32+++++++++++++++-----------------
Msrc/entity.py | 19+++++++++++++++----
Msrc/game.py | 3+--
5 files changed, 38 insertions(+), 24 deletions(-)

diff --git a/data/boards/testloc1/entities.json b/data/boards/testloc1/entities.json @@ -22,7 +22,7 @@ "sprite" : [4, 4], "npc_def" : { "npc_id" : 100, - "ai_package" : "wait" + "ai_package" : "wander" } } } diff --git a/main.py b/main.py @@ -18,6 +18,12 @@ from src import game # by simply making that many assets, or by making only the basic ones and scaling assets internally on startup and after a resolution change using # pygame's image transform features. As an addition, game elements that are positioned according to their absolute screen position, rather than being # positioned according to the relative location of another game element, should be accounted for in the aforementioned resolution association lists. +# 2. The need for DynamicEntity is questionable. Probably all on-board game objects can be handled by what is now StaticEntity, and things that the board +# shouldn't control can just source straight from entity. This will necessitate a redoing of entities.json files and some minor modifications to the +# syntax of the generation methods in Board. However, there should be another way to mark something as generate-once-only. Currently, NPCs are being +# generated only once owing to a special "npc_id" int in the entities.json "data" field. This is not good at all. There should be a way for the board +# to know that it is generating a StaticEntity for the first time, and to add it to holdovers in that case and not generate again in the future if it +# is of the correct type(s) (e.g. NPCs). ########### # main.py # diff --git a/src/board.py b/src/board.py @@ -56,7 +56,7 @@ class Board(object): # Entity values self.statics = pygame.sprite.Group() self.holdovers = pygame.sprite.Group() - self.dynamics = pygame.sprite.Group() + self.npcs = pygame.sprite.Group() self.overlays = pygame.sprite.Group() def coordinate_cells(self): @@ -135,6 +135,7 @@ class Board(object): to_gen = [] self.statics.empty() self.overlays.empty() + self.npcs.empty() # Next, decide which entities we should actually generate for e in self.entity_definition["static"]: @@ -163,6 +164,7 @@ class Board(object): if make: npce = entity.NPCEntity(npc_sheet[tuple(i[0]["data"]["sprite"])], tuple(i[0]["cell"]), tuple(i[0]["tile"]), None, xy, i[0]["data"]["npc_def"]) self.statics.add(npce) + self.npcs.add(npce) self.holdovers.add(npce) # Next, re-add holdovers @@ -175,23 +177,8 @@ class Board(object): hxy = (h.tilepos[0] + (herc[0] * self.cell_dimensions[0]), h.tilepos[1] + (herc[1] * self.cell_dimensions[1])) h.relative_tilepos = hxy self.statics.add(h) + self.npcs.add(h) - def create_dynamic_entities(self): - """ - Create the initial versions of any dynamic entities. - This happens only once, when the board is first - switched to. - """ - for e in self.entity_definition["dynamic"]: - pass - - def cell_shift_dynamic_entities(self): - """ - Move the managed dynamic entities around when a - cell change happens. - """ - pass - def position_to_tile(self, tilepos): """ Position this board to a given tilepos. @@ -222,6 +209,17 @@ class Board(object): self.game.switch_board(event["board"], tuple(event["cell"])) self.game.spawn_player(self.game.party[self.game.player_name], tuple(event["cell"]), tuple(event["tile"])) + def execute_npc_turns(self, time): + """ + Take a number of turns equal to time for NPCs. + """ + p = 0 + while p < time: + for npc in self.npcs: + npc.process_ai_package() + npc.position_to_board(self.scale_factor, self.position_offset, self.tile_offset) + p += 1 + def update_board(self, draw_surface, viewport_rect): """ Update the board logic, tiles, and entities. diff --git a/src/entity.py b/src/entity.py @@ -1,4 +1,4 @@ -import pygame, json, os +import pygame, json, os, random from .gamelib import * ############# @@ -162,8 +162,19 @@ class NPCEntity(StaticEntity): self.npc_id = npc_def["npc_id"] self.ai_package = npc_def["ai_package"] - def move_by_tile_offset(self): - pass + def move_by_tile_offset(self, offset): + """ + Change the relative_offset of this NPC to + a different value, by offset. + """ + if offset != (0, 0): + self.relative_tilepos = (self.relative_tilepos[0] + offset[0], self.relative_tilepos[1] + offset[1]) def process_ai_package(self): - pass + """ + Perform an AI move at turn time. + """ + if self.ai_package == "idle": + return + elif self.ai_package == "wander": + self.move_by_tile_offset((random.randint(-1, 1), random.randint(-1, 1))) diff --git a/src/game.py b/src/game.py @@ -141,7 +141,6 @@ class Game(object): self.old_location = self.current_board.display_name self.current_board = self.loaded_boards[boardname] self.current_board.change_cell(cellpos) - self.current_board.create_dynamic_entities() self.collect_game_data() self.message_board.post("$PLAYERNAME enters $CURRENTBOARDNAME.", self.gamedata) else: @@ -259,7 +258,7 @@ class Game(object): # Then, if take_turn is true, take a turn for the other entities. if take_turn: - pass + self.current_board.execute_npc_turns(time_mod) def derive_datestring(self): """