2026-03-18 09:04:37 +08:00
|
|
|
|
import time
|
|
|
|
|
|
import math
|
2026-04-15 12:15:00 +08:00
|
|
|
|
from hardware_control import hw_ctrl
|
2026-03-18 09:04:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DeathManager:
|
2026-04-11 22:58:44 +08:00
|
|
|
|
def __init__(self, patrol_system, resurrection_waypoints_path=None,
|
|
|
|
|
|
release_spirit_key='9', resurrect_key='0'):
|
2026-03-18 09:04:37 +08:00
|
|
|
|
self.corpse_pos = None
|
|
|
|
|
|
self.patrol_system = patrol_system
|
|
|
|
|
|
self.is_running_to_corpse = False
|
2026-03-25 10:51:25 +08:00
|
|
|
|
self._spirit_release_sent = False
|
2026-04-11 22:58:44 +08:00
|
|
|
|
self.resurrection_waypoints = self._load_waypoints(resurrection_waypoints_path)
|
|
|
|
|
|
self._resurrection_completed = False
|
|
|
|
|
|
self.release_spirit_key = str(release_spirit_key).strip() or '9'
|
|
|
|
|
|
self.resurrect_key = str(resurrect_key).strip() or '0'
|
|
|
|
|
|
|
|
|
|
|
|
def _load_waypoints(self, path):
|
|
|
|
|
|
"""加载复活点路线 JSON,可置空。"""
|
|
|
|
|
|
if not path:
|
|
|
|
|
|
return None
|
|
|
|
|
|
import json, os
|
|
|
|
|
|
if not os.path.exists(path):
|
|
|
|
|
|
return None
|
|
|
|
|
|
try:
|
|
|
|
|
|
with open(path, 'r', encoding='utf-8') as f:
|
|
|
|
|
|
data = json.load(f)
|
|
|
|
|
|
if isinstance(data, list) and len(data) > 0:
|
|
|
|
|
|
return [(float(p[0]), float(p[1])) for p in data]
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
pass
|
|
|
|
|
|
return None
|
2026-03-25 10:51:25 +08:00
|
|
|
|
|
|
|
|
|
|
def reset_when_alive(self):
|
|
|
|
|
|
"""存活时清标志,避免下次死亡无法再次释放灵魂/记录尸体坐标。"""
|
|
|
|
|
|
self._spirit_release_sent = False
|
|
|
|
|
|
self.corpse_pos = None
|
|
|
|
|
|
self.is_running_to_corpse = False
|
2026-04-11 22:58:44 +08:00
|
|
|
|
self._resurrection_completed = False
|
2026-03-18 09:04:37 +08:00
|
|
|
|
|
|
|
|
|
|
def on_death(self, state):
|
|
|
|
|
|
"""1. 死亡瞬间调用:从 player_position 获取坐标并记录"""
|
2026-03-25 10:51:25 +08:00
|
|
|
|
if self._spirit_release_sent:
|
|
|
|
|
|
return
|
|
|
|
|
|
self._spirit_release_sent = True
|
2026-03-18 09:04:37 +08:00
|
|
|
|
self.corpse_pos = (state['x'], state['y'])
|
|
|
|
|
|
self.is_running_to_corpse = True
|
|
|
|
|
|
print(f">>> [系统] 记录死亡坐标: {self.corpse_pos},准备释放灵魂...")
|
2026-04-15 12:15:00 +08:00
|
|
|
|
hw_ctrl.press(self.release_spirit_key)
|
2026-03-18 09:04:37 +08:00
|
|
|
|
time.sleep(5) # 等待加载界面
|
|
|
|
|
|
|
2026-04-11 22:58:44 +08:00
|
|
|
|
def run_to_corpse(self, state, get_state=None):
|
|
|
|
|
|
"""
|
|
|
|
|
|
2. 跑尸寻路逻辑:坐标与朝向从 player_position 获取。
|
|
|
|
|
|
|
|
|
|
|
|
两阶段逻辑:
|
|
|
|
|
|
- 阶段1:若配置了复活点路线,先跑完该路线
|
|
|
|
|
|
- 阶段2:路线跑完后(或无路线),跑向尸体
|
|
|
|
|
|
"""
|
2026-03-18 09:04:37 +08:00
|
|
|
|
if not self.corpse_pos:
|
2026-04-11 22:58:44 +08:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# 阶段1:复活路线未走完 → navigate_path 跑完路线
|
|
|
|
|
|
if self.resurrection_waypoints and not self._resurrection_completed:
|
|
|
|
|
|
if get_state is None:
|
|
|
|
|
|
return
|
|
|
|
|
|
if self.patrol_system.navigate_path(get_state, self.resurrection_waypoints):
|
|
|
|
|
|
self._resurrection_completed = True
|
|
|
|
|
|
print(">>> 复活路线跑完,开始跑向尸体...")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# 阶段2:复活路线走完(或无) → 直接跑向尸体
|
2026-03-18 09:04:37 +08:00
|
|
|
|
is_arrived = self.patrol_system.navigate_to_point(state, self.corpse_pos)
|
|
|
|
|
|
# 如果距离尸体很近(0.005 约等于 10-20 码)
|
|
|
|
|
|
if is_arrived:
|
|
|
|
|
|
print(">>> 已到达尸体附近,尝试复活...")
|
2026-04-15 12:15:00 +08:00
|
|
|
|
hw_ctrl.press(self.resurrect_key)
|
2026-03-18 09:04:37 +08:00
|
|
|
|
time.sleep(5)
|
2026-04-19 15:53:45 +08:00
|
|
|
|
# 检查是否还是灵魂状态,如果是则再按一次复活键
|
|
|
|
|
|
if get_state:
|
|
|
|
|
|
new_state = get_state()
|
|
|
|
|
|
if new_state and new_state.get('death_state') == 2:
|
|
|
|
|
|
print(">>> 仍为灵魂状态,再次尝试复活...")
|
|
|
|
|
|
hw_ctrl.press(self.resurrect_key)
|
|
|
|
|
|
time.sleep(5)
|
2026-03-18 09:04:37 +08:00
|
|
|
|
self.is_running_to_corpse = False
|
|
|
|
|
|
self.corpse_pos = None
|
|
|
|
|
|
return
|