#pragma once
#include "squirrel/SquirrelEx.h"
#include "Actor/Object/ActiveObject.h"

static ActiveObject *GetActiveObject(HSQUIRRELVM v)
{
    SQUserPointer ptr;
    sq_getuserpointer(v, 2, &ptr);
    ActiveObject *obj = (ActiveObject *)ptr;
    return obj;
}

static SQInteger SQR_SetSpeed(HSQUIRRELVM v)
{
    ActiveObject *obj = GetActiveObject(v);
    SQFloat speedX, speedY, speedZ;
    sq_getfloat(v, 3, &speedX);
    sq_getfloat(v, 4, &speedY);
    sq_getfloat(v, 5, &speedZ);
    obj->SetSpeed(VecSpeed3(speedX, speedY, speedZ));
    return 0;
}

static SQInteger SQR_GetSpeed(HSQUIRRELVM v)
{
    ActiveObject *obj = GetActiveObject(v);
    VecSpeed3 speed = obj->GetSpeed();
    sq_newtable(v);
    sq_pushstring(v, _SC("x"), -1);
    sq_pushfloat(v, speed.x);
    sq_newslot(v, 3, SQFalse);
    sq_pushstring(v, _SC("y"), -1);
    sq_pushfloat(v, speed.y);
    sq_newslot(v, 3, SQFalse);
    sq_pushstring(v, _SC("z"), -1);
    sq_pushfloat(v, speed.z);
    sq_newslot(v, 3, SQFalse);
    return 1;
}

static void RegisterActiveObjectNutApi(const SQChar *funcName, SQFUNCTION funcAddr, HSQUIRRELVM v)
{
    sq_pushroottable(v);
    sq_pushstring(v, funcName, -1);
    sq_newclosure(v, funcAddr, 0);
    sq_newslot(v, -3, false);
    sq_poptop(v);
}

static void RegisterActiveObject()
{
    SDL_Log("初始化 动态对象-松鼠脚本!");
    HSQUIRRELVM v = SquirrelEx::GetInstance().GetSquirrelVM();
    // 设置速度
    RegisterActiveObjectNutApi(_SC("sq_SetSpeed"), SQR_SetSpeed, v);
    RegisterActiveObjectNutApi(_SC("sq_GetSpeed"), SQR_GetSpeed, v);
}
