Major scripting update🕹

- Added Input wren module
- Added Math wren module
- Added Entity wren module
- Changed camera API
This commit is contained in:
Antoine Pilote
2021-06-10 22:23:53 -04:00
parent 41742ce0ad
commit 855bb1edd6
15 changed files with 473 additions and 43 deletions

View File

@@ -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)
}
}

View File

@@ -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()
}

View File

@@ -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)
}
}

View File

@@ -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)
*/
}

View File

@@ -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!!!!!!")
}
}
}

View File

@@ -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;
}

View File

@@ -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();

View File

@@ -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);

View File

@@ -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);
}
};
}

View File

@@ -0,0 +1,38 @@
#pragma once
#include "wren.h"
#include <string>
#include <src/Core/Logger.h>
#include "ScriptModule.h"
#include <iostream>
#include <wren.h>
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);
}
};
}

View File

@@ -0,0 +1,105 @@
#pragma once
#include "wren.h"
#include <string>
#include <src/Core/Logger.h>
#include "ScriptModule.h"
#include <iostream>
#include <wren.h>
#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());
}
};
}

View File

@@ -0,0 +1,39 @@
#pragma once
#include "wren.h"
#include <string>
#include <src/Core/Logger.h>
#include "ScriptModule.h"
#include <iostream>
#include <wren.h>
#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);
}
};
}

View File

@@ -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<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)
{
}
};
}

View File

@@ -4,6 +4,9 @@
#include <src/Scripting/Modules/ScriptModule.h>
#include <src/Scripting/Modules/EngineModule.h>
#include <src/Scripting/Modules/SceneModule.h>
#include <src/Scripting/Modules/MathModule.h>
#include <src/Scripting/Modules/InputModule.h>
WrenVM* ScriptingEngine::m_WrenVM;
@@ -91,6 +94,10 @@ void ScriptingEngine::Init()
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);
}

View File

@@ -245,7 +245,6 @@ namespace UI
KatanaArray errors = Data->errors;
return false;
}
katana_dump_output(Data);
return true;
}
};