Major cleanup.

Moved to namespace
Cleanup includes
This commit is contained in:
Antoine Pilote
2021-07-11 18:56:44 -04:00
parent f77bef6b28
commit 0c6ed48bbd
174 changed files with 11772 additions and 11296 deletions

View File

@@ -5,27 +5,29 @@
#include "ScriptModule.h"
#include <iostream>
#include <wren.h>
namespace ScriptAPI
namespace Nuake
{
class EngineModule : public ScriptModule
namespace ScriptAPI
{
std::string ModuleName = "Engine";
std::string GetModuleName() override
class EngineModule : public ScriptModule
{
return "Engine";
}
std::string ModuleName = "Engine";
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("Log(_)", (void*)Log);
}
std::string GetModuleName() override
{
return "Engine";
}
static void Log(WrenVM* vm)
{
std::string msg = wrenGetSlotString(vm, 1);
Logger::Log(msg);
}
};
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("Log(_)", (void*)Log);
}
static void Log(WrenVM* vm)
{
std::string msg = wrenGetSlotString(vm, 1);
Logger::Log(msg);
}
};
}
}

View File

@@ -6,33 +6,34 @@
#include <iostream>
#include <wren.h>
namespace ScriptAPI
namespace Nuake
{
class EngineModule : public ScriptModule
namespace ScriptAPI
{
std::string ModuleName = "Engine";
std::string WrenAPI = "class Engine { \n"
"foreign static Log(msg) \n"
"}"
"";
std::string GetModuleName() override
class EngineModule : public ScriptModule
{
return "Engine";
}
std::string ModuleName = "Engine";
std::string WrenAPI = "class Engine { \n"
"foreign static Log(msg) \n"
"}"
"";
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("Log(_)", (void*)Log);
WrenInterpretResult result = wrenInterpret(vm, "main", WrenAPI.c_str());
}
static void Log(WrenVM* vm)
{
std::string msg = wrenGetSlotString(vm, 1);
Logger::Log(msg);
}
};
std::string GetModuleName() override
{
return "Engine";
}
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("Log(_)", (void*)Log);
WrenInterpretResult result = wrenInterpret(vm, "main", WrenAPI.c_str());
}
static void Log(WrenVM* vm)
{
std::string msg = wrenGetSlotString(vm, 1);
Logger::Log(msg);
}
};
}
}

View File

@@ -8,98 +8,99 @@
#include "../Core/Maths.h"
#include "../Core/Input.h"
namespace ScriptAPI
{
class InputModule : public ScriptModule
{
std::string GetModuleName() override
namespace Nuake {
namespace ScriptAPI {
class InputModule : public ScriptModule
{
return "Input";
}
std::string GetModuleName() override
{
return "Input";
}
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("GetMouseX()", GetMouseX);
RegisterMethod("GetMouseY()", (void*)GetMouseY);
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("GetMouseX()", GetMouseX);
RegisterMethod("GetMouseY()", (void*)GetMouseY);
RegisterMethod("IsKeyDown_(_)", (void*)IsKeyDown);
RegisterMethod("IsKeyPressed_(_)", (void*)IsKeyPressed);
RegisterMethod("IsKeyReleased_(_)", (void*)IsKeyReleased);
RegisterMethod("IsKeyDown_(_)", (void*)IsKeyDown);
RegisterMethod("IsKeyPressed_(_)", (void*)IsKeyPressed);
RegisterMethod("IsKeyReleased_(_)", (void*)IsKeyReleased);
RegisterMethod("IsMouseButtonDown_(_)", (void*)IsMouseButtonDown);
RegisterMethod("IsMouseButtonPressed_(_)", (void*)IsMouseButtonPressed);
RegisterMethod("IsMouseButtonReleased_(_)", (void*)IsMouseButtonReleased);
RegisterMethod("IsMouseButtonDown_(_)", (void*)IsMouseButtonDown);
RegisterMethod("IsMouseButtonPressed_(_)", (void*)IsMouseButtonPressed);
RegisterMethod("IsMouseButtonReleased_(_)", (void*)IsMouseButtonReleased);
RegisterMethod("HideMouse()", (void*)HideMouse);
RegisterMethod("ShowMouse()", (void*)ShowMouse);
RegisterMethod("IsMouseHidden()", (void*)IsMouseHidden);
}
RegisterMethod("HideMouse()", (void*)HideMouse);
RegisterMethod("ShowMouse()", (void*)ShowMouse);
RegisterMethod("IsMouseHidden()", (void*)IsMouseHidden);
}
static void GetMouseX(WrenVM* vm)
{
wrenSetSlotDouble(vm, 0, Input::GetMouseX());
}
static void GetMouseX(WrenVM* vm)
{
wrenSetSlotDouble(vm, 0, Input::GetMouseX());
}
static void GetMouseY(WrenVM* vm)
{
wrenSetSlotDouble(vm, 0, Input::GetMouseY());
}
static void GetMouseY(WrenVM* vm)
{
wrenSetSlotDouble(vm, 0, Input::GetMouseY());
}
static void IsMouseButtonDown(WrenVM* vm)
{
int key = wrenGetSlotDouble(vm, 1);
bool result = Input::IsMouseButtonDown(key);
wrenSetSlotBool(vm, 0, result);
}
static void IsMouseButtonDown(WrenVM* vm)
{
int key = wrenGetSlotDouble(vm, 1);
bool result = Input::IsMouseButtonDown(key);
wrenSetSlotBool(vm, 0, result);
}
static void IsMouseButtonPressed(WrenVM* vm)
{
int key = (int)wrenGetSlotDouble(vm, 1);
bool result = Input::IsMouseButtonPressed(key);
wrenSetSlotBool(vm, 0, result);
}
static void IsMouseButtonPressed(WrenVM* vm)
{
int key = (int)wrenGetSlotDouble(vm, 1);
bool result = Input::IsMouseButtonPressed(key);
wrenSetSlotBool(vm, 0, result);
}
static void IsMouseButtonReleased(WrenVM* vm)
{
int key = wrenGetSlotDouble(vm, 1);
bool result = Input::IsMouseButtonReleased(key);
wrenSetSlotBool(vm, 0, result);
}
static void IsMouseButtonReleased(WrenVM* vm)
{
int key = wrenGetSlotDouble(vm, 1);
bool result = Input::IsMouseButtonReleased(key);
wrenSetSlotBool(vm, 0, result);
}
static void IsKeyDown(WrenVM* vm)
{
int key = wrenGetSlotDouble(vm, 1);
bool result = Input::IsKeyDown(key);
wrenSetSlotBool(vm, 0, result);
}
static void IsKeyDown(WrenVM* vm)
{
int key = wrenGetSlotDouble(vm, 1);
bool result = Input::IsKeyDown(key);
wrenSetSlotBool(vm, 0, result);
}
static void IsKeyPressed(WrenVM* vm)
{
int key = wrenGetSlotDouble(vm, 1);
bool result = Input::IsKeyPressed(key);
wrenSetSlotBool(vm, 0, result);
}
static void IsKeyPressed(WrenVM* vm)
{
int key = wrenGetSlotDouble(vm, 1);
bool result = Input::IsKeyPressed(key);
wrenSetSlotBool(vm, 0, result);
}
static void IsKeyReleased(WrenVM* vm)
{
int key = wrenGetSlotDouble(vm, 1);
bool result = Input::IsKeyReleased(key);
wrenSetSlotBool(vm, 0, result);
}
static void IsKeyReleased(WrenVM* vm)
{
int key = wrenGetSlotDouble(vm, 1);
bool result = Input::IsKeyReleased(key);
wrenSetSlotBool(vm, 0, result);
}
static void HideMouse(WrenVM* vm)
{
Input::HideMouse();
}
static void HideMouse(WrenVM* vm)
{
Input::HideMouse();
}
static void ShowMouse(WrenVM* vm)
{
Input::ShowMouse();
}
static void ShowMouse(WrenVM* vm)
{
Input::ShowMouse();
}
static void IsMouseHidden(WrenVM* vm)
{
wrenSetSlotBool(vm, 0, Input::IsMouseHidden());
}
};
static void IsMouseHidden(WrenVM* vm)
{
wrenSetSlotBool(vm, 0, Input::IsMouseHidden());
}
};
}
}

