Return to repo list

tzed

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

commit b82ca424260a2042bc2f61bca9533818b45cadfb
parent bee56e9c4b291caab14e32721e636591f5057e2c
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Wed,  1 Sep 2021 00:24:35 -0500

npc can now move

Diffstat:
Msrc/board.py | 2+-
Msrc/entity.py | 31++++++++++++++++++++++++++++---
Msrc/game.py | 4++--
3 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/src/board.py b/src/board.py @@ -162,7 +162,7 @@ class Board(object): if h.npc_id == i[0]["data"]["npc_def"]["npc_id"]: make = False 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"]) + 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) self.statics.add(npce) self.npcs.add(npce) self.holdovers.add(npce) diff --git a/src/entity.py b/src/entity.py @@ -152,13 +152,16 @@ class NPCEntity(StaticEntity): in locations. """ - def __init__(self, image, cellpos, tilepos, pixcolor = None, rel_tilepos = None, npc_def = {}): + def __init__(self, image, cellpos, tilepos, pixcolor = None, rel_tilepos = None, npc_def = {}, board = None): # Parent intialization super().__init__(image, cellpos, tilepos, pixcolor, rel_tilepos) - # Definition vals + # Other saved vals self.definition = npc_def + self.board = board + + # Definition vals self.npc_id = npc_def["npc_id"] self.ai_package = npc_def["ai_package"] @@ -168,7 +171,28 @@ class NPCEntity(StaticEntity): a different value, by offset. """ if offset != (0, 0): - self.relative_tilepos = (self.relative_tilepos[0] + offset[0], self.relative_tilepos[1] + offset[1]) + nt = (self.tilepos[0] + offset[0], self.tilepos[1] + offset[1]) + + nc = (0, 0) + if nt[0] < 0: + nc = (-1, nc[1]) + nt = (nt[0] + self.board.cell_dimensions[0], nt[1]) + elif nt[0] >= self.board.cell_dimensions[0]: + nc = (1, nc[1]) + nt = (nt[0] - self.board.cell_dimensions[0], nt[1]) + + if nt[1] < 0: + nc = (nc[0], -1) + nt = (nt[0], nt[1] + self.board.cell_dimensions[1]) + elif nt[1] >= self.board.cell_dimensions[1]: + nc = (nc[0], 1) + nt = (nt[0], nt[1] - self.board.cell_dimensions[1]) + + ccc = (self.cellpos[0] + nc[0], self.cellpos[1] + nc[1]) + if ccc in self.board.cellmap.keys(): + self.cellpos = ccc + self.tilepos = nt + self.relative_tilepos = (self.relative_tilepos[0] + offset[0], self.relative_tilepos[1] + offset[1]) def process_ai_package(self): """ @@ -178,3 +202,4 @@ class NPCEntity(StaticEntity): return elif self.ai_package == "wander": self.move_by_tile_offset((random.randint(-1, 1), random.randint(-1, 1))) + self.position_to_board(self.board.scale_factor, self.board.position_offset, self.board.tile_offset) diff --git a/src/game.py b/src/game.py @@ -199,7 +199,6 @@ class Game(object): if np[1] >= 0 and np[1] < len(self.current_board.current_cell) and np[0] >= 0 and np[0] < len(self.current_board.current_cell[np[1]]): if self.current_board.cellmap[self.player.cellpos][np[1]][np[0]][1]: self.player.tilepos = np - self.pass_time() canmove = True # Otherwise, in case we are trying to move out of the current cell, calculate the relative offset of the new cellpos @@ -217,13 +216,14 @@ class Game(object): self.player.cellpos = ncc self.player.tilepos = ntp self.current_board.change_cell(self.player.cellpos) - self.pass_time() canmove = True # Finally, post the message, reposition, and check events self.message_board.post("$PLAYERNAME moves " + d[offset] if offset in d.keys() and canmove else "$PLAYERNAME cannot move there!" if not canmove else "", self.gamedata) self.current_board.position_to_tile(self.player.tilepos) self.current_board.check_events_at_tilepos(self.player.tilepos) + if canmove: + self.pass_time() def pass_time(self, time_mod = 1, take_turn = True): """