Pathfinder singletone-class was added. Fixed leaky abstraction Map - move - Object using Pathfinder.

This commit is contained in:
shiva404
2026-03-07 03:07:39 +03:00
parent e347eb6fa6
commit b1b5375e46
4 changed files with 606 additions and 56 deletions

View File

@@ -1,4 +1,5 @@
from common import deepcopy, dataclass, field, random
from pathfinder import pathfinder
@dataclass
class Action:
@@ -44,8 +45,8 @@ class Creature(Object):
render_offset: tuple = (0.0, 0.0)
start_pos: tuple = None # (row, col) начальная позиция сегмента пути
waypoints: list = field(default_factory = list)
walkable_matrix: list = field(default_factory = list)
rocks_matrix: list = field(default_factory = list)
#walkable_matrix: list = field(default_factory = list)
#rocks_matrix: list = field(default_factory = list)
inventory: dict = field(default_factory = dict)
quick_actions: list = field(default_factory = list)
@@ -62,8 +63,7 @@ class Creature(Object):
interrupt_action_status: str = "completed"
def replan(self, cells, pos):
from common import find_way
path = find_way(cells, pos, self.final_goal, self.walkable_matrix, self.rocks_matrix)
path = pathfinder.find_way(pos, self.final_goal)
if path and len(path) > 1:
self.waypoints = path[1:]
self.current_target = self.waypoints[0]
@@ -131,10 +131,9 @@ class Creature(Object):
offset_y = (target_row - start_row) * cell_size * self.move_progress
self.render_offset = (offset_x, offset_y)
def move(self, cells, goal):
from common import find_way
def move(self, goal):
self.final_goal = goal
path = find_way(cells, self.grid_pos, self.final_goal, self.walkable_matrix, self.rocks_matrix)
path = pathfinder.find_way(self.grid_pos, self.final_goal)
if path and len(path) > 1:
self.waypoints = path[1:]
self.current_target = self.waypoints[0]
@@ -205,12 +204,12 @@ class Creature(Object):
# self.action_counter = 0
# if self.task_counter >= len(self.tasks):
# self.task_counter = 0
#
#
# self.action = self.tasks[self.task_counter][self.action_counter]
# self.action_counter += 1
# self.action_time = 0.0
def patrol(self, cells, area):
def patrol(self, area):
goal = (random.randint(self.grid_pos[0] - area,
self.grid_pos[0] + area),
random.randint(self.grid_pos[1] - area,
@@ -220,13 +219,13 @@ class Creature(Object):
self.grid_pos[0] + area),
random.randint(self.grid_pos[1] - area,
self.grid_pos[1] + area))
self.move(cells, goal)
self.move(goal)
def move_rand(self, cells, area_start, area_end):
def move_rand(self, area_start, area_end):
goal = (random.randint(area_start, area_end), random.randint(area_start, area_end))
while goal == self.grid_pos:
goal = (random.randint(area_start, area_end), random.randint(area_start, area_end))
self.move(cells, goal)
self.move(goal)
@dataclass
class Item(Object):