View File

@@ -7,86 +7,84 @@
#include <wren.h>
#include "../Core/Maths.h"
namespace ScriptAPI
{
class MathModule : public ScriptModule
{
std::string ModuleName = "Math";
std::string GetModuleName() override
namespace Nuake {
namespace ScriptAPI {
class MathModule : public ScriptModule
{
return "Math";
}
std::string ModuleName = "Math";
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("Sqrt_(_,_,_)", (void*)Sqrt);
RegisterMethod("Sin(_)", (void*)Sin);
RegisterMethod("Cos(_)", (void*)Cos);
RegisterMethod("Radians(_)", (void*)Radians);
RegisterMethod("Degrees(_)", (void*)Degrees);
RegisterMethod("Cross_(_,_,_,_,_,_)", (void*)Cross);
}
std::string GetModuleName() override
{
return "Math";
}
static void Sqrt(WrenVM* vm)
{
float x = wrenGetSlotDouble(vm, 1);
float y = wrenGetSlotDouble(vm, 2);
float z = wrenGetSlotDouble(vm, 3);
float result = glm::sqrt((x * x) + (y * y) + (z * z));
wrenSetSlotDouble(vm, 0, result);
}
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("Sqrt_(_,_,_)", (void*)Sqrt);
RegisterMethod("Sin(_)", (void*)Sin);
RegisterMethod("Cos(_)", (void*)Cos);
RegisterMethod("Radians(_)", (void*)Radians);
RegisterMethod("Degrees(_)", (void*)Degrees);
RegisterMethod("Cross_(_,_,_,_,_,_)", (void*)Cross);
}
static void Cos(WrenVM* vm)
{
float d = wrenGetSlotDouble(vm, 1);
float result = glm::cos(d);
wrenSetSlotDouble(vm, 0, result);
}
static void Sqrt(WrenVM* vm)
{
float x = wrenGetSlotDouble(vm, 1);
float y = wrenGetSlotDouble(vm, 2);
float z = wrenGetSlotDouble(vm, 3);
float result = glm::sqrt((x * x) + (y * y) + (z * z));
wrenSetSlotDouble(vm, 0, result);
}
static void Sin(WrenVM* vm)
{
float d = wrenGetSlotDouble(vm, 1);
float result = glm::sin(d);
wrenSetSlotDouble(vm, 0, result);
}
static void Cos(WrenVM* vm)
{
float d = wrenGetSlotDouble(vm, 1);
float result = glm::cos(d);
wrenSetSlotDouble(vm, 0, result);
}
static void Radians(WrenVM* vm)
{
float d = wrenGetSlotDouble(vm, 1);
float result = glm::radians(d);
wrenSetSlotDouble(vm, 0, result);
}
static void Sin(WrenVM* vm)
{
float d = wrenGetSlotDouble(vm, 1);
float result = glm::sin(d);
wrenSetSlotDouble(vm, 0, result);
}
static void Degrees(WrenVM* vm)
{
float d = wrenGetSlotDouble(vm, 1);
float result = glm::degrees(d);
wrenSetSlotDouble(vm, 0, result);
}
static void Radians(WrenVM* vm)
{
float d = wrenGetSlotDouble(vm, 1);
float result = glm::radians(d);
wrenSetSlotDouble(vm, 0, result);
}
static void Cross(WrenVM* vm)
{
Vector3 v1 = Vector3(wrenGetSlotDouble(vm, 1),
wrenGetSlotDouble(vm, 2),
wrenGetSlotDouble(vm, 3));
Vector3 v2 = Vector3(wrenGetSlotDouble(vm, 4),
wrenGetSlotDouble(vm, 5),
wrenGetSlotDouble(vm, 6));
Vector3 cross = glm::cross(v1, v2);
static void Degrees(WrenVM* vm)
{
float d = wrenGetSlotDouble(vm, 1);
float result = glm::degrees(d);
wrenSetSlotDouble(vm, 0, result);
}
static void Cross(WrenVM* vm)
{
Vector3 v1 = Vector3(wrenGetSlotDouble(vm, 1),
wrenGetSlotDouble(vm, 2),
wrenGetSlotDouble(vm, 3));
Vector3 v2 = Vector3(wrenGetSlotDouble(vm, 4),
wrenGetSlotDouble(vm, 5),
wrenGetSlotDouble(vm, 6));
Vector3 cross = glm::cross(v1, v2);
wrenSetSlotNewList(vm, 0);
wrenSetSlotDouble(vm, 1, cross.x);
wrenSetSlotDouble(vm, 2, cross.y);
wrenSetSlotDouble(vm, 3, cross.z);
wrenInsertInList(vm, 0, -1, 1);
wrenInsertInList(vm, 0, -1, 2);
wrenInsertInList(vm, 0, -1, 3);
}
};
wrenSetSlotNewList(vm, 0);
wrenSetSlotDouble(vm, 1, cross.x);
wrenSetSlotDouble(vm, 2, cross.y);
wrenSetSlotDouble(vm, 3, cross.z);
wrenInsertInList(vm, 0, -1, 1);
wrenInsertInList(vm, 0, -1, 2);
wrenInsertInList(vm, 0, -1, 3);
}
};
}
}

View File

