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

"""
缩放脚本 - 让对象产生呼吸般的缩放效果
"""

from core.script_system import ScriptBase
import math

class ScalerScript(ScriptBase):
    """缩放脚本类"""
    
    def __init__(self):
        super().__init__()
        
        # 缩放参数
        self.base_scale = 1.0          # 基础缩放
        self.scale_amplitude = 0.3     # 缩放幅度
        self.scale_speed = 2.0         # 缩放速度 (周期/秒)
        self.uniform_scale = True      # 是否统一缩放（所有轴）
        
        # 内部变量
        self.time_accumulator = 0.0    # 时间累积器
        self.original_scale = None     # 原始缩放
        self.is_scaling = True         # 是否正在缩放
    
    def start(self):
        """脚本开始时调用"""
        self.log("缩放脚本启动！")
        self.log(f"缩放参数: 基础={self.base_scale}, 幅度={self.scale_amplitude}, 速度={self.scale_speed}")
        
        # 记录原始缩放
        self.original_scale = self.gameObject.getScale()
        self.log(f"原始缩放: {self.original_scale}")
    
    def update(self, dt):
        """每帧更新"""
        if not self.is_scaling:
            return
        
        # 累积时间
        self.time_accumulator += dt
        
        # 计算正弦波缩放值
        sine_value = math.sin(self.time_accumulator * self.scale_speed * 2 * math.pi)
        scale_factor = self.base_scale + (self.scale_amplitude * sine_value)
        
        # 应用缩放
        if self.uniform_scale:
            # 统一缩放
            self.gameObject.setScale(scale_factor)
        else:
            # 非统一缩放（仅Z轴）
            current_scale = self.gameObject.getScale()
            self.gameObject.setScale(current_scale.getX(), current_scale.getY(), scale_factor)
    
    def set_scale_parameters(self, base=None, amplitude=None, speed=None, uniform=None):
        """设置缩放参数"""
        if base is not None:
            self.base_scale = base
        if amplitude is not None:
            self.scale_amplitude = amplitude
        if speed is not None:
            self.scale_speed = speed
        if uniform is not None:
            self.uniform_scale = uniform
        
        self.log(f"缩放参数更新: 基础={self.base_scale}, 幅度={self.scale_amplitude}, 速度={self.scale_speed}")
    
    def toggle_scaling(self):
        """切换缩放状态"""
        self.is_scaling = not self.is_scaling
        status = "恢复" if self.is_scaling else "暂停"
        self.log(f"缩放{status}")
    
    def reset_scale(self):
        """重置到原始缩放"""
        if self.original_scale:
            self.gameObject.setScale(self.original_scale)
            self.time_accumulator = 0.0
            self.log("缩放已重置到原始值")
    
    def pulse_once(self):
        """执行一次脉冲缩放"""
        self.time_accumulator = 0.0
        self.log("执行脉冲缩放")
    
    def on_destroy(self):
        """脚本销毁时调用"""
        self.log("缩放脚本停止") 