5#include "sqrat/sqratVM.h" 
    7#include "../Util/AssetsDef.h" 
    8#include "../Util/Render/SMaterial.h" 
    9#include "../Component/LightComponent.h" 
   10#include "../Object/SScriptObject.h" 
   11#include "../Component/RenderComponent.h" 
   12#include "EngineCore.h" 
   17const char* CSEngineScript = 
"                            \n\ 
   18class CSEngineScript {                                            \n\ 
   19//CSEngineComponent                                                       \n\ 
   24function SetCSEngine(component) {                         \n\ 
   25    if(CSEngine != null) return;              \n\ 
   26        CSEngine = component;                                     \n\ 
   27        gameobject = CSEngine.GetGameObject();    \n\ 
   28        gameObject = CSEngine.GetGameObject();    \n\ 
   30function IsEnable(){                                              \n\ 
   31return CSEngine.IsEnable();                                       \n\ 
   34function SetEnable(enable) {                              \n\ 
   35        CSEngine.SetEnable(enable);                               \n\ 
   38function GetTransform() {                                         \n\ 
   39        return gameobject.GetTransform();                 \n\ 
   48#define COMPONENT_DEF(CLASSNAME) SQRComponentDef<CLASSNAME>(_SC(#CLASSNAME)) 
   49#define COMPONENT_DEF_WITH_SQNAME(CLASSNAME, SQNAME) SQRComponentDef<CLASSNAME>(_SC(#SQNAME)) 
   51ScriptMgr::ScriptMgr() = 
default;
 
   54ScriptMgr::~ScriptMgr() {
 
   56    sq_close(DefaultVM::Get());
 
   60void ScriptMgr::Init() {
 
   64    Sqrat::DefaultVM::Set(vm);
 
   67    sqstd_register_mathlib(vm);
 
   75void ScriptMgr::RegisterScript(
const std::string& script) {
 
   77    HSQUIRRELVM vm = DefaultVM::Get();
 
   80    if (!script.empty()) {
 
   81        Script compiledScript;
 
   82        compiledScript.CompileString(script);
 
   83        if (Sqrat::Error::Occurred(vm)) {
 
   84            SafeLog::Log((_SC(
"Compile Failed: ") + Error::Message(vm)).c_str());
 
   88        if (Sqrat::Error::Occurred(vm)) {
 
   89            SafeLog::Log((_SC(
"Run Failed: ") + Error::Message(vm)).c_str());
 
   92        compiledScript.Release();
 
   97SQInteger GetCustomComponentFunc(HSQUIRRELVM v) {
 
   98    SQInteger args = sq_gettop(v);
 
  100    if (args != 2) 
return 1;
 
  102    SGameObject* game_object = Var<SGameObject*>(v, 1).value;
 
  103    char* classname_str = Var<char*>(v, 2).value;
 
  104    auto customComponent = game_object->GetCustomComponent(classname_str);
 
  106    if (customComponent._type == OT_NULL) {
 
  110    sq_pushobject(v, customComponent);
 
  118void ScriptMgr::DefineClasses(HSQUIRRELVM vm) {
 
  123    SQRClassDef<SGameObject>(_SC(
"GameObject"), vm)
 
  124            .Func(_SC(
"Find"), &SGameObject::Find)
 
  125            .Func(_SC(
"GetTransform"), &SGameObject::GetTransform)
 
  126            .Func(_SC(
"IsEnable"), &SGameObject::GetIsEnable)
 
  127            .Func(_SC(
"SetEnable"), &SGameObject::SetIsEnable)
 
  128            .Func(_SC(
"SetName"), &SGameObject::SetName)
 
  129            .Func(_SC(
"GetName"), &SGameObject::GetName)
 
  130            .SquirrelFunc(_SC(
"GetClass"), GetCustomComponentFunc);
 
  143    SQRClassDef<TransformInterface>(_SC(
"Transform"), vm)
 
  144            .Var(_SC(
"position"), &TransformInterface::m_position)
 
  145            .Var(_SC(
"rotation"), &TransformInterface::m_rotation)
 
  146            .Var(_SC(
"scale"), &TransformInterface::m_scale);
 
  150            .Func(_SC(
"GetGameObject"), &CustomComponent::GetGameObject)
 
  151            .Func(_SC(
"IsEnable"), &CustomComponent::GetIsEnable)
 
  152            .Func(_SC(
"SetEnable"), &CustomComponent::SetIsEnable)
 
  153            .Func(_SC(
"Log"), &CustomComponent::Log);
 
  156            .Func(_SC(
"GetMaterial"), &RenderComponent::GetMaterial)
 
  157            .Func(_SC(
"SetEnable"), &RenderComponent::SetIsEnable)
 
  158            .Func(_SC(
"GetEnable"), &RenderComponent::GetIsEnable);
 
  162            .Func(_SC(
"SetLightType"), &LightComponent::SetLightType)
 
  163            .Func(_SC(
"SetColor"), &LightComponent::SetColor)
 
  164            .Func(_SC(
"GetColor"), &LightComponent::GetColor)
 
  165            .Func(_SC(
"SetDirection"), &LightComponent::SetDirection)
 
  166            .Func(_SC(
"GetDirection"), &LightComponent::GetDirection);
 
  169            .Func(_SC(
"SetTarget"), &CameraComponent::SetTarget)
 
  170            .Func(_SC(
"SetTargetVector"), &CameraComponent::SetTargetVector)
 
  171            .Func(_SC(
"SetUp"), &CameraComponent::SetUp)
 
  172            .Func(_SC(
"SetOrtho"), &CameraComponent::SetOrtho)
 
  173            .Func(_SC(
"SetZDepthRange"), &CameraComponent::SetZDepthRange)
 
  174            .Func(_SC(
"SetPerspective"), &CameraComponent::SetPerspective);
 
  179    SQRClassDef<vec2>(_SC(
"vec2"))
 
  180            .Var(_SC(
"x"), &vec2::x)
 
  181            .Var(_SC(
"y"), &vec2::y)
 
  182            .Func(_SC(
"Set"), &vec2::Set);
 
  184    SQRClassDef<vec3>(_SC(
"vec3"))
 
  185            .Var(_SC(
"x"), &vec3::x)
 
  186            .Var(_SC(
"y"), &vec3::y)
 
  187            .Var(_SC(
"z"), &vec3::z)
 
  188            .Func(_SC(
"Cross"), &vec3::Cross)
 
  189            .Func(_SC(
"Dot"), &vec3::Dot)
 
  190            .Func(_SC(
"Set"), &vec3::Set);
 
  192    SQRClassDef<vec4>(_SC(
"vec4"))
 
  193            .Var(_SC(
"x"), &vec4::x)
 
  194            .Var(_SC(
"y"), &vec4::y)
 
  195            .Var(_SC(
"z"), &vec4::z)
 
  196            .Var(_SC(
"w"), &vec4::w)
 
  197            .Func(_SC(
"Set"), &vec4::Set);
 
  199    SQRClassDef<Quaternion>(_SC(
"Quaternion"))
 
  200            .Var(_SC(
"x"), &Quaternion::x)
 
  201            .Var(_SC(
"y"), &Quaternion::y)
 
  202            .Var(_SC(
"z"), &Quaternion::z)
 
  203            .Var(_SC(
"w"), &Quaternion::w)
 
  204            .Func(_SC(
"Set"), &Quaternion::Set)
 
  205            .Func(_SC(
"Clone"), &Quaternion::Clone)
 
  206            .StaticFunc(_SC(
"AngleAxis"), &Quaternion::AngleAxis)
 
  207            .Func(_SC(
"Rotate"), &Quaternion::Rotate)
 
  208            .Func(_SC(
"Slerp"), &Quaternion::Slerp)
 
  209            .Func(_SC(
"ToEulerAngle"), &Quaternion::ToEulerAngle);
 
  211    SQRClassDef<SMaterial>(_SC(
"Material"))
 
  212            .Func(_SC(
"SetInt"), &SMaterial::SetInt)
 
  213            .Func(_SC(
"SetFloat"), &SMaterial::SetFloat)
 
  214            .Func(_SC(
"SetVec3"), &SMaterial::SetVec3)
 
  215            .Func(_SC(
"SetTexture"), &SMaterial::SetTexture);
 
  219void ScriptMgr::ReleaseSqratObject() {
 
  221    for (
auto obj : m_objects) {
 
  229void ScriptMgr::ReadScriptList() {
 
  230    auto assets = CORE->GetCore(
ResMgr)->GetAssetReferences(AssetMgr::TYPE::SCRIPT);
 
  233    RegisterScript(CSEngineScript);
 
  235    for (
const auto& asset : assets) {
 
  236        SResource::Create<SScriptObject>(asset);