Return to repo list

tzed

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

commit abf48ae063c2081a7d2e18ae39dfa8c846fcf57c
parent b82ca424260a2042bc2f61bca9533818b45cadfb
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Thu,  2 Sep 2021 14:59:58 -0500

npc tile col handled

Diffstat:
Msrc/board.py | 15+++++++++++++++
Msrc/entity.py | 21+++++++++++++++------
Msrc/game.py | 7++++---
3 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/src/board.py b/src/board.py @@ -47,6 +47,8 @@ class Board(object): } self.position_offset = (0, 0) self.tile_offset = (0, 0) + self.playerpos = ((-1, -1), (-1, -1)) + self.npc_positions = [] self.cellmap = {} self.coordinate_cells() @@ -84,6 +86,14 @@ class Board(object): self.calculate_adjacent_cells() self.generate_tiles() self.generate_static_entities() + self.get_playerpos() + + def get_playerpos(self): + """ + Get the position of the player from the game. + """ + if self.game.player != None: + self.playerpos = (self.game.player.cellpos, self.game.player.tilepos) def calculate_adjacent_cells(self): """ @@ -230,3 +240,8 @@ class Board(object): # Update entities self.statics.update(draw_surface, viewport_rect) + + # Final updates + self.npc_positions = [] + for n in self.npcs: + self.npc_positions.append((n.cellpos, n.tilepos)) diff --git a/src/entity.py b/src/entity.py @@ -165,15 +165,21 @@ class NPCEntity(StaticEntity): self.npc_id = npc_def["npc_id"] self.ai_package = npc_def["ai_package"] - def move_by_tile_offset(self, offset): + def ai_move_by_tile_offset(self, offset): """ Change the relative_offset of this NPC to - a different value, by offset. + a different value, by offset. Called only + by the AI package. """ + + # First, make sure we have a valid offset, otherwise do nothing if offset != (0, 0): - nt = (self.tilepos[0] + offset[0], self.tilepos[1] + offset[1]) + # Next, set up basic vals representing landing cellpos and tilepos to work with nc = (0, 0) + nt = (self.tilepos[0] + offset[0], self.tilepos[1] + offset[1]) + + # In this block, we decide if we are going to have to change cells, and if so, in what direction if nt[0] < 0: nc = (-1, nc[1]) nt = (nt[0] + self.board.cell_dimensions[0], nt[1]) @@ -188,8 +194,11 @@ class NPCEntity(StaticEntity): nc = (nc[0], 1) nt = (nt[0], nt[1] - self.board.cell_dimensions[1]) + # Set the final landing cell to the new value. Note that this should add (0, 0) if no cell change happens ccc = (self.cellpos[0] + nc[0], self.cellpos[1] + nc[1]) - if ccc in self.board.cellmap.keys(): + + # If the cell target is valid, the tile target is passable, and the player is not standing there, move + if ccc in self.board.cellmap.keys() and self.board.cellmap[ccc][nt[1]][nt[0]][1] and self.board.playerpos != (ccc, nt) and (ccc, nt) not in self.board.npc_positions: self.cellpos = ccc self.tilepos = nt self.relative_tilepos = (self.relative_tilepos[0] + offset[0], self.relative_tilepos[1] + offset[1]) @@ -201,5 +210,5 @@ class NPCEntity(StaticEntity): if self.ai_package == "idle": 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) + r = ((-1, 0), (1, 0), (0, -1), (0, 1)) + self.ai_move_by_tile_offset(r[random.randint(0, len(r) - 1)]) diff --git a/src/game.py b/src/game.py @@ -161,6 +161,7 @@ class Game(object): self.current_board.position_offset = self.player.rect.topleft self.current_board.position_to_tile(tilepos) self.current_board.generate_tiles() + self.current_board.get_playerpos() else: return 1 @@ -197,7 +198,7 @@ class Game(object): # Next, if the new tilepos is within the bounds of the current cell and is passable, simply change the players tilepos to that value. 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]: + if self.current_board.cellmap[self.player.cellpos][np[1]][np[0]][1] and (self.player.cellpos, np) not in self.current_board.npc_positions: self.player.tilepos = np canmove = True @@ -212,7 +213,7 @@ class Game(object): ntp = (0 if celloff[0] > 0 else self.player.tilepos[0] if celloff[0] == 0 else self.current_board.cell_dimensions[0] - 1, 0 if celloff[1] > 0 else self.player.tilepos[1] if celloff[1] == 0 else self.current_board.cell_dimensions[1] - 1) # If that tilepos is passable, move the player's cellpos and tilepos, then change the board cell. - if self.current_board.cellmap[ncc][ntp[1]][ntp[0]][1]: + if self.current_board.cellmap[ncc][ntp[1]][ntp[0]][1] and (ncc, ntp) not in self.current_board.npc_positions: self.player.cellpos = ncc self.player.tilepos = ntp self.current_board.change_cell(self.player.cellpos) @@ -221,8 +222,8 @@ class Game(object): # 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.current_board.check_events_at_tilepos(self.player.tilepos) self.pass_time() def pass_time(self, time_mod = 1, take_turn = True):