/*
文件名:ActiveObjectClass.nut
路径:User/Object/ActiveObject/ActiveObjectClass.nut
创建日期:2024-11-29	23:05
文件用途:动态Obj对象
*/
class ActiveObject extends BaseObject {



    Info = null;
    //动画组
    AnimationArr = null;
    //攻击信息组
    AttackinfoArr = null;
    //当前动画
    CurrentAni = null;
    //当前攻击信息
    Attackinfo = null;
    //图层信息
    Layer = null;
    //状态机
    StateMachine = null;
    //状态信息包
    StateVar = null;
    //受击框
    DamageBox = null;
    //攻击框
    AttackBox = null;

    //队伍
    Team = 0;

    //各方向加速度
    XSpeed = 0;
    YSpeed = 0;
    ZSpeed = 0;



    function Init(Info) {
        //如果存在Ani 初始化所有Ani
        if ("animotion" in Info) {
            AnimationArr = [];
            foreach(_index, path in Info.animotion) {
                local AniBuf = Animation(path);
                // AniBuf.ShowBorder(true);
                AnimationArr.append(AniBuf);
            }
        }
        //如果存在Atk 初始化所有Atk
        if ("attackinfo" in Info) {
            AttackinfoArr = [];
            foreach(_index, path in Info.attackinfo) {
                local AtkBuf = AttackInfo(path);
                AttackinfoArr.append(AtkBuf);
            }
        }

        //动态对象 绑定状态机
        StateMachine = FiniteStateMachineClass();
        //增加状态信息包
        StateVar = {};
    }

    //设置Ani
    function SetAnimation(Ani) {
        //如果已经有Ani了
        if (CurrentAni) {
            Removechild(CurrentAni);
        }
        if (type(Ani) == "integer") {
            CurrentAni = AnimationArr[Ani];
        } else {
            CurrentAni = Ani;
        }
        //重置Ani
        CurrentAni.Reset();
        //绑定状态机
        CurrentAni.BindenvStateMachine(StateMachine);
        Addchild(CurrentAni);
    }

    //设置Atk
    function SetAttackinfo(Idx) {
        Attackinfo = AttackinfoArr[Idx];
    }

    /*
     * @函数作用: 设置状态
     * @参数 gState 将要设置的状态
     */
    function SetState(gState) {
        //调用状态机设定状态
        return StateMachine.ChangeState(gState);
    }

    /*
     * @函数作用: 获取状态
     */
    function GetState() {
        //调用状态机设定状态
        return StateMachine.State;
    }

    //同步碰撞框
    function SyncObjectBox() {
        //同步受击框 和 攻击框
        DamageBox = null;
        AttackBox = null;
        //当前有Ani存在则同步 此处为怪物和被动对象的碰撞框同步逻辑 角色类override
        if (CurrentAni) {
            local Info = CurrentAni.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);
                }
            }
            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 SetSpeed(Type, Value) {
        switch (Type) {
            case 0:
                XSpeed = Value;
                break;
            case 1:
                YSpeed = Value;
                break;
            case 2:
                ZSpeed = Value;
                break;
        }
    }

    /*
     * @函数作用: 设置附着速度
     */
    function SetXSpeedWithDirection(gSpeed) {
        if (Direction == 0)
            SetSpeed(0, -gSpeed);
        else
            SetSpeed(0, gSpeed);
    }

    //计算自身加速度
    function CalculateAcceleration(dt) {
        if (XSpeed != 0 || YSpeed != 0 || ZSpeed != 0) {
            MoveBy(XSpeed * dt * 0.001, YSpeed * dt * 0.001, ZSpeed * dt * 0.001);
        }
    }

    function OnUpdate(dt) {
        //同步碰撞框
        SyncObjectBox();
        //状态机
        StateMachine.OnUpdate(dt);
        //计算自身加速度
        CalculateAcceleration(dt);

        base.OnUpdate(dt);
    }

    //攻击到其他对象时
    function OnAttack(Damager) {

    }


    //销毁自身
    function DestroySelf() {
        getroottable().CurrentMap.RemoveObject(this);
        base.DestroySelf();
    }

    //基于自身召唤Obj
    function CreatePassiveObject(Id, Offset_X, Offset_Y, Offset_Z, Pack) {
        local objbuf = PassiveObject();
        objbuf.Init(Id);
        objbuf.SetPosition(X + Offset_X, Y + Offset_Y, Z + Offset_Z);
        objbuf.StateVar = Pack;
        objbuf.Team = Team;
        getroottable().CurrentMap.AddObject(objbuf);
    }
}