/*
文件名:CharacterObjectClass.nut
路径:User/GameClass/ObjectClass/CharacterObjectClass.nut
创建日期:2024-05-11	21:03
文件用途:角色类
*/
class GameObject.Character extends GameObject.ActiveObject {

    hair = null; //头部
    hat = null; //帽子
    face = null; //脸部
    breast = null; //胸部
    coat = null; //上衣
    skin = null; //皮肤
    waist = null; //腰部
    pants = null; //下装
    shoes = null; //鞋子
    weapon = null; //武器

    //动画对象管理器
    AnimationManager = null;

    //属性对象
    Attribute = null;


    function Init(Idx) {
        //初始化动画组
        CurrentAni = [];
        //获取角色职业信息
        Info = sq_DeepCopy(AssetManager.CharacterInfoList[Idx]);
        base.Init(Info);

        //构造角色动画对象
        AnimationManager = Character_Animation();
        Addchild(AnimationManager);
        AnimationManager.Init();

        foreach(EquId in Info.default_avatar) {
            local EquObj = GameItem.Equipment(EquId);
            ChangeEquipment(EquObj);
        }
        //构造属性对象
        // Attribute = AttributeClass();
    }


    function SyncObjectBox() {
        //同步受击框 和 攻击框
        DamageBox = null;
        AttackBox = null;
        //皮肤同步受击框 武器同步攻击框
        foreach(AniObj in CurrentAni) {
            if (AniObj.Type == "skin") {
                local Info = AniObj.GetCurrentFrameInfo();
                if (Info.DamageBox.len() > 0) {
                    DamageBox = [];
                    foreach(Box in Info.DamageBox) {
                        local NewBox = [Box[0] + X, Box[1] + Y, Box[2] + Z, Box[3] + X, Box[4] + Y, Box[5] + Z];
                        DamageBox.append(NewBox);
                    }
                }
            } else if (AniObj.Type == "weapon") {
                local Info = AniObj.GetCurrentFrameInfo();
                if (Info.AttackBox.len() > 0) {
                    AttackBox = [];
                    foreach(Box in Info.AttackBox) {
                        local NewBox = [Box[0] + X, Box[1] + Y, Box[2] + Z, Box[3] + X, Box[4] + Y, Box[5] + Z];
                        AttackBox.append(NewBox);
                    }
                }
            }
        }
    }

    // function OnUpdate(dt) {
    //     //原始逻辑
    //     base.OnUpdate(dt);
    // }


    //设置Ani
    function SetAnimation(Ani) {
        //如果存在动画管理器则设置
        if (AnimationManager) AnimationManager.SetAnimation(Ani);
    }


    //切换装备
    function ChangeEquipment(Equ) {
        //如果当前装备槽已经有装备则移除
        if (this[Equ.SlotType]) {
            this[Equ.SlotType].OnWearEnd();
            this[Equ.SlotType] = null;
        }
        //将装备对象赋值给对应装备槽
        this[Equ.SlotType] = Equ;
        this[Equ.SlotType].OnWearStart();

        //如果是武器或者时装则同步动画
        if (Equ.SlotType == "weapon" || Equ.Type == "avatar") {
            AnimationManager.Init(Equ.SlotType);
        }
    }
    //切换装备列表
    function ChangeEquipmentList(EquList) {
        foreach(Equ in EquList) {
            ChangeEquipment(Equ);
        }
    }
}

//通过职业和装备列表来构造一个角色
GameObject.CreateCharacter <- function(Job = 0, EquIdList = []) {

    //构造角色
    local Character = GameObject.Character();
    Character.Init(Job);
    foreach(EquId in EquIdList) {
        if (EquId > 0) {
            local EquObj = GameItem.Equipment(EquId);
            Character.ChangeEquipment(EquObj);
        }
    }
    return Character;
}