2026-rff_mp/MininaVD/docs2/data2/commandsMove_command.py

58 lines
1.5 KiB
Python

#!/usr/bin/env python
# coding: utf-8
# In[ ]:
from typing import Optional
from commandsCommand import Command
from commandsPlayer import Player
from modelsMaze import Maze
from modelsCell import Cell
class MoveCommand(Command):
"""Команда перемещения игрока."""
# Направления
DIRECTIONS = {
'w': (0, -1), # вверх
's': (0, 1), # вниз
'a': (-1, 0), # влево
'd': (1, 0), # вправо
}
def __init__(self, player: Player, maze: Maze, direction: str):
self.player = player
self.maze = maze
self.direction = direction.lower()
self._target_cell: Optional[Cell] = None
self._executed = False
def execute(self) -> bool:
"""Выполнить перемещение."""
if self.direction not in self.DIRECTIONS:
return False
dx, dy = self.DIRECTIONS[self.direction]
x = self.player.current_cell.x + dx
y = self.player.current_cell.y + dy
self._target_cell = self.maze.get_cell(x, y)
if self._target_cell and self._target_cell.is_passable():
self.player.move_to(self._target_cell)
self._executed = True
return True
return False
def undo(self) -> bool:
"""Отменить перемещение."""
if self._executed:
success = self.player.undo_move()
if success:
self._executed = False
return True
return False