@@ -7,57 +7,59 @@
#include <iostream>
#include <wren.h>
#include "../Core/Physics/PhysicsManager.h"
namespace ScriptAPI
{
class PhysicsModule : public ScriptModule
{
std::string ModuleName = "Engine";
std::string GetModuleName() override
namespace Nuake {
namespace ScriptAPI {
class PhysicsModule : public ScriptModule
{
return "Physics";
}
std::string ModuleName = "Engine";
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("Raycast_(_,_,_,_,_,_)", (void*)Raycast);
}
std::string GetModuleName() override
{
return "Physics";
}
static void Raycast(WrenVM* vm)
{
Vector3 v1 = Vector3(wrenGetSlotDouble(vm, 1),
wrenGetSlotDouble(vm, 2),
wrenGetSlotDouble(vm, 3));
Vector3 v2 = Vector3(wrenGetSlotDouble(vm, 4),
wrenGetSlotDouble(vm, 5),
wrenGetSlotDouble(vm, 6));
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("Raycast_(_,_,_,_,_,_)", (void*)Raycast);
}
RaycastResult result = PhysicsManager::Get()->Raycast(v1, v2);
wrenSetSlotNewList(vm, 0);
static void Raycast(WrenVM* vm)
{
Vector3 v1 = Vector3(wrenGetSlotDouble(vm, 1),
wrenGetSlotDouble(vm, 2),
wrenGetSlotDouble(vm, 3));
Vector3 v2 = Vector3(wrenGetSlotDouble(vm, 4),
wrenGetSlotDouble(vm, 5),
wrenGetSlotDouble(vm, 6));
// Returns a list with 3 vectors placed sequentially
// should be reconstructed in the wren side.
wrenSetSlotDouble(vm, 1, result.LocalPoint.x);
wrenSetSlotDouble(vm, 2, result.LocalPoint.y);
wrenSetSlotDouble(vm, 3, result.LocalPoint.z);
RaycastResult result = PhysicsManager::Get()->Raycast(v1, v2);
wrenSetSlotNewList(vm, 0);
wrenSetSlotDouble(vm, 4, result.WorldPoint.x);
wrenSetSlotDouble(vm, 5, result.WorldPoint.y);
wrenSetSlotDouble(vm, 6, result.WorldPoint.z);
// Returns a list with 3 vectors placed sequentially
// should be reconstructed in the wren side.
wrenSetSlotDouble(vm, 1, result.LocalPoint.x);
wrenSetSlotDouble(vm, 2, result.LocalPoint.y);
wrenSetSlotDouble(vm, 3, result.LocalPoint.z);
wrenSetSlotDouble(vm, 7, result.Normal.x);
wrenSetSlotDouble(vm, 8, result.Normal.y);
wrenSetSlotDouble(vm, 9, result.Normal.z);
wrenSetSlotDouble(vm, 4, result.WorldPoint.x);
wrenSetSlotDouble(vm, 5, result.WorldPoint.y);
wrenSetSlotDouble(vm, 6, result.WorldPoint.z);
wrenInsertInList(vm, 0, -1, 1);
wrenInsertInList(vm, 0, -1, 2);
wrenInsertInList(vm, 0, -1, 3);
wrenInsertInList(vm, 0, -1, 4);
wrenInsertInList(vm, 0, -1, 5);
wrenInsertInList(vm, 0, -1, 6);
wrenInsertInList(vm, 0, -1, 7);
wrenInsertInList(vm, 0, -1, 8);
wrenInsertInList(vm, 0, -1, 9);
}
};
wrenSetSlotDouble(vm, 7, result.Normal.x);
wrenSetSlotDouble(vm, 8, result.Normal.y);
wrenSetSlotDouble(vm, 9, result.Normal.z);
wrenInsertInList(vm, 0, -1, 1);
wrenInsertInList(vm, 0, -1, 2);
wrenInsertInList(vm, 0, -1, 3);
wrenInsertInList(vm, 0, -1, 4);
wrenInsertInList(vm, 0, -1, 5);
wrenInsertInList(vm, 0, -1, 6);
wrenInsertInList(vm, 0, -1, 7);
wrenInsertInList(vm, 0, -1, 8);
wrenInsertInList(vm, 0, -1, 9);
}
};
}
}

View File

