pre commit, make CAPS tasks from main

This commit is contained in:
shiva404
2026-02-20 02:09:43 +03:00
parent 197469350d
commit 171eff1434
10 changed files with 284 additions and 126 deletions

View File

@@ -15,9 +15,11 @@ main_dir = os.path.dirname(os.path.abspath(__file__))
sprites_dir = os.path.join(main_dir, "res", "sprites")
#class Render
#class ObjectManager
#class MapManager
#class Event
#class EventManager
#class Control
@dataclass
class Cell:
@@ -60,22 +62,45 @@ class Map:
self.cells[line].append(final_cell)
def move_obj(self, type, s_x, s_y, d_x, d_y):
if d_y >= len(self.cells) or d_x >= len(self.cells[s_y]) or s_y >= len(self.cells) or s_x >= len(self.cells[s_y]):
return False
def move_obj(self, type, start, goal):
"""Перемещает объект типа 'terrain_obj', 'item_obj' или 'creature_obj'
из клетки start=(row, col) в goal=(row, col)"""
s_y, s_x = start
d_y, d_x = goal
# Проверка границ
if (s_y >= len(self.cells) or s_x >= len(self.cells[s_y]) or
d_y >= len(self.cells) or d_x >= len(self.cells[d_y])):
return False
source_cell = self.cells[s_y][s_x]
dest_cell = self.cells[d_y][d_x]
obj = getattr(source_cell, type)
if obj is None:
return False
setattr(source_cell, type, None)
setattr(dest_cell, type, obj)
return True
def get_way(self, s_x, s_y, d_x, d_y):
pass
def get_cell_at_mouse(self, mouse_pos):
"""Возвращает индексы клетки (row, col) по позиции мыши или None если вне карты"""
mx, my = mouse_pos
# Переводим экранные координаты в координаты карты с учетом камеры и масштаба
map_x = (mx - self.cam_x * self.scale) / self.scale / self.size
map_y = (my - self.cam_y * self.scale) / self.scale / self.size
# Получаем индексы ближайшей клетки
col = int(map_x)
row = int(map_y)
# Проверяем границы карты
if (row < 0 or row >= len(self.cells) or
col < 0 or col >= len(self.cells[row])):
return None
return (row, col)
def draw_map(self, screen, current_frame, grid = True):
for j in range(len(self.cells)):
@@ -97,8 +122,19 @@ class Map:
cell.creature_obj.draw(dd)
if grid:
pygame.draw.rect(screen, self.color, pygame.Rect(dd["x"], dd["y"], dd["w"], dd["h"]), self.bord)
if cell.is_target:
pygame.draw.rect(screen, self.target_color, pygame.Rect(dd["x"], dd["y"], dd["w"], dd["h"]), self.bord)
else:
pygame.draw.rect(screen, self.color, pygame.Rect(dd["x"], dd["y"], dd["w"], dd["h"]), self.bord)
def update_map(self, current_frame):
for j in range(len(self.cells)):
for i, cell in enumerate(self.cells[j]):
if cell.creature_obj is not None and len(cell.creature_obj.waypoints) > 1 and current_frame % 57 == 0:
del cell.creature_obj.waypoints[0]
self.move_obj("creature_obj", (j, i), (cell.creature_obj.waypoints[0]))
@dataclass
class Engine:
sprites: dict = field(default_factory = dict)
@@ -186,6 +222,11 @@ class Engine:
def main_loop(self):
easy_map = Map("def_map.json", self.cached_sprites)
#print(easy_map.find_way((1, 1), (5, 5)))
#print(easy_map.find_way((0, 0), (1, 1)))
#print(easy_map.find_way((0, 0), (0, 1)))
#print(easy_map.find_way((0, 0), (0, 0)))
#sp = eb_objects.Sprite(self.sprites, "elf_watching")
#gr = pygame.image.load(file_path).convert_alpha()
@@ -210,6 +251,8 @@ class Engine:
global_counter = 0
global_counter_cap = 100000
active_cell = []
# profiling
process = psutil.Process(os.getpid())
@@ -262,12 +305,13 @@ class Engine:
# fill the screen with a color to wipe away anything from last frame
#self.screen.fill("chartreuse4")
self.screen.blit(background, (0, 0))
easy_map.update_map(global_counter)
easy_map.draw_map(self.screen, current_frame + 1)
manager.draw_ui(self.screen)
if not console_active:
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
easy_map.cam_y += self.camera_step
if keys[pygame.K_s]:
@@ -276,6 +320,7 @@ class Engine:
easy_map.cam_x += self.camera_step
if keys[pygame.K_d]:
easy_map.cam_x -= self.camera_step
if keys[pygame.K_q]:
easy_map.scale += self.scale_step
self.spr_scale += self.scale_step
@@ -284,6 +329,27 @@ class Engine:
easy_map.scale -= self.scale_step
self.spr_scale -= self.scale_step
self.scale_sprites()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: # Левая кнопка
mouse_pos = pygame.mouse.get_pos()
console_rect = console_window.get_abs_rect()
if not console_rect.collidepoint(mouse_pos):
cell_coords = easy_map.get_cell_at_mouse(mouse_pos)
if cell_coords:
if active_cell:
easy_map.cells[active_cell[0]][active_cell[1]].is_target = False
active_cell = cell_coords
easy_map.cells[active_cell[0]][active_cell[1]].is_target = True
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:
mouse_pos = pygame.mouse.get_pos()
cell_coords = easy_map.get_cell_at_mouse(mouse_pos)
console_rect = console_window.get_abs_rect()
if not console_rect.collidepoint(mouse_pos) and easy_map.cells[active_cell[0]][active_cell[1]].creature_obj is not None:
easy_map.cells[active_cell[0]][active_cell[1]].creature_obj.move(easy_map.cells, (active_cell[0], active_cell[1]), (cell_coords[0], cell_coords[1]))
if keys[pygame.K_ESCAPE]:
running = False
@@ -298,6 +364,6 @@ class Engine:
if global_counter % 10 == 0:
current_fps = clock.get_fps()
#print(f"Current FPS: {current_fps:.2f}")
print(f"Current FPS: {current_fps:.2f}")
pygame.quit()