"""
骨骼动画系统插件使用示例
提供各种使用场景的代码示例
"""

def example_basic_usage(world):
    """
    基本使用示例
    """
    print("=== 骨骼动画插件基本使用示例 ===")
    
    # 获取插件实例
    skeletal_plugin = world.plugin_manager.get_plugin("skeletal_animation_system")
    if not skeletal_plugin:
        print("错误: 骨骼动画插件未加载")
        return
        
    animation_manager = skeletal_plugin.get_animation_manager()
    
    # 1. 加载简单的Actor
    print("1. 加载Actor...")
    actor, actor_id = animation_manager.load_actor(
        model_path="models/characters/robot.egg",
        position=(0, 0, 0),
        scale=1.0
    )
    
    if actor:
        print(f"   成功加载Actor: {actor_id}")
        
        # 2. 获取可用动画
        animations = animation_manager.get_actor_animations(actor_id)
        print(f"2. 可用动画: {animations}")
        
        # 3. 播放动画
        if animations:
            print("3. 播放第一个动画...")
            animation_manager.play_animation(
                actor_id=actor_id,
                anim_name=animations[0],
                loop=True,
                play_rate=1.0
            )
            
        # 4. 获取动画信息
        if animations:
            info = animation_manager.get_animation_info(actor_id, animations[0])
            print(f"4. 动画信息: {info}")
            
    print("=== 基本使用示例结束 ===\n")


def example_advanced_usage(world):
    """
    高级使用示例
    """
    print("=== 骨骼动画插件高级使用示例 ===")
    
    # 获取插件实例
    skeletal_plugin = world.plugin_manager.get_plugin("skeletal_animation_system")
    if not skeletal_plugin:
        print("错误: 骨骼动画插件未加载")
        return
        
    animation_manager = skeletal_plugin.get_animation_manager()
    
    # 1. 加载带多个动画的Actor
    print("1. 加载带多个动画的Actor...")
    anim_dict = {
        "walk": "models/anim/walk.egg",
        "run": "models/anim/run.egg",
        "jump": "models/anim/jump.egg",
        "idle": "models/anim/idle.egg"
    }
    
    actor, actor_id = animation_manager.load_actor(
        model_path="models/characters/hero.egg",
        anim_dict=anim_dict,
        position=(5, 0, 0),
        scale=1.2
    )
    
    if actor:
        print(f"   成功加载Actor: {actor_id}")
        
        # 2. 播放循环动画
        print("2. 播放循环idle动画...")
        animation_manager.play_animation(actor_id, "idle", loop=True)
        
        # 3. 3秒后切换到walk动画
        def switch_to_walk():
            print("3. 切换到walk动画...")
            animation_manager.stop_animation(actor_id, "idle")
            animation_manager.play_animation(actor_id, "walk", loop=True)
            
        # 使用Panda3D的任务系统在3秒后执行
        def schedule_task(task):
            if task.time >= 3.0:
                switch_to_walk()
                return task.done
            return task.cont
            
        world.taskMgr.doMethodLater(3.0, schedule_task, "switch_animation_task")
        
        # 4. 创建动画序列
        print("4. 创建动画序列...")
        sequence = animation_manager.create_animation_sequence(
            actor_id, 
            ["walk", "run", "jump"], 
            loop=True
        )
        
        # 5. 动画混合示例
        print("5. 创建动画混合...")
        blend = animation_manager.blend_animations(
            actor_id, 
            "walk", 
            "run", 
            blend_time=2.0,
            loop=True
        )
        
    print("=== 高级使用示例结束 ===\n")


def example_event_driven_usage(world):
    """
    事件驱动使用示例
    """
    print("=== 骨骼动画插件事件驱动使用示例 ===")
    
    # 1. 通过事件加载Actor
    print("1. 通过事件加载Actor...")
    world.acceptOnce("example_load_done", lambda: print("Actor加载完成"))
    world.send("skeletal.load_actor", [
        "models/characters/npc.egg",
        {"greet": "models/anim/greet.egg"},
        "npc_actor",
        (10, 0, 0),
        1.0
    ])
    
    # 2. 通过事件播放动画
    def play_greeting():
        print("2. 通过事件播放动画...")
        world.send("skeletal.play_animation", ["npc_actor", "greet", False, 1.0])
        
    # 3秒后播放动画
    def schedule_greeting(task):
        if task.time >= 3.0:
            play_greeting()
            return task.done
        return task.cont
        
    world.taskMgr.doMethodLater(3.0, schedule_greeting, "play_greeting_task")
    
    print("=== 事件驱动使用示例结束 ===\n")