@@ -17,294 +17,295 @@
#include <src/Scene/Components/TriggerZone.h>
#include <src/Scene/Components/BSPBrushComponent.h>
namespace ScriptAPI
{
class SceneModule : public ScriptModule
{
std::string GetModuleName() override
namespace Nuake {
namespace ScriptAPI {
class SceneModule : public ScriptModule
{
return "Scene";
}
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("GetEntityID(_)", (void*)GetEntity);
RegisterMethod("EntityHasComponent(_,_)", (void*)EntityHasComponent);
RegisterMethod("GetTranslation_(_)", (void*)GetTranslation);
RegisterMethod("SetTranslation_(_,_,_,_)", (void*)SetTranslation);
RegisterMethod("SetLightIntensity_(_,_)", (void*)SetLightIntensity);
RegisterMethod("GetLightIntensity_(_)", (void*)GetLightIntensity);
RegisterMethod("GetScript_(_)", (void*)GetScript);
RegisterMethod("SetCameraDirection_(_,_,_,_)", (void*)SetCameraDirection);
RegisterMethod("GetCameraDirection_(_)", (void*)GetCameraDirection);
RegisterMethod("GetCameraRight_(_)", (void*)GetCameraRight);
RegisterMethod("MoveAndSlide_(_,_,_,_)", (void*)MoveAndSlide);
RegisterMethod("IsCharacterControllerOnGround_(_)", (void*)IsCharacterControllerOnGround);
RegisterMethod("TriggerGetOverlappingBodyCount_(_)", (void*)TriggerGetOverlappingBodyCount);
RegisterMethod("TriggerGetOverlappingBodies_(_)", (void*)TriggerGetOverlappingBodies);
RegisterMethod("BrushGetTargets_(_)", (void*)BrushGetTargets);
RegisterMethod("BrushGetTargetsCount_(_)", (void*)BrushGetTargetsCount);
}
static void GetEntity(WrenVM* vm)
{
std::string name = wrenGetSlotString(vm, 1);
int handle = Engine::GetCurrentScene()->GetEntity(name).GetHandle();
wrenSetSlotDouble(vm, 0, handle);
}
static void EntityHasComponent(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
std::string name = wrenGetSlotString(vm, 2);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
if (name == "Transform")
std::string GetModuleName() override
{
bool result = ent.HasComponent<TransformComponent>();
return "Scene";
}
void RegisterModule(WrenVM* vm) override
{
RegisterMethod("GetEntityID(_)", (void*)GetEntity);
RegisterMethod("EntityHasComponent(_,_)", (void*)EntityHasComponent);
RegisterMethod("GetTranslation_(_)", (void*)GetTranslation);
RegisterMethod("SetTranslation_(_,_,_,_)", (void*)SetTranslation);
RegisterMethod("SetLightIntensity_(_,_)", (void*)SetLightIntensity);
RegisterMethod("GetLightIntensity_(_)", (void*)GetLightIntensity);
RegisterMethod("GetScript_(_)", (void*)GetScript);
RegisterMethod("SetCameraDirection_(_,_,_,_)", (void*)SetCameraDirection);
RegisterMethod("GetCameraDirection_(_)", (void*)GetCameraDirection);
RegisterMethod("GetCameraRight_(_)", (void*)GetCameraRight);
RegisterMethod("MoveAndSlide_(_,_,_,_)", (void*)MoveAndSlide);
RegisterMethod("IsCharacterControllerOnGround_(_)", (void*)IsCharacterControllerOnGround);
RegisterMethod("TriggerGetOverlappingBodyCount_(_)", (void*)TriggerGetOverlappingBodyCount);
RegisterMethod("TriggerGetOverlappingBodies_(_)", (void*)TriggerGetOverlappingBodies);
RegisterMethod("BrushGetTargets_(_)", (void*)BrushGetTargets);
RegisterMethod("BrushGetTargetsCount_(_)", (void*)BrushGetTargetsCount);
}
static void GetEntity(WrenVM* vm)
{
std::string name = wrenGetSlotString(vm, 1);
int handle = Engine::GetCurrentScene()->GetEntity(name).GetHandle();
wrenSetSlotDouble(vm, 0, handle);
}
static void EntityHasComponent(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
std::string name = wrenGetSlotString(vm, 2);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
if (name == "Transform")
{
bool result = ent.HasComponent<TransformComponent>();
wrenSetSlotBool(vm, 0, result);
}
if (name == "Light")
{
bool result = ent.HasComponent<LightComponent>();
wrenSetSlotBool(vm, 0, result);
}
if (name == "QuakeMap")
{
bool result = ent.HasComponent<QuakeMapComponent>();
wrenSetSlotBool(vm, 0, result);
}
if (name == "CharacterController")
{
bool result = ent.HasComponent<CharacterControllerComponent>();
wrenSetSlotBool(vm, 0, result);
}
if (name == "Camera")
{
bool result = ent.HasComponent<CameraComponent>();
wrenSetSlotBool(vm, 0, result);
}
if (name == "Script")
{
bool result = ent.HasComponent<CameraComponent>();
wrenSetSlotBool(vm, 0, result);
}
if (name == "Rigidbody")
{
bool result = ent.HasComponent<RigidBodyComponent>();
wrenSetSlotBool(vm, 0, result);
}
if (name == "Script") {
bool result = ent.HasComponent<WrenScriptComponent>();
wrenSetSlotBool(vm, 0, result);
}
if (name == "Brush") {
bool result = ent.HasComponent<BSPBrushComponent>();
wrenSetSlotBool(vm, 0, result);
}
}
static void GetScript(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
if (ent.HasComponent<WrenScriptComponent>())
{
wrenSetSlotHandle(vm, 0, ent.GetComponent<WrenScriptComponent>().WrenScript->m_Instance);
}
}
static void SetLightIntensity(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
float intensity = wrenGetSlotDouble(vm, 2);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& light = ent.GetComponent<LightComponent>();
light.Strength = intensity;
}
static void GetLightIntensity(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
float intensity = wrenGetSlotDouble(vm, 2);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& light = ent.GetComponent<LightComponent>();
wrenSetSlotDouble(vm, 0, light.Strength);
}
static void SetCameraDirection(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
float x = wrenGetSlotDouble(vm, 2);
float y = wrenGetSlotDouble(vm, 3);
float z = wrenGetSlotDouble(vm, 4);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& cam = ent.GetComponent<CameraComponent>();
cam.CameraInstance->SetDirection(Vector3(x, y, z));
}
static void GetCameraDirection(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& cam = ent.GetComponent<CameraComponent>();
Vector3 dir = cam.CameraInstance->GetDirection();
wrenEnsureSlots(vm, 4);
// set the slots
// Fill the list
wrenSetSlotNewList(vm, 0);
wrenSetSlotDouble(vm, 1, dir.x);
wrenSetSlotDouble(vm, 2, dir.y);
wrenSetSlotDouble(vm, 3, dir.z);
wrenInsertInList(vm, 0, -1, 1);
wrenInsertInList(vm, 0, -1, 2);
wrenInsertInList(vm, 0, -1, 3);
}
static void GetCameraRight(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& cam = ent.GetComponent<CameraComponent>();
Vector3 right = cam.CameraInstance->cameraRight;
// set the slots
wrenSetSlotDouble(vm, 1, right.x);
wrenSetSlotDouble(vm, 2, right.y);
wrenSetSlotDouble(vm, 3, right.z);
// Fill the list
wrenSetSlotNewList(vm, 0);
wrenInsertInList(vm, 0, 0, 1);
wrenInsertInList(vm, 0, 1, 2);
wrenInsertInList(vm, 0, 2, 3);
}
static void IsCharacterControllerOnGround(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& characterController = ent.GetComponent<CharacterControllerComponent>();
bool result = characterController.CharacterController->IsOnGround;
wrenSetSlotBool(vm, 0, result);
}
if (name == "Light")
static void MoveAndSlide(WrenVM* vm)
{
bool result = ent.HasComponent<LightComponent>();
wrenSetSlotBool(vm, 0, result);
int handle = wrenGetSlotDouble(vm, 1);
float x = wrenGetSlotDouble(vm, 2);
float y = wrenGetSlotDouble(vm, 3);
float z = wrenGetSlotDouble(vm, 4);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& characterController = ent.GetComponent<CharacterControllerComponent>();
characterController.CharacterController->MoveAndSlide(Vector3(x, y, z));
}
if (name == "QuakeMap")
static void GetTranslation(WrenVM* vm)
{
bool result = ent.HasComponent<QuakeMapComponent>();
wrenSetSlotBool(vm, 0, result);
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& transform = ent.GetComponent<TransformComponent>();
// set the slots
wrenSetSlotDouble(vm, 1, transform.Translation.x);
wrenSetSlotDouble(vm, 2, transform.Translation.y);
wrenSetSlotDouble(vm, 3, transform.Translation.z);
// Fill the list
wrenSetSlotNewList(vm, 0);
wrenInsertInList(vm, 0, 0, 1);
wrenInsertInList(vm, 0, 1, 2);
wrenInsertInList(vm, 0, 2, 3);
}
if (name == "CharacterController")
static void SetTranslation(WrenVM* vm)
{
bool result = ent.HasComponent<CharacterControllerComponent>();
wrenSetSlotBool(vm, 0, result);
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& transform = ent.GetComponent<TransformComponent>();
// set the slots
float x = wrenGetSlotDouble(vm, 2);
float y = wrenGetSlotDouble(vm, 3);
float z = wrenGetSlotDouble(vm, 4);
transform.Translation = Vector3(x, y, z);
}
if (name == "Camera")
static void TriggerGetOverlappingBodyCount(WrenVM* vm)
{
bool result = ent.HasComponent<CameraComponent>();
wrenSetSlotBool(vm, 0, result);
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
TriggerZone trigger = ent.GetComponent<TriggerZone>();
int count = trigger.GetOverLappingCount();
wrenSetSlotDouble(vm, 0, count);
}
if (name == "Script")
static void TriggerGetOverlappingBodies(WrenVM* vm)
{
bool result = ent.HasComponent<CameraComponent>();
wrenSetSlotBool(vm, 0, result);
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
TriggerZone trigger = ent.GetComponent<TriggerZone>();
std::vector<Entity> entities = trigger.GetOverlappingBodies();
wrenEnsureSlots(vm, entities.size());
wrenSetSlotNewList(vm, 0);
int idx = 1;
for (auto& e : entities)
{
wrenSetSlotDouble(vm, idx, e.GetHandle());
wrenInsertInList(vm, 0, -1, idx);
idx++;
}
}
if (name == "Rigidbody")
static void BrushGetTargets(WrenVM* vm)
{
bool result = ent.HasComponent<RigidBodyComponent>();
wrenSetSlotBool(vm, 0, result);
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
BSPBrushComponent& brush = ent.GetComponent<BSPBrushComponent>();
wrenEnsureSlots(vm, brush.Targets.size());
wrenSetSlotNewList(vm, 0);
int idx = 1;
for (auto& e : brush.Targets)
{
wrenSetSlotDouble(vm, idx, e.GetHandle());
wrenInsertInList(vm, 0, -1, idx);
idx++;
}
}
if (name == "Script") {
bool result = ent.HasComponent<WrenScriptComponent>();
wrenSetSlotBool(vm, 0, result);
static void BrushGetTargetsCount(WrenVM* vm) {
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
BSPBrushComponent& brush = ent.GetComponent<BSPBrushComponent>();
wrenSetSlotDouble(vm, 0, brush.Targets.size());
}
if (name == "Brush") {
bool result = ent.HasComponent<BSPBrushComponent>();
wrenSetSlotBool(vm, 0, result);
}
}
static void GetScript(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
if (ent.HasComponent<WrenScriptComponent>())
{
wrenSetSlotHandle(vm, 0, ent.GetComponent<WrenScriptComponent>().WrenScript->m_Instance);
}
}
static void SetLightIntensity(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
float intensity = wrenGetSlotDouble(vm, 2);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& light = ent.GetComponent<LightComponent>();
light.Strength = intensity;
}
static void GetLightIntensity(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
float intensity = wrenGetSlotDouble(vm, 2);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& light = ent.GetComponent<LightComponent>();
wrenSetSlotDouble(vm, 0, light.Strength);
}
static void SetCameraDirection(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
float x = wrenGetSlotDouble(vm, 2);
float y = wrenGetSlotDouble(vm, 3);
float z = wrenGetSlotDouble(vm, 4);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& cam = ent.GetComponent<CameraComponent>();
cam.CameraInstance->SetDirection(Vector3(x, y, z));
}
static void GetCameraDirection(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& cam = ent.GetComponent<CameraComponent>();
Vector3 dir = cam.CameraInstance->GetDirection();
wrenEnsureSlots(vm, 4);
// set the slots
// Fill the list
wrenSetSlotNewList(vm, 0);
wrenSetSlotDouble(vm, 1, dir.x);
wrenSetSlotDouble(vm, 2, dir.y);
wrenSetSlotDouble(vm, 3, dir.z);
wrenInsertInList(vm, 0, -1, 1);
wrenInsertInList(vm, 0, -1, 2);
wrenInsertInList(vm, 0, -1, 3);
}
static void GetCameraRight(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& cam = ent.GetComponent<CameraComponent>();
Vector3 right = cam.CameraInstance->cameraRight;
// set the slots
wrenSetSlotDouble(vm, 1, right.x);
wrenSetSlotDouble(vm, 2, right.y);
wrenSetSlotDouble(vm, 3, right.z);
// Fill the list
wrenSetSlotNewList(vm, 0);
wrenInsertInList(vm, 0, 0, 1);
wrenInsertInList(vm, 0, 1, 2);
wrenInsertInList(vm, 0, 2, 3);
}
static void IsCharacterControllerOnGround(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& characterController = ent.GetComponent<CharacterControllerComponent>();
bool result = characterController.CharacterController->IsOnGround;
wrenSetSlotBool(vm, 0, result);
}
static void MoveAndSlide(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
float x = wrenGetSlotDouble(vm, 2);
float y = wrenGetSlotDouble(vm, 3);
float z = wrenGetSlotDouble(vm, 4);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& characterController = ent.GetComponent<CharacterControllerComponent>();
characterController.CharacterController->MoveAndSlide(Vector3(x, y, z));
}
static void GetTranslation(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& transform = ent.GetComponent<TransformComponent>();
// set the slots
wrenSetSlotDouble(vm, 1, transform.Translation.x);
wrenSetSlotDouble(vm, 2, transform.Translation.y);
wrenSetSlotDouble(vm, 3, transform.Translation.z);
// Fill the list
wrenSetSlotNewList(vm, 0);
wrenInsertInList(vm, 0, 0, 1);
wrenInsertInList(vm, 0, 1, 2);
wrenInsertInList(vm, 0, 2, 3);
}
static void SetTranslation(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& transform = ent.GetComponent<TransformComponent>();
// set the slots
float x = wrenGetSlotDouble(vm, 2);
float y = wrenGetSlotDouble(vm, 3);
float z = wrenGetSlotDouble(vm, 4);
transform.Translation = Vector3(x, y, z);
}
static void TriggerGetOverlappingBodyCount(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
TriggerZone trigger = ent.GetComponent<TriggerZone>();
int count = trigger.GetOverLappingCount();
wrenSetSlotDouble(vm, 0, count);
}
static void TriggerGetOverlappingBodies(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
TriggerZone trigger = ent.GetComponent<TriggerZone>();
std::vector<Entity> entities = trigger.GetOverlappingBodies();
wrenEnsureSlots(vm, entities.size() );
wrenSetSlotNewList(vm, 0);
int idx = 1;
for (auto& e : entities)
{
wrenSetSlotDouble(vm, idx, e.GetHandle());
wrenInsertInList(vm, 0, -1, idx);
idx++;
}
}
static void BrushGetTargets(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
BSPBrushComponent& brush = ent.GetComponent<BSPBrushComponent>();
wrenEnsureSlots(vm, brush.Targets.size());
wrenSetSlotNewList(vm, 0);
int idx = 1;
for (auto& e : brush.Targets)
{
wrenSetSlotDouble(vm, idx, e.GetHandle());
wrenInsertInList(vm, 0, -1, idx);
idx++;
}
}
static void BrushGetTargetsCount(WrenVM* vm) {
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
BSPBrushComponent& brush = ent.GetComponent<BSPBrushComponent>();
wrenSetSlotDouble(vm, 0, brush.Targets.size());
}
};
}
};
}
}

