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

"""
颜色变化脚本 - 让对象颜色产生循环变化
"""

from core.script_system import ScriptBase
from panda3d.core import Vec4
import math

class ColorChangerScript(ScriptBase):
    """颜色变化脚本类"""
    
    def __init__(self):
        super().__init__()
        
        # 颜色参数
        self.color_speed = 1.0         # 颜色变化速度 (周期/秒)
        self.color_mode = "rainbow"    # 颜色模式: "rainbow", "pulse", "fade", "strobe"
        self.base_color = Vec4(1, 1, 1, 1)  # 基础颜色
        self.intensity = 1.0           # 颜色强度
        
        # 内部变量
        self.time_accumulator = 0.0    # 时间累积器
        self.original_color = None     # 原始颜色
        self.is_changing = True        # 是否正在变化
        self.strobe_state = False      # 闪烁状态
    
    def start(self):
        """脚本开始时调用"""
        self.log("颜色变化脚本启动！")
        self.log(f"颜色参数: 速度={self.color_speed}, 模式={self.color_mode}, 强度={self.intensity}")
        
        # 记录原始颜色
        self.original_color = self.gameObject.getColor()
        self.log(f"原始颜色: {self.original_color}")
    
    def update(self, dt):
        """每帧更新"""
        if not self.is_changing:
            return
        
        # 累积时间
        self.time_accumulator += dt
        
        # 根据模式计算新颜色
        if self.color_mode == "rainbow":
            new_color = self._calculate_rainbow_color()
        elif self.color_mode == "pulse":
            new_color = self._calculate_pulse_color()
        elif self.color_mode == "fade":
            new_color = self._calculate_fade_color()
        elif self.color_mode == "strobe":
            new_color = self._calculate_strobe_color()
        else:
            new_color = self.base_color
        
        # 应用颜色
        self.gameObject.setColor(new_color)
    
    def _calculate_rainbow_color(self):
        """计算彩虹颜色"""
        # 使用HSV到RGB的转换创建彩虹效果
        hue = (self.time_accumulator * self.color_speed) % 1.0
        
        # 简单的HSV到RGB转换
        i = int(hue * 6.0)
        f = (hue * 6.0) - i
        p = 0.0
        q = 1.0 - f
        t = f
        
        if i % 6 == 0:
            r, g, b = 1.0, t, p
        elif i % 6 == 1:
            r, g, b = q, 1.0, p
        elif i % 6 == 2:
            r, g, b = p, 1.0, t
        elif i % 6 == 3:
            r, g, b = p, q, 1.0
        elif i % 6 == 4:
            r, g, b = t, p, 1.0
        else:
            r, g, b = 1.0, p, q
        
        return Vec4(r * self.intensity, g * self.intensity, b * self.intensity, 1.0)
    
    def _calculate_pulse_color(self):
        """计算脉冲颜色"""
        pulse = (math.sin(self.time_accumulator * self.color_speed * 2 * math.pi) + 1.0) / 2.0
        multiplier = pulse * self.intensity
        return Vec4(
            self.base_color.getX() * multiplier,
            self.base_color.getY() * multiplier,
            self.base_color.getZ() * multiplier,
            self.base_color.getW()
        )
    
    def _calculate_fade_color(self):
        """计算淡入淡出颜色"""
        fade = (math.sin(self.time_accumulator * self.color_speed * 2 * math.pi) + 1.0) / 2.0
        alpha = fade * self.intensity
        return Vec4(
            self.base_color.getX(),
            self.base_color.getY(),
            self.base_color.getZ(),
            alpha
        )
    
    def _calculate_strobe_color(self):
        """计算闪烁颜色"""
        # 根据时间间隔切换状态
        interval = 1.0 / (self.color_speed * 2)  # 闪烁间隔
        if int(self.time_accumulator / interval) % 2 == 0:
            return Vec4(
                self.base_color.getX() * self.intensity,
                self.base_color.getY() * self.intensity,
                self.base_color.getZ() * self.intensity,
                self.base_color.getW()
            )
        else:
            return Vec4(0.1, 0.1, 0.1, self.base_color.getW())  # 暗色状态
    
    def set_color_parameters(self, speed=None, mode=None, base_color=None, intensity=None):
        """设置颜色参数"""
        if speed is not None:
            self.color_speed = speed
        if mode is not None and mode in ["rainbow", "pulse", "fade", "strobe"]:
            self.color_mode = mode
        if base_color is not None:
            self.base_color = base_color
        if intensity is not None:
            self.intensity = intensity
        
        self.log(f"颜色参数更新: 速度={self.color_speed}, 模式={self.color_mode}, 强度={self.intensity}")
    
    def toggle_color_change(self):
        """切换颜色变化状态"""
        self.is_changing = not self.is_changing
        status = "恢复" if self.is_changing else "暂停"
        self.log(f"颜色变化{status}")
    
    def reset_color(self):
        """重置到原始颜色"""
        if self.original_color:
            self.gameObject.setColor(self.original_color)
            self.time_accumulator = 0.0
            self.log("颜色已重置到原始值")
    
    def set_solid_color(self, r=1.0, g=1.0, b=1.0, a=1.0):
        """设置固定颜色"""
        color = Vec4(r, g, b, a)
        self.gameObject.setColor(color)
        self.base_color = color
        self.log(f"设置固定颜色: {color}")
    
    def on_destroy(self):
        """脚本销毁时调用"""
        self.log("颜色变化脚本停止") 