def example_multiple_actors(world):
    """
    多Actor管理示例
    """
    print("=== 骨骼动画插件多Actor管理示例 ===")
    
    # 获取插件实例
    skeletal_plugin = world.plugin_manager.get_plugin("skeletal_animation_system")
    if not skeletal_plugin:
        print("错误: 骨骼动画插件未加载")
        return
        
    animation_manager = skeletal_plugin.get_animation_manager()
    
    # 1. 创建多个Actor
    print("1. 创建多个Actor...")
    actors_data = [
        {
            "model": "models/characters/warrior.egg",
            "anims": {"walk": "models/anim/warrior_walk.egg", "attack": "models/anim/warrior_attack.egg"},
            "position": (-5, -5, 0),
            "scale": 1.0,
            "id": "warrior"
        },
        {
            "model": "models/characters/mage.egg",
            "anims": {"walk": "models/anim/mage_walk.egg", "cast": "models/anim/mage_cast.egg"},
            "position": (5, -5, 0),
            "scale": 1.0,
            "id": "mage"
        },
        {
            "model": "models/characters/archer.egg",
            "anims": {"walk": "models/anim/archer_walk.egg", "shoot": "models/anim/archer_shoot.egg"},
            "position": (0, -10, 0),
            "scale": 1.0,
            "id": "archer"
        }
    ]
    
    created_actors = []
    for actor_data in actors_data:
        actor, actor_id = animation_manager.load_actor(
            model_path=actor_data["model"],
            anim_dict=actor_data["anims"],
            actor_id=actor_data["id"],
            position=actor_data["position"],
            scale=actor_data["scale"]
        )
        
        if actor:
            created_actors.append(actor_id)
            print(f"   创建Actor: {actor_id}")
            
    # 2. 统一控制所有Actor
    print("2. 统一控制所有Actor...")
    
    def start_all_walk():
        for actor_id in created_actors:
            animations = animation_manager.get_actor_animations(actor_id)
            if "walk" in animations:
                animation_manager.play_animation(actor_id, "walk", loop=True)
                
    def stop_all_animations():
        for actor_id in created_actors:
            animation_manager.stop_all_animations(actor_id)
            
    # 3秒后开始行走，再过3秒后停止
    def control_all_actors(task):
        if task.time >= 3.0 and task.time < 3.1:
            start_all_walk()
            print("   所有角色开始行走")
        elif task.time >= 6.0 and task.time < 6.1:
            stop_all_animations()
            print("   所有角色停止动画")
            return task.done
        return task.cont
        
    world.taskMgr.add(control_all_actors, "control_all_actors_task")
    
    print("=== 多Actor管理示例结束 ===\n")


def example_animation_manipulation(world):
    """
    动画操作示例
    """
    print("=== 骨骼动画插件动画操作示例 ===")
    
    # 获取插件实例
    skeletal_plugin = world.plugin_manager.get_plugin("skeletal_animation_system")
    if not skeletal_plugin:
        print("错误: 骨骼动画插件未加载")
        return
        
    animation_manager = skeletal_plugin.get_animation_manager()
    
    # 1. 加载Actor用于演示
    print("1. 加载Actor用于演示...")
    anim_dict = {
        "walk": "models/anim/walk.egg",
        "run": "models/anim/run.egg",
        "jump": "models/anim/jump.egg"
    }
    
    actor, actor_id = animation_manager.load_actor(
        model_path="models/characters/demo_character.egg",
        anim_dict=anim_dict,
        position=(0, 10, 0),
        scale=1.0
    )
    
    if actor:
        # 2. 演示播放速度控制
        print("2. 演示播放速度控制...")
        animation_manager.play_animation(actor_id, "walk", loop=True, play_rate=0.5)
        print("   以0.5倍速度播放walk动画")
        
        # 3秒后调整速度
        def change_speed(task):
            if task.time >= 3.0 and task.time < 3.1:
                animation_manager.set_animation_speed(actor_id, "walk", 2.0)
                print("   调整为2.0倍速度播放walk动画")
                return task.done
            return task.cont
            
        world.taskMgr.doMethodLater(3.0, change_speed, "change_speed_task")
        
        # 3. 演示暂停和恢复
        print("3. 演示暂停和恢复...")
        
        def pause_resume_demo(task):
            if task.time >= 6.0 and task.time < 6.1:
                animation_manager.pause_animation(actor_id)
                print("   暂停动画")
            elif task.time >= 8.0 and task.time < 8.1:
                animation_manager.resume_animation(actor_id)
                print("   恢复动画")
                return task.done
            return task.cont
            
        world.taskMgr.doMethodLater(6.0, pause_resume_demo, "pause_resume_task")
        
        # 4. 演示动画信息获取
        print("4. 演示动画信息获取...")
        
        def show_animation_info(task):
            if task.time >= 10.0 and task.time < 10.1:
                info = animation_manager.get_actor_info(actor_id)
                print(f"   Actor信息: {info}")
                return task.done
            return task.cont
            
        world.taskMgr.doMethodLater(10.0, show_animation_info, "show_info_task")
        
    print("=== 动画操作示例结束 ===\n")


def run_all_examples(world):
    """
    运行所有示例
    """
    print("开始运行骨骼动画插件所有示例...")
    
    example_basic_usage(world)
    example_advanced_usage(world)
    example_event_driven_usage(world)
    example_multiple_actors(world)
    example_animation_manipulation(world)
    
    print("所有示例运行完成!")


# 使用说明和API参考
USAGE_GUIDE = """
骨骼动画插件使用说明
==================

1. 基本概念
----------
- Actor: 带有骨骼的3D模型对象
- Animation: 应用于Actor的动画序列
- Manager: 管理所有Actor和动画的中心控制器

2. 主要功能
----------
- Actor加载和管理
- 动画播放、暂停、停止控制
- 动画速度调节
- 动画混合和序列播放
- 事件驱动的动画控制
- 多Actor统一管理

3. 核心API
----------
SkeletalAnimationManager:
  - load_actor(): 加载Actor模型
  - unload_actor(): 卸载Actor
  - play_animation(): 播放动画
  - stop_animation(): 停止动画
  - set_animation_speed(): 设置动画速度
  - pause_animation(): 暂停动画
  - resume_animation(): 恢复动画
  - blend_animations(): 混合动画
  - create_animation_sequence(): 创建动画序列

4. 事件系统
----------
- skeletal.load_actor: 加载Actor
- skeletal.play_animation: 播放动画
- skeletal.stop_animation: 停止动画
- skeletal.set_animation_speed: 设置动画速度
- skeletal.pause_animation: 暂停动画
- skeletal.resume_animation: 恢复动画

5. 使用示例
----------
参见examples.py文件中的各种使用示例
"""

if __name__ == "__main__":
    print(USAGE_GUIDE)