View File

@@ -2,26 +2,26 @@
#include <string>
#include <map>
struct WrenVM;
class ScriptModule
{
private:
std::map<std::string, void*> Methods = std::map<std::string, void*>();
std::string WrenAPI;
public:
std::string ModuleName;
virtual void RegisterModule(WrenVM*) = 0;
virtual std::string GetModuleName() = 0;
void RegisterMethod(const std::string& signature, void* ptr)
namespace Nuake {
class ScriptModule
{
Methods[signature] = ptr;
}
public:
std::string ModuleName;
virtual void RegisterModule(WrenVM*) = 0;
virtual std::string GetModuleName() = 0;
void* GetMethodPointer(const std::string& signature)
{
return Methods[signature];
}
void RegisterMethod(const std::string& signature, void* ptr)
{
Methods[signature] = ptr;
}
};
void* GetMethodPointer(const std::string& signature)
{
return Methods[signature];
}
private:
std::map<std::string, void*> Methods = std::map<std::string, void*>();
std::string WrenAPI;
};
}

View File

@@ -1,18 +0,0 @@
#include "ScriptLoader.h"
#include <windows.h>
#include <libloaderapi.h>
bool ScriptLoader::RegisterScript(ScriptableEntity* script)
{
return false;
}
bool ScriptLoader::LoadModule(const std::string& path)
{
return true;
}

View File

