update move

This commit is contained in:
bridge
2025-10-10 00:28:52 +08:00
parent c982996509
commit 6ecf8cc18f
7 changed files with 98 additions and 39 deletions

View File

@@ -1,7 +1,8 @@
from __future__ import annotations
from src.classes.action import InstantAction
from src.classes.action import InstantAction, Move
from src.classes.event import Event
from src.classes.action.move_helper import clamp_manhattan_with_diagonal_priority
class MoveAwayFromRegion(InstantAction):
@@ -10,15 +11,26 @@ class MoveAwayFromRegion(InstantAction):
PARAMS = {"region": "RegionName"}
def _execute(self, region: str) -> None:
# 简化:向地图边缘移动一步
dx = 1 if self.avatar.pos_x < self.world.map.width - 1 else -1
dy = 1 if self.avatar.pos_y < self.world.map.height - 1 else -1
nx = max(0, min(self.world.map.width - 1, self.avatar.pos_x + dx))
ny = max(0, min(self.world.map.height - 1, self.avatar.pos_y + dy))
if self.world.map.is_in_bounds(nx, ny):
self.avatar.pos_x = nx
self.avatar.pos_y = ny
self.avatar.tile = self.world.map.get_tile(nx, ny)
# 策略:朝最近的边界方向移动,曼哈顿步长限制,优先斜向
width = self.world.map.width
height = self.world.map.height
x = self.avatar.pos_x
y = self.avatar.pos_y
# 离四边的距离
dist_left = x
dist_right = width - 1 - x
dist_top = y
dist_bottom = height - 1 - y
# 选择更近的两个边界方向组成一个大致“远离区域”的方向
# 简化:朝左右中更近的一侧+上下中更近的一侧移动
dir_x = -1 if dist_left < dist_right else (1 if dist_right < dist_left else 0)
dir_y = -1 if dist_top < dist_bottom else (1 if dist_bottom < dist_top else 0)
step = getattr(self.avatar, "move_step_length", 1)
dx, dy = clamp_manhattan_with_diagonal_priority(dir_x * step, dir_y * step, step)
Move(self.avatar, self.world).execute(dx, dy)
def can_start(self, region: str | None = None) -> bool:
return True