#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 移动脚本 - 让对象在指定方向上来回移动 """ from core.script_system import ScriptBase import math class MoverScript(ScriptBase): """移动脚本类""" def __init__(self): super().__init__() # 移动参数 self.move_distance = 5.0 # 移动距离 self.move_speed = 2.0 # 移动速度 (单位/秒) self.move_axis = "x" # 移动轴: "x", "y", "z" # 内部变量 self.start_position = None # 起始位置 self.current_direction = 1 # 当前移动方向: 1或-1 self.current_distance = 0.0 # 当前移动距离 self.is_moving = True # 是否正在移动 def start(self): """脚本开始时调用""" self.log("移动脚本启动!") self.log(f"移动参数: 距离={self.move_distance}, 速度={self.move_speed}, 轴={self.move_axis}") # 记录起始位置 self.start_position = self.gameObject.getPos() self.log(f"起始位置: {self.start_position}") def update(self, dt): """每帧更新""" if not self.is_moving or self.start_position is None: return # 计算移动增量 move_delta = self.move_speed * dt * self.current_direction self.current_distance += abs(move_delta) # 检查是否需要改变方向 if self.current_distance >= self.move_distance: self.current_direction *= -1 self.current_distance = 0.0 # 应用移动 current_pos = self.gameObject.getPos() new_pos = [current_pos.getX(), current_pos.getY(), current_pos.getZ()] if self.move_axis == "x": new_pos[0] += move_delta elif self.move_axis == "y": new_pos[1] += move_delta elif self.move_axis == "z": new_pos[2] += move_delta self.gameObject.setPos(new_pos[0], new_pos[1], new_pos[2]) def set_move_parameters(self, distance=None, speed=None, axis=None): """设置移动参数""" if distance is not None: self.move_distance = distance if speed is not None: self.move_speed = speed if axis is not None and axis in ["x", "y", "z"]: self.move_axis = axis self.log(f"移动参数更新: 距离={self.move_distance}, 速度={self.move_speed}, 轴={self.move_axis}") def toggle_movement(self): """切换移动状态""" self.is_moving = not self.is_moving status = "恢复" if self.is_moving else "暂停" self.log(f"移动{status}") def reset_position(self): """重置到起始位置""" if self.start_position: self.gameObject.setPos(self.start_position) self.current_distance = 0.0 self.current_direction = 1 self.log("位置已重置到起始点") def on_destroy(self): """脚本销毁时调用""" self.log("移动脚本停止")