@@ -1,30 +0,0 @@
#pragma once
#include <map>
#include <string>
#include "../Scene/Entities/ScriptableEntity.h"
typedef void (ScriptModuleRegister)();
//#define X_INPUT_GET_STATE(name) DWORD WINAPI name(DWORD dwUserIndex, XINPUT_STATE* pState)
// Now we create a typedef for this function pointer
//typedef X_INPUT_GET_STATE(x_input_get_state);
extern "C"
{
class ScriptLoader
{
private:
static std::map<std::string, ScriptableEntity*> m_Scripts;
public:
static bool RegisterScript(ScriptableEntity* script);
static ScriptableEntity* GetScript(std::string script) {
return m_Scripts[script];
}
static bool LoadModule(const std::string& path);
};
}

View File

@@ -8,183 +8,186 @@
#include <src/Scripting/Modules/InputModule.h>
#include <src/Scripting/Modules/PhysicsModule.h>
WrenVM* ScriptingEngine::m_WrenVM;
namespace Nuake {
WrenVM* ScriptingEngine::m_WrenVM;
std::map<std::string, Ref<WrenScript>> ScriptingEngine::m_Scripts;
std::map<std::string, Ref<ScriptModule>> ScriptingEngine::Modules;
std::vector<std::string> ScriptingEngine::m_LoadedScripts;
std::map<std::string, Ref<WrenScript>> ScriptingEngine::m_Scripts;
std::map<std::string, Ref<ScriptModule>> ScriptingEngine::Modules;
std::vector<std::string> ScriptingEngine::m_LoadedScripts;
void errorFn(WrenVM* vm, WrenErrorType errorType,
const char* module, const int line,
const char* msg)
{
switch (errorType)
void errorFn(WrenVM* vm, WrenErrorType errorType,
const char* module, const int line,
const char* msg)
{
case WREN_ERROR_COMPILE:
{
std::string t = std::string(module)+ " line " + std::to_string(line) + ": "+ msg;
Logger::Log(t, CRITICAL);
Engine::ExitPlayMode();
} break;
case WREN_ERROR_STACK_TRACE:
{
std::string t = "Stack trace: " + std::string(module) + " line " + std::to_string(line) + ": " + msg;
Logger::Log(t, CRITICAL);
} break;
case WREN_ERROR_RUNTIME:
{
std::string t = "Script Runtime Error: " + std::string(msg);
Logger::Log(t, WARNING);
} break;
switch (errorType)
{
case WREN_ERROR_COMPILE:
{
std::string t = std::string(module) + " line " + std::to_string(line) + ": " + msg;
Logger::Log(t, CRITICAL);
Engine::ExitPlayMode();
} break;
case WREN_ERROR_STACK_TRACE:
{
std::string t = "Stack trace: " + std::string(module) + " line " + std::to_string(line) + ": " + msg;
Logger::Log(t, CRITICAL);
} break;
case WREN_ERROR_RUNTIME:
{
std::string t = "Script Runtime Error: " + std::string(msg);
Logger::Log(t, WARNING);
} break;
}
}
}
void writeFn(WrenVM* vm, const char* text)
{
printf("%s", text);
}
bool hasEnding(std::string const& fullString, std::string const& ending)
{
if (fullString.length() >= ending.length())
void writeFn(WrenVM* vm, const char* text)
{
return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
printf("%s", text);
}
else
bool hasEnding(std::string const& fullString, std::string const& ending)
{
if (fullString.length() >= ending.length())
{
return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
}
else
{
return false;
}
}
const std::string NuakeModulePrefix = "Nuake:";
WrenLoadModuleResult myLoadModule(WrenVM* vm, const char* name)
{
WrenLoadModuleResult result = { 0 };
std::string sname = std::string(name);
std::string path = "resources/" + std::string(name);
bool isAbsolute = false;
if (sname.rfind(NuakeModulePrefix, 0) == 0)
{
size_t prefixPosition = sname.find(NuakeModulePrefix);
sname.erase(prefixPosition, NuakeModulePrefix.length());
path = "resources/Scripts/" + std::string(sname);
}
else
{
path = FileSystem::Root + name;
isAbsolute = true;
}
if (!hasEnding(path, ".wren"))
path += ".wren";
std::string str = FileSystem::ReadFile(path, true);
char* c = strcpy(new char[str.length() + 1], str.c_str());
result.source = c;
return result;
}
Ref<WrenScript> ScriptingEngine::RegisterScript(const std::string& path, const std::string& mod)
{
// Check if scripts has already been loaded.
// You can't import the same module twice, otherwise, compile error.
if (m_Scripts.find(path) == m_Scripts.end())
{
std::string query = "import \"" + path + "\" for " + mod;
wrenInterpret(m_WrenVM, "main", query.c_str());
}
return CreateRef<WrenScript>(path, mod);
}
// Useful to check if a script has been imported, importing a script
// twice gives a compile error.
bool ScriptingEngine::IsScriptImported(const std::string& path)
{
for (auto& script : m_LoadedScripts)
{
if (script == path)
return true;
}
return false;
}
}
const std::string NuakeModulePrefix = "Nuake:";
WrenLoadModuleResult myLoadModule(WrenVM* vm, const char* name)
{
WrenLoadModuleResult result = { 0 };
std::string sname = std::string(name);
std::string path = "resources/" + std::string(name);
bool isAbsolute = false;
if (sname.rfind(NuakeModulePrefix, 0) == 0)
void ScriptingEngine::ImportScript(const std::string& path)
{
size_t prefixPosition = sname.find(NuakeModulePrefix);
sname.erase(prefixPosition, NuakeModulePrefix.length());
path = "resources/Scripts/" + std::string(sname);
}
else
{
path = FileSystem::Root + name;
isAbsolute = true;
if (IsScriptImported(path))
return;
m_LoadedScripts.push_back(path);
}
if (!hasEnding(path, ".wren"))
path += ".wren";
std::string str = FileSystem::ReadFile(path, true);
char* c = strcpy(new char[str.length() + 1], str.c_str());
result.source = c;
return result;
}
Ref<WrenScript> ScriptingEngine::RegisterScript(const std::string& path, const std::string& mod)
{
// Check if scripts has already been loaded.
// You can't import the same module twice, otherwise, compile error.
if (m_Scripts.find(path) == m_Scripts.end())
void ScriptingEngine::RegisterModule(Ref<ScriptModule> scriptModule)
{
std::string query = "import \"" + path + "\" for " + mod;
wrenInterpret(m_WrenVM, "main", query.c_str());
Modules[scriptModule->GetModuleName()] = scriptModule;
scriptModule->RegisterModule(m_WrenVM);
}
return CreateRef<WrenScript>(path, mod);
}
// Useful to check if a script has been imported, importing a script
// twice gives a compile error.
bool ScriptingEngine::IsScriptImported(const std::string& path)
{
for (auto& script : m_LoadedScripts)
void ScriptingEngine::InitScript(Ref<WrenScript> script)
{
if (script == path)
return true;
script->CallInit();
}
return false;
}
void ScriptingEngine::ImportScript(const std::string& path)
{
if (IsScriptImported(path))
return;
m_LoadedScripts.push_back(path);
}
void ScriptingEngine::RegisterModule(Ref<ScriptModule> scriptModule)
{
Modules[scriptModule->GetModuleName()] = scriptModule;
scriptModule->RegisterModule(m_WrenVM);
}
void ScriptingEngine::InitScript(Ref<WrenScript> script)
{
script->CallInit();
}
void ScriptingEngine::UpdateScript(Ref<WrenScript> script, Timestep timestep)
{
script->CallUpdate(timestep);
}
void ScriptingEngine::ExitScript(Ref<WrenScript> script)
{
script->CallExit();
}
void ScriptingEngine::Init()
{
m_Scripts = std::map<std::string, Ref<WrenScript>>();
m_LoadedScripts.clear();
WrenConfiguration config;
wrenInitConfiguration(&config);
config.loadModuleFn = &myLoadModule;
config.errorFn = &errorFn;
config.writeFn = &writeFn;
config.bindForeignMethodFn = &bindForeignMethod;
m_WrenVM = wrenNewVM(&config);
Ref<ScriptAPI::EngineModule> engineModule = CreateRef<ScriptAPI::EngineModule>();
RegisterModule(engineModule);
Ref<ScriptAPI::SceneModule> sceneModule = CreateRef<ScriptAPI::SceneModule>();
RegisterModule(sceneModule);
Ref<ScriptAPI::MathModule> mathModule = CreateRef<ScriptAPI::MathModule>();
RegisterModule(mathModule);
Ref<ScriptAPI::InputModule> inputModule = CreateRef<ScriptAPI::InputModule> ();
RegisterModule(inputModule);
Ref<ScriptAPI::PhysicsModule> physicsModule = CreateRef<ScriptAPI::PhysicsModule>();
RegisterModule(physicsModule);
}
void ScriptingEngine::Close()
{
wrenFreeVM(m_WrenVM);
}
WrenForeignMethodFn ScriptingEngine::bindForeignMethod(WrenVM* vm, const char* module, const char* className, bool isStatic, const char* signature)
{
if (Modules.find(className) != Modules.end())
void ScriptingEngine::UpdateScript(Ref<WrenScript> script, Timestep timestep)
{
Ref<ScriptModule> mod = Modules[className];
void* ptr = mod->GetMethodPointer(signature);
return (WrenForeignMethodFn)ptr;
script->CallUpdate(timestep);
}
void ScriptingEngine::ExitScript(Ref<WrenScript> script)
{
script->CallExit();
}
void ScriptingEngine::Init()
{
m_Scripts = std::map<std::string, Ref<WrenScript>>();
m_LoadedScripts.clear();
WrenConfiguration config;
wrenInitConfiguration(&config);
config.loadModuleFn = &myLoadModule;
config.errorFn = &errorFn;
config.writeFn = &writeFn;
config.bindForeignMethodFn = &bindForeignMethod;
m_WrenVM = wrenNewVM(&config);
Ref<ScriptAPI::EngineModule> engineModule = CreateRef<ScriptAPI::EngineModule>();
RegisterModule(engineModule);
Ref<ScriptAPI::SceneModule> sceneModule = CreateRef<ScriptAPI::SceneModule>();
RegisterModule(sceneModule);
Ref<ScriptAPI::MathModule> mathModule = CreateRef<ScriptAPI::MathModule>();
RegisterModule(mathModule);
Ref<ScriptAPI::InputModule> inputModule = CreateRef<ScriptAPI::InputModule>();
RegisterModule(inputModule);
Ref<ScriptAPI::PhysicsModule> physicsModule = CreateRef<ScriptAPI::PhysicsModule>();
RegisterModule(physicsModule);
}
void ScriptingEngine::Close()
{
wrenFreeVM(m_WrenVM);
}
WrenForeignMethodFn ScriptingEngine::bindForeignMethod(WrenVM* vm, const char* module, const char* className, bool isStatic, const char* signature)
{
if (Modules.find(className) != Modules.end())
{
Ref<ScriptModule> mod = Modules[className];
void* ptr = mod->GetMethodPointer(signature);
return (WrenForeignMethodFn)ptr;
}
}
WrenVM* ScriptingEngine::GetWrenVM()
{
return m_WrenVM;
}
}
WrenVM* ScriptingEngine::GetWrenVM()
{
return m_WrenVM;
}

View File

@@ -2,41 +2,43 @@
#include <string>
#include <vector>
#include <map>
#include <string>
#include "src/Core/Core.h"
#include "src/Vendors/wren/src/include/wren.hpp"
#include <src/Core/Timestep.h>
#include "src/Core/Timestep.h"
class WrenScript;
class ScriptModule;
class ScriptingEngine
namespace Nuake
{
private:
static WrenVM* m_WrenVM;
class WrenScript;
class ScriptModule;
class ScriptingEngine
{
private:
static WrenVM* m_WrenVM;
static std::vector<std::string> m_LoadedScripts;
static std::map<std::string, Ref<WrenScript>> m_Scripts;
static std::map<std::string, Ref<ScriptModule>> Modules;
public:
static Ref<WrenScript> RegisterScript(const std::string& path, const std::string& mod);
static bool IsScriptImported(const std::string& path);
static void ImportScript(const std::string& path);
static void RegisterModule(Ref<ScriptModule> module);
static void InitScript(Ref<WrenScript> script);
static void UpdateScript(Ref<WrenScript> script, Timestep timestep);
static void ExitScript(Ref<WrenScript> script);
static void Init();
static void RunCode(const std::string& code);
static void Close();
static std::vector<std::string> m_LoadedScripts;
static std::map<std::string, Ref<WrenScript>> m_Scripts;
static std::map<std::string, Ref<ScriptModule>> Modules;
public:
static Ref<WrenScript> RegisterScript(const std::string& path, const std::string& mod);
static bool IsScriptImported(const std::string& path);
static void ImportScript(const std::string& path);
static void RegisterModule(Ref<ScriptModule> module);
static void InitScript(Ref<WrenScript> script);
static void UpdateScript(Ref<WrenScript> script, Timestep timestep);
static void ExitScript(Ref<WrenScript> script);
static void Init();
static void RunCode(const std::string& code);
static void Close();
static WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature);
static WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature);
static WrenVM* GetWrenVM();
};
static WrenVM* GetWrenVM();
};
}

