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

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

    //当前动画组
    CurrentAni = null;

    //等待Ani
    WaitingAni = null;
    //移动Ani
    MoveAni = null;
    //蹲下Ani
    SitAni = null;
    //受伤Ani
    DamageAni1 = null;
    DamageAni2 = null;
    //倒地Ani
    DownAni = null;
    //被击后退Ani
    OverturnAni = null;
    //跳跃Ani
    JumoAni = null;
    //跳跃攻击Ani
    JumpAttackAni = null;
    //站立Ani
    RestAni = null;
    //引导Ani
    ThrowAni1_1 = null;
    ThrowAni1_2 = null;
    ThrowAni2_1 = null;
    ThrowAni2_2 = null;
    ThrowAni3_1 = null;
    ThrowAni3_2 = null;
    ThrowAni4_1 = null;
    ThrowAni4_2 = null;
    //奔跑Ani
    DashAni = null;
    //奔跑攻击Ani
    DashAttackAni = null;
    //拾取Ani
    GetItemAni = null;
    //释放BUFFAni
    BuffAni = null;
    //普通攻击Ani
    AttackAni = null;

    //属性对象
    Attribute = null;


    function Init(Idx) {
        //初始化动画组
        CurrentAni = [];

        Info = sq_DeepCopy(AssetManager.CharacterInfoList[Idx]);
        base.Init(Info);

        // Util.PrintTable(Info);
        //初始化基础Ani
        InitBaseAni();

        //构造属性对象
        // 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) {
        //因为是Ani组所以要foreach调用
        //如果已经有Ani了
        if (CurrentAni) {
            foreach(AniObj in CurrentAni) {
                Removechild(AniObj);
            }
        }
        if (type(Ani) == "integer") {
            //TODO 进阶ANI
        } else {
            CurrentAni = Ani;
        }
        //重置Ani 并添加子对象
        foreach(AniObj in CurrentAni) {
            AniObj.Reset();
            Addchild(AniObj);
        }
    }


    function FormatAvatarAniImgPath(AniInfo, FormatValue1, FormatValue2) {
        for (local i = 0; i< AniInfo.Frame.len(); i++) {
            AniInfo.Frame[i].Img_Path = format(AniInfo.Frame[i].Img_Path, FormatValue1, FormatValue2);
        }
        return AniInfo;
    }

    function ReadAndSetAni(AniObj, Src) {
        //如果有这个标签Ani则初始化
        if (Info.rawin(Src)) Src = Info[Src];
        else return;

        //如果Ani组不存在就初始化数组
        if (this[AniObj] == null) this[AniObj] = [];

        foreach(Type in getconsttable().AvatarType) {
            //如果有时装就初始化Ani
            //除了皮肤 在没有的情况下初始化0
            if (Type == "skin") {
                local SkinAni;
                if (this[Type] == null) {
                    local BufInfo = sq_DeepCopy(ScriptData.GetAni(Src));
                    local Ao = {
                        ImgVariation = [0, 0],
                        ImgFormat = function(ImgPath) {
                            if (ImgVariation[0] > 0) {
                                local Pos = ImgPath.find("%04d");
                                ImgPath = ImgPath.slice(0, Pos) + "%02d%02d" + ImgPath.slice(Pos + 4);
                                return format(ImgPath, ImgVariation[0], ImgVariation[1]);
                            } else {
                                return format(ImgPath, ImgVariation[1]);
                            }
                        }
                    }
                    SkinAni = Animation(BufInfo, Ao);
                } else {
                    local AvaInfo = ScriptData.GetEquipment(this[Type]);
                    local JobInfo = AvaInfo["Ani_" + Info["job"]];
                    local BufInfo = sq_DeepCopy(ScriptData.GetAni(Src));
                    local Ao = {
                        ImgVariation = JobInfo["variation"],
                        ImgFormat = function(ImgPath) {
                            if (ImgVariation[0] > 0) {
                                local Pos = ImgPath.find("%04d");
                                ImgPath = ImgPath.slice(0, Pos) + "%02d%02d" + ImgPath.slice(Pos + 4);
                                return format(ImgPath, ImgVariation[0], ImgVariation[1]);
                            } else {
                                return format(ImgPath, ImgVariation[1]);
                            }
                        }
                    }
                    SkinAni = Animation(BufInfo, Ao);
                }
                //如果是皮肤Ani 绑定状态机 确保唯一性
                SkinAni.BindenvStateMachine(StateMachine);
                //将Ani类型设置为皮肤
                SkinAni.Type = "skin";
                //加入组
                this[AniObj].append(SkinAni);
            } else {
                if (this[Type] != null) {

                    local AvaInfo = ScriptData.GetEquipment(this[Type]);
                    local JobInfo = AvaInfo["Ani_" + Info["job"]];
                    foreach(_index, value in JobInfo["layer_variation"]) {
                        local BufInfo = sq_DeepCopy(ScriptData.GetAni(AvaInfo["DirPath"] + value["Path"] + Src.slice(Src.find("/"))));
                        local Ao = {
                            ImgVariation = JobInfo["variation"],
                            ImgFormat = function(ImgPath) {
                                return format(ImgPath, ImgVariation[0], ImgVariation[1]);
                            }
                        }
                        local AniBuf = Animation(BufInfo, Ao);
                        //设置Ani类型
                        AniBuf.Type = Type;
                        AniBuf.SetZOrder(value["Zorder"]);
                        this[AniObj].append(AniBuf);
                    }
                }
            }
        }
    }

    function ReadAndSetAttackAni() {
        local AttackAniArrSrc = Info["attack_motion"];
        AttackAni = array(AttackAniArrSrc.len());
        foreach(_Index, Path in AttackAniArrSrc) {
            local Src = Path;
            AttackAni[_Index] = [];
            foreach(Type in getconsttable().AvatarType) {
                //如果有时装就初始化Ani
                //除了皮肤 在没有的情况下初始化0
                if (Type == "skin") {
                    local SkinAni;
                    if (this[Type] == null) {
                        local BufInfo = sq_DeepCopy(ScriptData.GetAni(Src));
                        local Ao = {
                            ImgVariation = [0, 0],
                            ImgFormat = function(ImgPath) {
                                if (ImgVariation[0] > 0) {
                                    local Pos = ImgPath.find("%04d");
                                    ImgPath = ImgPath.slice(0, Pos) + "%02d%02d" + ImgPath.slice(Pos + 4);
                                    return format(ImgPath, ImgVariation[0], ImgVariation[1]);
                                } else {
                                    return format(ImgPath, ImgVariation[1]);
                                }
                            }
                        }
                        SkinAni = Animation(BufInfo, Ao);
                    } else {
                        local AvaInfo = ScriptData.GetEquipment(this[Type]);
                        local JobInfo = AvaInfo["Ani_" + Info["job"]];
                        local BufInfo = sq_DeepCopy(ScriptData.GetAni(Src));
                        local Ao = {
                            ImgVariation = JobInfo["variation"],
                            ImgFormat = function(ImgPath) {
                                return format(ImgPath, ImgVariation[0], ImgVariation[1]);
                            }
                        }
                        SkinAni = Animation(BufInfo, Ao);
                    }
                    //如果是皮肤Ani 绑定状态机 确保唯一性
                    SkinAni.BindenvStateMachine(StateMachine);
                    //将Ani类型设置为皮肤
                    SkinAni.Type = "skin";
                    //加入组
                    AttackAni[_Index].append(SkinAni);
                } else {
                    if (this[Type] != null) {

                        local AvaInfo = ScriptData.GetEquipment(this[Type]);
                        local JobInfo = AvaInfo["Ani_" + Info["job"]];
                        foreach(_index, value in JobInfo["layer_variation"]) {
                            local BufInfo = sq_DeepCopy(ScriptData.GetAni(AvaInfo["DirPath"] + value["Path"] + Src.slice(Src.find("/"))));
                            local Ao = {
                                ImgVariation = JobInfo["variation"],
                                ImgFormat = function(ImgPath) {
                                    return format(ImgPath, ImgVariation[0], ImgVariation[1]);
                                }
                            }
                            local AniBuf = Animation(BufInfo, Ao);
                            //设置Ani类型
                            AniBuf.Type = Type;
                            AniBuf.SetZOrder(value["Zorder"]);
                            AttackAni[_Index].append(AniBuf);
                        }
                    }
                }
            }
        }
    }

    function InitBaseAni() {
        //读取并设置 等待Ani
        ReadAndSetAni("WaitingAni", "waiting motion");
        //读取并设置 移动Ani
        ReadAndSetAni("MoveAni", "move motion");
        //读取并设置 蹲下Ani
        ReadAndSetAni("SitAni", "sit motion");
        //读取并设置 受伤Ani
        ReadAndSetAni("DamageAni1", "damage motion 1");
        //读取并设置 受伤Ani
        ReadAndSetAni("DamageAni2", "damage motion 2");
        //读取并设置 倒地Ani
        ReadAndSetAni("DownAni", "down motion");
        //读取并设置 被击后退Ani
        ReadAndSetAni("OverturnAni", "overturn motion");
        //读取并设置 跳跃Ani
        ReadAndSetAni("JumoAni", "jump motion");
        //读取并设置 跳跃攻击Ani
        ReadAndSetAni("JumpAttackAni", "jumpattack motion");
        //读取并设置 站立Ani
        ReadAndSetAni("RestAni", "rest motion");
        //读取并设置 引导Ani
        ReadAndSetAni("ThrowAni1_1", "throw motion 1-1");
        ReadAndSetAni("ThrowAni1_2", "throw motion 1-2");
        ReadAndSetAni("ThrowAni2_1", "throw motion 2-1");
        ReadAndSetAni("ThrowAni2_2", "throw motion 2-2");
        ReadAndSetAni("ThrowAni3_1", "throw motion 3-1");
        ReadAndSetAni("ThrowAni3_2", "throw motion 3-2");
        ReadAndSetAni("ThrowAni4_1", "throw motion 4-1");
        ReadAndSetAni("ThrowAni4_2", "throw motion 4-2");
        //读取并设置 奔跑Ani
        ReadAndSetAni("DashAni", "dash motion");
        //读取并设置 奔跑攻击Ani
        ReadAndSetAni("DashAttackAni", "dashattack motion");
        //读取并设置 拾取Ani
        ReadAndSetAni("GetItemAni", "getitem motion");
        //读取并设置 释放BUFFAni
        ReadAndSetAni("BuffAni", "buff motion");
        //读取并设置 AttackAni
        ReadAndSetAttackAni();

    }
}