#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
旋转脚本 - 让对象持续旋转
"""

from core.script_system import ScriptBase


class RotatorScript(ScriptBase):
    """旋转脚本类"""

    def __init__(self):
        super().__init__()
        self.rotation_speed_y = 30.0  # Y轴旋转速度 (度/秒)
        self.max_angle = 15.0
        self.direction = 1
        self.current_angle = 0.0
        self.is_rotating = True  # 是否正在旋转

    def start(self):
        """脚本开始时调用"""
        self.log("旋转脚本启动！")
        self.log(f"旋转速度: {self.rotation_speed_y}度/秒")

    def update(self, dt):
        """每帧更新"""

        delta_angle = self.rotation_speed_y * dt * self.direction
        self.current_angle += delta_angle

        # 如果超出角度范围，则反向
        if self.current_angle > self.max_angle:
            self.current_angle = self.max_angle
            self.direction *= -1
        elif self.current_angle < -self.max_angle:
            self.current_angle = -self.max_angle
            self.direction *= -1

        # 设置新的旋转（只改变Z轴，保持其他不变）
        base_hpr = self.gameObject.getHpr()
        new_r = self.current_angle
        self.gameObject.setHpr(base_hpr.getX(), base_hpr.getY(), new_r)

        # if not self.is_rotating:
        #     return
        #
        # # 获取当前旋转并应用增量
        # current_hpr = self.gameObject.getHpr()
        # new_r = current_hpr.getZ() + self.rotation_speed_y * dt
        # self.gameObject.setHpr(current_hpr.getX(), current_hpr.getY(), new_r)

    def on_destroy(self):
        """脚本销毁时调用"""
        self.log("旋转脚本停止")