View File

@@ -2,110 +2,112 @@
#include "../Core/FileSystem.h"
#include <src/Vendors/wren/src/include/wren.h>
WrenScript::WrenScript(const std::string& path, const std::string& mod, bool isEntity)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
CompiledSuccesfully = true;
// Can't import twice the same script, otherwise gives a compile error.
if (!ScriptingEngine::IsScriptImported(path))
namespace Nuake {
WrenScript::WrenScript(const std::string& path, const std::string& mod, bool isEntity)
{
std::string source = "import \"" + path + "\" for " + mod;
WrenInterpretResult result = wrenInterpret(vm, "main", source.c_str());
WrenVM* vm = ScriptingEngine::GetWrenVM();
if (result != WREN_RESULT_SUCCESS)
CompiledSuccesfully = false;
CompiledSuccesfully = true;
// Can't import twice the same script, otherwise gives a compile error.
if (!ScriptingEngine::IsScriptImported(path))
{
std::string source = "import \"" + path + "\" for " + mod;
WrenInterpretResult result = wrenInterpret(vm, "main", source.c_str());
if (result != WREN_RESULT_SUCCESS)
CompiledSuccesfully = false;
if (!CompiledSuccesfully)
return;
ScriptingEngine::ImportScript(path);
}
// Get handle to class
wrenEnsureSlots(vm, 1);
wrenGetVariable(vm, "main", mod.c_str(), 0);
WrenHandle* classHandle = wrenGetSlotHandle(vm, 0);
// Call the constructor
WrenHandle* constructHandle = wrenMakeCallHandle(vm, "new()");
wrenCall(vm, constructHandle);
// Retreive value of constructor
this->m_Instance = wrenGetSlotHandle(vm, 0);
// Create handles to the instance methods.
this->m_OnInitHandle = wrenMakeCallHandle(vm, "init()");
this->m_OnUpdateHandle = wrenMakeCallHandle(vm, "update(_)");
this->m_OnFixedUpdateHandle = wrenMakeCallHandle(vm, "fixedUpdate(_)");
this->m_OnExitHandle = wrenMakeCallHandle(vm, "exit()");
if (isEntity)
this->m_SetEntityIDHandle = wrenMakeCallHandle(vm, "SetEntityId(_)");
}
void WrenScript::CallInit()
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenSetSlotHandle(vm, 0, this->m_Instance);
WrenInterpretResult result = wrenCall(vm, this->m_OnInitHandle);
}
void WrenScript::CallUpdate(float timestep)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenEnsureSlots(vm, 2);
wrenSetSlotHandle(vm, 0, this->m_Instance);
wrenSetSlotDouble(vm, 1, timestep);
WrenInterpretResult result = wrenCall(vm, this->m_OnUpdateHandle);
}
void WrenScript::CallFixedUpdate(float timestep)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenEnsureSlots(vm, 2);
wrenSetSlotHandle(vm, 0, this->m_Instance);
wrenSetSlotDouble(vm, 1, timestep);
WrenInterpretResult result = wrenCall(vm, this->m_OnFixedUpdateHandle);
}
void WrenScript::CallExit()
{
if (!CompiledSuccesfully)
return;
ScriptingEngine::ImportScript(path);
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenEnsureSlots(vm, 1);
wrenSetSlotHandle(vm, 0, this->m_Instance);
WrenInterpretResult result = wrenCall(vm, this->m_OnExitHandle);
}
// Get handle to class
wrenEnsureSlots(vm, 1);
wrenGetVariable(vm, "main", mod.c_str(), 0);
WrenHandle* classHandle = wrenGetSlotHandle(vm, 0);
void WrenScript::RegisterMethod(const std::string& signature)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
WrenHandle* handle = wrenMakeCallHandle(vm, signature.c_str());
methods.emplace(signature, handle);
}
// Call the constructor
WrenHandle* constructHandle = wrenMakeCallHandle(vm, "new()");
wrenCall(vm, constructHandle);
void WrenScript::CallMethod(const std::string& signature)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
// Retreive value of constructor
this->m_Instance = wrenGetSlotHandle(vm, 0);
// Create handles to the instance methods.
this->m_OnInitHandle = wrenMakeCallHandle(vm, "init()");
this->m_OnUpdateHandle = wrenMakeCallHandle(vm, "update(_)");
this->m_OnFixedUpdateHandle = wrenMakeCallHandle(vm, "fixedUpdate(_)");
this->m_OnExitHandle = wrenMakeCallHandle(vm, "exit()");
// Not found. maybe try to register it?
if (methods.find(signature) == methods.end())
return;
if (isEntity)
this->m_SetEntityIDHandle = wrenMakeCallHandle(vm, "SetEntityId(_)");
}
void WrenScript::CallInit()
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenSetSlotHandle(vm, 0, this->m_Instance);
WrenInterpretResult result = wrenCall(vm, this->m_OnInitHandle);
}
void WrenScript::CallUpdate(float timestep)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenEnsureSlots(vm, 2);
wrenSetSlotHandle(vm, 0, this->m_Instance);
wrenSetSlotDouble(vm, 1, timestep);
WrenInterpretResult result = wrenCall(vm, this->m_OnUpdateHandle);
}
void WrenScript::CallFixedUpdate(float timestep)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenEnsureSlots(vm, 2);
wrenSetSlotHandle(vm, 0, this->m_Instance);
wrenSetSlotDouble(vm, 1, timestep);
WrenInterpretResult result = wrenCall(vm, this->m_OnFixedUpdateHandle);
}
void WrenScript::CallExit()
{
if (!CompiledSuccesfully)
return;
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenEnsureSlots(vm, 1);
wrenSetSlotHandle(vm, 0, this->m_Instance);
WrenInterpretResult result = wrenCall(vm, this->m_OnExitHandle);
}
void WrenScript::RegisterMethod(const std::string& signature)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
WrenHandle* handle = wrenMakeCallHandle(vm, signature.c_str());
methods.emplace(signature, handle);
}
void WrenScript::CallMethod(const std::string& signature)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
// Not found. maybe try to register it?
if (methods.find(signature) == methods.end())
return;
wrenSetSlotHandle(vm, 0, this->m_Instance);
WrenHandle* handle = methods[signature];
WrenInterpretResult result = wrenCall(vm, handle);
}
void WrenScript::SetScriptableEntityID(int id)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenSetSlotHandle(vm, 0, this->m_Instance);
wrenSetSlotDouble(vm, 1, id);
WrenInterpretResult result = wrenCall(vm, this->m_SetEntityIDHandle);
wrenSetSlotHandle(vm, 0, this->m_Instance);
WrenHandle* handle = methods[signature];
WrenInterpretResult result = wrenCall(vm, handle);
}
void WrenScript::SetScriptableEntityID(int id)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenSetSlotHandle(vm, 0, this->m_Instance);
wrenSetSlotDouble(vm, 1, id);
WrenInterpretResult result = wrenCall(vm, this->m_SetEntityIDHandle);
}
}

