diff --git a/Editor/resources/Scripts/Engine.wren b/Editor/resources/Scripts/Engine.wren index 8129176e..1138329a 100644 --- a/Editor/resources/Scripts/Engine.wren +++ b/Editor/resources/Scripts/Engine.wren @@ -1,26 +1,10 @@ +import "Scripts/Math" for Vector3 + class Engine { foreign static Log(msg) } -class Scene { - foreign static GetEntityID(name) - static GetEntity(name) { - var entId = Scene.GetEntityID(name) - var ent = Entity.new(entId) - return ent - } - foreign static EntityHasComponent(id, name) -} -class Entity { - construct new(id) { - _entityId = id - } - - HasComponent(component) { - return Scene.EntityHasComponent(_entityId, component) - } -} diff --git a/Editor/resources/Scripts/Input.wren b/Editor/resources/Scripts/Input.wren new file mode 100644 index 00000000..321dacd0 --- /dev/null +++ b/Editor/resources/Scripts/Input.wren @@ -0,0 +1,70 @@ +import "Scripts/Math" for Vector3 +import "Scripts/Engine" for Engine +class Input { + foreign static GetMouseX() + foreign static GetMouseY() + + // Gets the mouse position of X & Y and returns a Vector3 + static GetMousePos() { + var result = Vector3.new(this.GetMouseX_(), this.GetMouseY_(), 0) + return result + } + + // Keys + foreign static IsKeyDown_(key) + static IsKeyDown(key) { + if(key is Num) { + return this.IsKeyDown_(key) + } + Engine.Log("IsKeyDown expects a number. Got: %(key.type)") + } + + foreign static IsKeyPressed_(key) + static IsKeyPressed(key) { + if(key is Num){ + return this.IsKeyPressed_(key) + } + + Engine.Log("IsKeyPressed expects a number. Got: %(key.type)") + } + + foreign static IsKeyReleased_(key) + static IsKeyReleased(key) { + if(key is Num) { + return this.IsKeyReleased_(key) + } + + Engine.Log("IsKeyReleased expects a number. Got: %(key.type)") + } + + // Mouse + foreign static IsMouseButtonDown_(button) + static IsMouseButtonDown(button) { + if(button is Num){ + return this.IsMouseButtonDown_(button) + } + + } + + foreign static IsMouseButtonPressed_(button) + static IsMouseButtonPressed(button) { + if(button is Num) { + Engine.Log("IsmouseButtonPressed: %(button)") + return this.IsMouseButtonPressed_(button) + } + + } + + foreign static IsMouseButtonReleased_(button) + static IsMouseButtonReleased(button) { + if(button is Num){ + return this.IsMouseButtonReleased_(button) + } + + } + + foreign static HideMouse() + foreign static ShowMouse() + foreign static IsMouseHidden() + +} \ No newline at end of file diff --git a/Editor/resources/Scripts/Math.wren b/Editor/resources/Scripts/Math.wren new file mode 100644 index 00000000..7daa24ef --- /dev/null +++ b/Editor/resources/Scripts/Math.wren @@ -0,0 +1,23 @@ +class Math { + foreign static Sqrt_(x, y, z) +} + +class Vector3 { + construct new(x, y, z) { + _x = x + _y = y + _z = z + } + + Sqrt() { + return Math.Sqrt_(_x, _y, _z) + } + + Normalize() { + var length = this.Sqrt() + var x = _x / length + var y = _y / length + var z = _z / length + return Vector3.new(x, y, z) + } +} \ No newline at end of file diff --git a/Editor/resources/Scripts/Scene.wren b/Editor/resources/Scripts/Scene.wren new file mode 100644 index 00000000..c6c0d099 --- /dev/null +++ b/Editor/resources/Scripts/Scene.wren @@ -0,0 +1,126 @@ +import "Scripts/Engine" for Engine + +class Scene { + foreign static GetEntityID(name) + + + static GetEntity(name) { + var entId = Scene.GetEntityID(name) + var ent = Entity.new(entId) + return ent + } + + foreign static EntityHasComponent(id, name) + + // Component specific private setter and getter. + foreign static GetLightIntensity_(e) + foreign static SetLightIntensity_(e, intensity) + + + /* + // Transform + foreign static SetTranslation_(e, x, y, z) + foreign static SetRotation_(e, x, y, z) + foreign static SetScale_(e, x, y, z) + + // Character controller + foreign static SetVelocity(e, x, y, z) + foreign static SetStepHeight(e, x, y, z) + foreign static IsOnGround(e) + */ + // Light + // + /* + foreign static SetLightIsVolumetric_(e, bool) + foreign static SetLightSyncDirectionWithSky_(e, bool) + foreign static SetLightColor_(e, r, g, b) + + // Camera + foreign static SetCameraFov_(e, fov) + foreign static SetCameraType(e, type) + foreign static SetCameraDirection_(e, x, y, z) + */ +} + +class Entity { + construct new(id) { + _entityId = id + } + + HasComponent(component) { + return Scene.EntityHasComponent(_entityId, component) + } + + GetComponent(component) { + if(this.HasComponent(component) == false) { + Engine.Log("Tried getting a non-existent component of type: %(component) on entity with id: %(_entityId)") + return + } + + if (component == "Light") { + return Light.new(_entityId) + } + } + + // Foreign engine functions +/* + // Transform + foreign static SetTranslation_(e, x, y, z) + foreign static SetRotation_(e, x, y, z) + foreign static SetScale_(e, x, y, z) + + // Character controller + foreign static SetVelocity(e, x, y, z) + foreign static SetStepHeight(e, x, y, z) + foreign static IsOnGround(e) + */ + // Light + // + /* + foreign static SetLightIsVolumetric_(e, bool) + foreign static SetLightSyncDirectionWithSky_(e, bool) + foreign static SetLightColor_(e, r, g, b) + + // Camera + foreign static SetCameraFov_(e, fov) + foreign static SetCameraType(e, type) + foreign static SetCameraDirection_(e, x, y, z) + */ +} + +class Light { + construct new(id) { + _entityId = id + } + + + SetIntensity(intensity) { + Scene.SetLightIntensity_(_entityId, intensity) + } + + GetIntensity() { + return Scene.GetLightIntensity_(_entityId) + } + + + /* + SetType(type) { + this.SetType_(_entityId, type) + } + + SetColor(color) { + this.SetColor_(_entityId, color.r, color.g, color.b, color.a) + } + + SetDirection(direction) { + var normalized = direction.Normalize() + this.SetDirection_(_entityId, normalized.x, normalized.y, normalized.z) + } + + foreign static SetType_(id, type) + foreign static SetColor_(id, r, g, b) + + foreign static SetDirection_(id, x, y, z) + */ + +} \ No newline at end of file diff --git a/Editor/resources/Scripts/test.wren b/Editor/resources/Scripts/test.wren index aef4f421..67320b50 100644 --- a/Editor/resources/Scripts/test.wren +++ b/Editor/resources/Scripts/test.wren @@ -1,4 +1,7 @@ -import "Scripts/Engine" for Engine, Scene, Entity +import "Scripts/Engine" for Engine +import "Scripts/Input" for Input +import "Scripts/Scene" for Scene, Entity + class Test { init() { System.print("hello init") @@ -13,10 +16,12 @@ class Test { } static hello() { - var entity = Scene.GetEntity("Trenchbroom map") - var hasTransform = entity.HasComponent("Transform") - var hasLight = entity.HasComponent("Light") + var entity = Scene.GetEntity("Light") + var light = entity.GetComponent("Light") + light.SetIntensity(1.0) - Engine.Log("trasnform: %(hasTransform) light:%(hasLight)") + if(Input.IsMouseButtonPressed(2) == true) { + Engine.Log("RIGHT CLICK!!!!!!") + } } } \ No newline at end of file diff --git a/Nuake/src/Rendering/Camera.cpp b/Nuake/src/Rendering/Camera.cpp index 2e213108..6b40238d 100644 --- a/Nuake/src/Rendering/Camera.cpp +++ b/Nuake/src/Rendering/Camera.cpp @@ -38,6 +38,18 @@ void Camera::OnWindowResize(int x, int y) float height = y; } +void Camera::SetDirection(Vector3 direction) +{ + //cam->cameraDirection.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch)); + //cam->cameraDirection.y = sin(glm::radians(Pitch)); + //cam->cameraDirection.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch)); + //cam->cameraFront = glm::normalize(cam->cameraDirection); + //cam->cameraRight = glm::normalize(glm::cross(cam->up, cam->cameraFront)); + cameraDirection = glm::normalize(direction); + cameraFront = cameraDirection; + cameraRight = glm::normalize(glm::cross(up, cameraFront)); +} + glm::vec3 Camera::GetTranslation() { return Translation; } diff --git a/Nuake/src/Rendering/Camera.h b/Nuake/src/Rendering/Camera.h index 0c6f0695..6c6a6699 100644 --- a/Nuake/src/Rendering/Camera.h +++ b/Nuake/src/Rendering/Camera.h @@ -42,6 +42,8 @@ public: void SetType(CAMERA_TYPE type); void OnWindowResize(int x, int y); + void SetDirection(Vector3 direction); + Vector3 GetTranslation(); Vector3 GetDirection(); Matrix4 GetPerspective(); diff --git a/Nuake/src/Scene/EditorCamera.cpp b/Nuake/src/Scene/EditorCamera.cpp index a13d2062..04e42e20 100644 --- a/Nuake/src/Scene/EditorCamera.cpp +++ b/Nuake/src/Scene/EditorCamera.cpp @@ -8,13 +8,13 @@ void EditorCamera::Update(Timestep ts) float x = Input::GetMouseX(); float y = Input::GetMouseY(); - if (!controlled && Input::IsMouseButtonPressed(1)) + if (!controlled && Input::IsMouseButtonDown(1)) { mouseLastX = x; mouseLastY = y; } - controlled = Input::IsMouseButtonPressed(1); + controlled = Input::IsMouseButtonDown(1); if (!controlled) Input::ShowMouse(); @@ -36,30 +36,30 @@ void EditorCamera::Update(Timestep ts) } if (m_Type == CAMERA_TYPE::ORTHO) { - if (Input::IsKeyPressed(GLFW_KEY_RIGHT)) + if (Input::IsKeyDown(GLFW_KEY_RIGHT)) Translation.x += Speed * ts; - if (Input::IsKeyPressed(GLFW_KEY_LEFT)) + if (Input::IsKeyDown(GLFW_KEY_LEFT)) Translation.x -= Speed * ts; - if (Input::IsKeyPressed(GLFW_KEY_UP)) + if (Input::IsKeyDown(GLFW_KEY_UP)) Translation.y += Speed * ts; - if (Input::IsKeyPressed(GLFW_KEY_DOWN)) + if (Input::IsKeyDown(GLFW_KEY_DOWN)) Translation.y -= Speed * ts; } else { glm::vec3 movement = glm::vec3(0, 0, 0); - if (Input::IsKeyPressed(GLFW_KEY_D)) + if (Input::IsKeyDown(GLFW_KEY_D)) movement -= cameraRight * (Speed * ts); - if (Input::IsKeyPressed(GLFW_KEY_A)) + if (Input::IsKeyDown(GLFW_KEY_A)) movement += cameraRight * (Speed * ts); - if (Input::IsKeyPressed(GLFW_KEY_W)) + if (Input::IsKeyDown(GLFW_KEY_W)) movement += cameraDirection * (Speed * ts); - if (Input::IsKeyPressed(GLFW_KEY_S)) + if (Input::IsKeyDown(GLFW_KEY_S)) movement -= cameraDirection * (Speed * ts); - if (Input::IsKeyPressed(GLFW_KEY_LEFT_SHIFT)) + if (Input::IsKeyDown(GLFW_KEY_LEFT_SHIFT)) movement -= up * (Speed * ts); - if (Input::IsKeyPressed(GLFW_KEY_SPACE)) + if (Input::IsKeyDown(GLFW_KEY_SPACE)) movement += up * (Speed * ts); Translation += Vector3(movement); diff --git a/Nuake/src/Scripting/Modules/EngineModule.h b/Nuake/src/Scripting/Modules/EngineModule.h index ae5664cd..65f1eec0 100644 --- a/Nuake/src/Scripting/Modules/EngineModule.h +++ b/Nuake/src/Scripting/Modules/EngineModule.h @@ -11,10 +11,6 @@ namespace ScriptAPI class EngineModule : public ScriptModule { std::string ModuleName = "Engine"; - std::string WrenAPI = "class Engine { \n" - "foreign static Log(msg) \n" - "}" - ""; std::string GetModuleName() override { @@ -24,7 +20,6 @@ namespace ScriptAPI void RegisterModule(WrenVM* vm) override { RegisterMethod("Log(_)", (void*)Log); - WrenInterpretResult result = wrenInterpret(vm, "main", WrenAPI.c_str()); } static void Log(WrenVM* vm) @@ -33,6 +28,4 @@ namespace ScriptAPI Logger::Log(msg); } }; - - } \ No newline at end of file diff --git a/Nuake/src/Scripting/Modules/EntityModule.h b/Nuake/src/Scripting/Modules/EntityModule.h new file mode 100644 index 00000000..7835d49e --- /dev/null +++ b/Nuake/src/Scripting/Modules/EntityModule.h @@ -0,0 +1,38 @@ +#pragma once +#include "wren.h" +#include +#include +#include "ScriptModule.h" +#include +#include + +namespace ScriptAPI +{ + class EngineModule : public ScriptModule + { + std::string ModuleName = "Engine"; + std::string WrenAPI = "class Engine { \n" + "foreign static Log(msg) \n" + "}" + ""; + + 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); + } + }; + + +} \ No newline at end of file diff --git a/Nuake/src/Scripting/Modules/InputModule.h b/Nuake/src/Scripting/Modules/InputModule.h new file mode 100644 index 00000000..4f5d3cd7 --- /dev/null +++ b/Nuake/src/Scripting/Modules/InputModule.h @@ -0,0 +1,105 @@ +#pragma once +#include "wren.h" +#include +#include +#include "ScriptModule.h" +#include +#include +#include "../Core/Maths.h" +#include "../Core/Input.h" + +namespace ScriptAPI +{ + class InputModule : public ScriptModule + { + std::string GetModuleName() override + { + return "Input"; + } + + 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("IsMouseButtonDown_(_)", (void*)IsMouseButtonDown); + RegisterMethod("IsMouseButtonPressed_(_)", (void*)IsMouseButtonPressed); + RegisterMethod("IsMouseButtonReleased_(_)", (void*)IsMouseButtonReleased); + + RegisterMethod("HideMouse()", (void*)HideMouse); + RegisterMethod("ShowMouse()", (void*)ShowMouse); + RegisterMethod("IsMouseHidden()", (void*)IsMouseHidden); + } + + static void GetMouseX(WrenVM* vm) + { + wrenSetSlotDouble(vm, 0, Input::GetMouseX()); + } + + 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 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 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 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 ShowMouse(WrenVM* vm) + { + Input::ShowMouse(); + } + + static void IsMouseHidden(WrenVM* vm) + { + wrenSetSlotBool(vm, 0, Input::IsMouseHidden()); + } + }; +} \ No newline at end of file diff --git a/Nuake/src/Scripting/Modules/MathModule.h b/Nuake/src/Scripting/Modules/MathModule.h new file mode 100644 index 00000000..f4eff0f1 --- /dev/null +++ b/Nuake/src/Scripting/Modules/MathModule.h @@ -0,0 +1,39 @@ +#pragma once +#include "wren.h" +#include +#include +#include "ScriptModule.h" +#include +#include +#include "../Core/Maths.h" + +namespace ScriptAPI +{ + class MathModule : public ScriptModule + { + std::string ModuleName = "Math"; + + + std::string GetModuleName() override + { + return "Math"; + } + + void RegisterModule(WrenVM* vm) override + { + RegisterMethod("Sqrt_(_,_,_)", (void*)Sqrt); + + } + + static void Sqrt(WrenVM* vm) + { + float x = wrenGetSlotDouble(vm, 0); + float y = wrenGetSlotDouble(vm, 0); + float z = wrenGetSlotDouble(vm, 0); + float result = glm::sqrt((x * x) + (y * y) + (z * z)); + wrenSetSlotDouble(vm, 0, result); + } + }; + + +} \ No newline at end of file diff --git a/Nuake/src/Scripting/Modules/SceneModule.h b/Nuake/src/Scripting/Modules/SceneModule.h index 624e9c66..a2352946 100644 --- a/Nuake/src/Scripting/Modules/SceneModule.h +++ b/Nuake/src/Scripting/Modules/SceneModule.h @@ -28,6 +28,8 @@ namespace ScriptAPI { RegisterMethod("GetEntityID(_)", (void*)GetEntity); RegisterMethod("EntityHasComponent(_,_)", (void*)EntityHasComponent); + RegisterMethod("SetLightIntensity_(_,_)", (void*)SetLightIntensity); + RegisterMethod("GetLightIntensity_(_)", (void*)GetLightIntensity); } static void GetEntity(WrenVM* vm) @@ -74,5 +76,30 @@ namespace ScriptAPI wrenSetSlotBool(vm, 0, result); } } + + 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(); + 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(); + wrenSetSlotDouble(vm, 0, light.Strength); + } + + static void SetCameraDirection(WrenVM* vm) + { + + } }; } \ No newline at end of file diff --git a/Nuake/src/Scripting/ScriptingEngine.cpp b/Nuake/src/Scripting/ScriptingEngine.cpp index 7e501b88..2b38247c 100644 --- a/Nuake/src/Scripting/ScriptingEngine.cpp +++ b/Nuake/src/Scripting/ScriptingEngine.cpp @@ -4,6 +4,9 @@ #include #include #include +#include +#include + WrenVM* ScriptingEngine::m_WrenVM; @@ -91,6 +94,10 @@ void ScriptingEngine::Init() RegisterModule(engineModule); Ref sceneModule = CreateRef(); RegisterModule(sceneModule); + Ref mathModule = CreateRef(); + RegisterModule(mathModule); + Ref inputModule = CreateRef (); + RegisterModule(inputModule); } diff --git a/Nuake/src/UI/Styling/Stylesheet.h b/Nuake/src/UI/Styling/Stylesheet.h index 2aac27c7..5f6767f0 100644 --- a/Nuake/src/UI/Styling/Stylesheet.h +++ b/Nuake/src/UI/Styling/Stylesheet.h @@ -245,7 +245,6 @@ namespace UI KatanaArray errors = Data->errors; return false; } - katana_dump_output(Data); return true; } };