Pathfinder singletone-class was added. Fixed leaky abstraction Map - move - Object using Pathfinder.
This commit is contained in:
48
eb_engine.py
48
eb_engine.py
@@ -4,6 +4,7 @@ from common import pygame, pygame_gui
|
||||
import eb_objects
|
||||
import eb_terrain_objects
|
||||
import eb_creature_objects
|
||||
from pathfinder import pathfinder
|
||||
|
||||
#from pympler import muppy, summary
|
||||
import gc, psutil, os
|
||||
@@ -38,8 +39,6 @@ class Map:
|
||||
sprites: dict
|
||||
sprites_refresh: int = 60
|
||||
cells: dict = field(default_factory = dict)
|
||||
walkable_matrix: list = field(default_factory = list)
|
||||
rocks_matrix: list = field(default_factory = list)
|
||||
color: str = "gray57"
|
||||
target_color: str = "gold"
|
||||
cell_size: int = 150
|
||||
@@ -70,13 +69,6 @@ class Map:
|
||||
|
||||
self.cells[line].append(final_cell)
|
||||
|
||||
self.compute_walkable_rocks()
|
||||
for j in range(len(self.cells)):
|
||||
for cell in self.cells[j]:
|
||||
if cell.creature_obj:
|
||||
cell.creature_obj.walkable_matrix = self.walkable_matrix
|
||||
cell.creature_obj.rocks_matrix = self.rocks_matrix
|
||||
|
||||
def draw_obj(self, obj, draw_data):
|
||||
if draw_data["spr_up"] == 0:
|
||||
if obj.sprite_state == len(draw_data["sprites"][obj.sprite_name]) - 1:
|
||||
@@ -134,41 +126,15 @@ class Map:
|
||||
return (row, col)
|
||||
|
||||
def update_map(self, time_delta):
|
||||
self.compute_walkable_rocks()
|
||||
pathfinder.set_map(self.cells)
|
||||
for j in range(len(self.cells)):
|
||||
for cell in self.cells[j]:
|
||||
if cell.creature_obj:
|
||||
cell.creature_obj.walkable_matrix = self.walkable_matrix
|
||||
cell.creature_obj.rocks_matrix = self.rocks_matrix
|
||||
if cell.creature_obj.final_goal is not None:
|
||||
cell.creature_obj.calc_step(time_delta, self.cell_size, self)
|
||||
continue
|
||||
cell.creature_obj.update(time_delta)
|
||||
|
||||
def compute_walkable_rocks(self):
|
||||
"""Вычисляет матрицы walkable и rocks_only БЕЗ учета стартовой позиции"""
|
||||
rows = len(self.cells)
|
||||
if rows == 0:
|
||||
return None, None
|
||||
|
||||
cols = len(self.cells[0])
|
||||
|
||||
# ★ ИНИЦИАЛИЗАЦИЯ ★
|
||||
self.walkable_matrix = [[True] * cols for _ in range(rows)]
|
||||
self.rocks_matrix = [[False] * cols for _ in range(rows)]
|
||||
|
||||
# ★ ПОЛНЫЙ ПРОХОД ПО КАРТЕ ★
|
||||
for r in range(rows):
|
||||
for c in range(cols):
|
||||
cell = self.cells[r][c]
|
||||
# Запрещаем ВСЕ существа (включая стартовое!)
|
||||
if cell.creature_obj or \
|
||||
(cell.terrain_obj and cell.terrain_obj.sprite_name == "rock_small"):
|
||||
self.walkable_matrix[r][c] = False
|
||||
|
||||
# Отмечаем маленькие камни
|
||||
if cell.terrain_obj and cell.terrain_obj.sprite_name == "rock_small":
|
||||
self.rocks_matrix[r][c] = True
|
||||
|
||||
def OLDSTABLE_draw_map(self, screen, current_frame, grid=True):
|
||||
terrain_list = []
|
||||
@@ -421,12 +387,10 @@ class Engine:
|
||||
|
||||
elf = eb_objects.Creature(id=f"elf_{random.randint(1000,9999)}",
|
||||
name="Elf", sprite_name="elf_watching",
|
||||
grid_pos = pos,
|
||||
walkable_matrix = easy_map.walkable_matrix,
|
||||
rocks_matrix = easy_map.rocks_matrix)
|
||||
grid_pos = pos)
|
||||
|
||||
r_move_short = eb_objects.Action(sprite_name="elf_watching", func=partial(elf.patrol, easy_map.cells, 3), duration=0.01)
|
||||
r_move_long = eb_objects.Action(sprite_name="elf_watching", func=partial(elf.move_rand, easy_map.cells, 0, 99), duration=0.01)
|
||||
r_move_short = eb_objects.Action(sprite_name="elf_watching", func=partial(elf.patrol, 3), duration=0.01)
|
||||
r_move_long = eb_objects.Action(sprite_name="elf_watching", func=partial(elf.move_rand, 0, 99), duration=0.01)
|
||||
elf.tasks.append([r_move_short]*5 + [r_move_long])
|
||||
easy_map.cells[row][col].creature_obj = elf
|
||||
|
||||
@@ -558,7 +522,7 @@ class Engine:
|
||||
cell_coords = easy_map.get_cell_at_mouse(mouse_pos)
|
||||
if cell_coords:
|
||||
#print(f"Движение: {active_cell} -> {cell_coords}")
|
||||
easy_map.cells[active_cell[0]][active_cell[1]].creature_obj.move(easy_map.cells, cell_coords)
|
||||
easy_map.cells[active_cell[0]][active_cell[1]].creature_obj.move(cell_coords)
|
||||
|
||||
if keys[pygame.K_ESCAPE]:
|
||||
running = False
|
||||
|
||||
Reference in New Issue
Block a user