View File

@@ -4,28 +4,31 @@
#include <string>
class WrenHandle;
class WrenScript
{
public:
std::map <std::string, WrenHandle*> methods;
WrenHandle* m_Instance;
WrenHandle* m_OnInitHandle;
WrenHandle* m_OnUpdateHandle;
WrenHandle* m_OnFixedUpdateHandle;
WrenHandle* m_OnExitHandle;
WrenHandle* m_SetEntityIDHandle;
bool CompiledSuccesfully;
namespace Nuake {
class WrenScript
{
public:
std::map <std::string, WrenHandle*> methods;
WrenHandle* m_Instance;
WrenHandle* m_OnInitHandle;
WrenHandle* m_OnUpdateHandle;
WrenHandle* m_OnFixedUpdateHandle;
WrenHandle* m_OnExitHandle;
WrenHandle* m_SetEntityIDHandle;
WrenScript(const std::string& path, const std::string& mod, bool isEntity = false);
bool CompiledSuccesfully;
void CallInit();
void CallUpdate(float timestep);
void CallFixedUpdate(float timestep);
void CallExit();
WrenScript(const std::string& path, const std::string& mod, bool isEntity = false);
void RegisterMethod(const std::string& signature);
void CallMethod(const std::string& signature);
void CallInit();
void CallUpdate(float timestep);
void CallFixedUpdate(float timestep);
void CallExit();
void SetScriptableEntityID(int id);
};
void RegisterMethod(const std::string& signature);
void CallMethod(const std::string& signature);
void SetScriptableEntityID(int id);
};
}