Добавил ускоренный A* из библиотеки pathfinding - произволительность выросла, но несильно. Пока этот вариант закомментил, сейчас реализация BFS + walkable матрица, работает гораздо лучше, с неоптимальным рендером 100 объектов держит, без рендера 300.
This commit is contained in:
@@ -38,13 +38,28 @@ class Creature(Object):
|
||||
|
||||
move_progress: float = 0.0 # 0.0 = старт клетки, 1.0 = конец клетки
|
||||
current_target: tuple = None # (row, col) следующая клетка
|
||||
final_goal: tuple = None
|
||||
move_speed: float = 0.02 # пикселей/кадр (настройте)
|
||||
render_offset: tuple = (0.0, 0.0)
|
||||
start_pos: tuple = None # (row, col) начальная позиция сегмента пути
|
||||
|
||||
replan_counter: int = 0
|
||||
REPLAN_INTERVAL: int = 300
|
||||
|
||||
|
||||
def replan(self, cells, pos):
|
||||
from common import find_way
|
||||
path = find_way(cells, pos, self.final_goal)
|
||||
if path and len(path) > 1:
|
||||
self.waypoints = path[1:]
|
||||
self.current_target = self.waypoints[0]
|
||||
else:
|
||||
self.waypoints.clear()
|
||||
self.current_target = None
|
||||
|
||||
def move(self, cells, start, goal):
|
||||
from common import find_way
|
||||
self.final_goal = goal
|
||||
self.start_pos = start
|
||||
path = find_way(cells, start, goal)
|
||||
if path and len(path) > 1:
|
||||
@@ -59,6 +74,24 @@ class Creature(Object):
|
||||
if self.current_target is None or not self.waypoints:
|
||||
self.render_offset = (0.0, 0.0)
|
||||
return
|
||||
|
||||
self.replan_counter += 1
|
||||
if self.replan_counter >= self.REPLAN_INTERVAL:
|
||||
self.replan_counter = 0
|
||||
self.replan(map_obj.cells, self.start_pos)
|
||||
|
||||
|
||||
if self.current_target is None: return
|
||||
target_row, target_col = self.current_target
|
||||
if (target_row in map_obj.cells and
|
||||
target_col < len(map_obj.cells[target_row])):
|
||||
target_cell = map_obj.cells[target_row][target_col]
|
||||
if target_cell.creature_obj is not None:
|
||||
self.current_target = None
|
||||
self.waypoints.clear()
|
||||
self.render_offset = (0.0, 0.0)
|
||||
self.replan(map_obj.cells, self.start_pos)
|
||||
return
|
||||
|
||||
self.move_progress += self.move_speed * time_delta * 60
|
||||
self.move_progress = min(self.move_progress, 1.0)
|
||||
@@ -66,7 +99,7 @@ class Creature(Object):
|
||||
if self.move_progress >= 1.0:
|
||||
map_obj.move_obj('creature_obj', self.start_pos, self.current_target)
|
||||
|
||||
self.waypoints.pop(0)
|
||||
if self.waypoints: self.waypoints.pop(0)
|
||||
if self.waypoints:
|
||||
self.start_pos = self.current_target # Новая клетка как старт
|
||||
self.current_target = self.waypoints[0]
|
||||
@@ -79,7 +112,10 @@ class Creature(Object):
|
||||
|
||||
# ★ ТОЛЬКО интерполяция offset ★
|
||||
start_row, start_col = self.start_pos #or (0, 0)
|
||||
target_row, target_col = self.current_target
|
||||
if self.current_target is None:
|
||||
self.render_offset = (0.0, 0.0)
|
||||
return
|
||||
target_row, target_col = self.current_target
|
||||
offset_x = (target_col - start_col) * cell_size * self.move_progress
|
||||
offset_y = (target_row - start_row) * cell_size * self.move_progress
|
||||
self.render_offset = (offset_x, offset_y)
|
||||
|
||||
Reference in New Issue
Block a user