Various improvements:

Framebuffer resize queued to next frame to fix flickering
Wren VM init moved to on play event
Added new Wren modules
Added missing components to the editor
Changed how the wren scripts gets initialized
This commit is contained in:
Antoine Pilote
2021-06-12 13:01:07 -04:00
parent 855bb1edd6
commit cdfee1981c
25 changed files with 451 additions and 108 deletions

View File

@@ -3,6 +3,52 @@ class Math {
}
class Vector3 {
x {_x}
y {_y}
z {_z}
x=(value) {
_x = value
}
y=(value) {
_y = value
}
z=(value) {
_z = value
}
mul(other) {
if(other is Vector3) {
return Vector3.new(_x * other.x,
_y * other.y,
_z * other.z)
} else {
return Vector3.new(_x * other, _y * other, _z * other)
}
}
*(other) {
if(other is Vector3) {
return Vector3.new(_x * other.x,
_y * other.y,
_z * other.z)
} else if(other is Num) {
return Vector3.new(_x * other, _y * other, _z * other)
}
}
+(other) {
if(other is Vector3) {
return Vector3.new(_x + other.x,
_y + other.y,
_z + other.z)
} else if(other is Num) {
return Vector3.new(_x + other,
_y + other,
_z + other)
}
}
construct new(x, y, z) {
_x = x
_y = y

View File

@@ -1,4 +1,5 @@
import "Scripts/Engine" for Engine
import "Scripts/Math" for Vector3
class Scene {
foreign static GetEntityID(name)
@@ -11,35 +12,50 @@ class Scene {
}
foreign static EntityHasComponent(id, name)
static EntityGetComponent(id, component) {
if(this.EntityHasComponent(id, component) == false) {
Engine.Log("Tried getting a non-existent component of type: %(component) on entity with id: %(id)")
return
}
// Component specific private setter and getter.
foreign static GetLightIntensity_(e)
foreign static SetLightIntensity_(e, intensity)
if (component == "Light") {
return Light.new(id)
} else if (component == "CharacterController") {
return CharacterController.new(id)
} else if (component == "Camera") {
return Camera.new(id)
}
}
/*
// 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
//
// Components
//
/*
foreign static SetLightIsVolumetric_(e, bool)
foreign static SetLightSyncDirectionWithSky_(e, bool)
foreign static SetLightColor_(e, r, g, b)
// Transform
//foreign static SetTranslation_(e, x, y, z)
//foreign static SetRotation_(e, x, y, z)
//foreign static SetScale_(e, x, y, z)
// Light
foreign static GetLightIntensity_(e) // returns a float
foreign static SetLightIntensity_(e, intensity)
//foreign static SetLightIsVolumetric_(e, bool)
//foreign static SetLightSyncDirectionWithSky_(e, bool)
//foreign static SetLightColor_(e, r, g, b)
//foreign static GetLightColor_(e)
// Camera
foreign static SetCameraFov_(e, fov)
foreign static SetCameraType(e, type)
foreign static SetCameraDirection_(e, x, y, z)
*/
foreign static GetCameraDirection_(e) // returns a list x,y,z
foreign static GetCameraRight_(e) // returns a list x,y,z
//foreign static SetcameraFov(e, fov)
//foreign static GetCameraFov(e) // returns a float
// Character controller
foreign static MoveAndSlide_(e, x, y, z)
//foreign static IsOnGround_(e)
}
class Entity {
@@ -52,14 +68,7 @@ class Entity {
}
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)
}
return Scene.EntityGetComponent(_entityId, component)
}
// Foreign engine functions
@@ -110,17 +119,37 @@ class Light {
SetColor(color) {
this.SetColor_(_entityId, color.r, color.g, color.b, color.a)
}*/
}
class CharacterController {
construct new(id) {
_entityId = id
}
SetDirection(direction) {
var normalized = direction.Normalize()
this.SetDirection_(_entityId, normalized.x, normalized.y, normalized.z)
MoveAndSlide(vel) {
Scene.MoveAndSlide_(_entityId, vel.x, vel.y, vel.z)
}
foreign static SetType_(id, type)
foreign static SetColor_(id, r, g, b)
foreign static SetDirection_(id, x, y, z)
*/
}
}
class Camera {
construct new(id) {
_entityId = id
}
SetDirection(dir) {
Scene.SetCameraDirection_(_entityId, dir.x, dir.y, dir.z)
}
GetDirection() {
var dir = Scene.GetCameraDirection_(_entityId)
return Vector3.new(dir[0], dir[1], dir[2])
}
GetRight() {
var dir = Scene.GetCameraRight_(_entityId)
return Vector3.new(dir[0], dir[1], dir[2])
}
}

View File

@@ -0,0 +1,15 @@
import "Scripts/Scene" for Scene
class ScriptableEntity {
SetEntityId(id) {
_EntityID = id
}
GetComponent(component) {
return Scene.EntityGetComponent(_EntityID, component)
}
HasComponent(component) {
return Scene.EntityHasComponent(_EntityID, component)
}
}

View File

@@ -19,6 +19,7 @@
#include "src/Resource/Project.h"
#include <src/Scene/Entities/Components/LuaScriptComponent.h>
#include <src/Core/Logger.h>
#include <src/Scene/Entities/Components/WrenScriptComponent.h>
Ref<UI::UserInterface> userInterface;
ImFont* normalFont;
ImFont* EditorInterface::bigIconFont;
@@ -80,7 +81,7 @@ void EditorInterface::DrawViewport()
glm::vec2 viewportPanelSize = glm::vec2(regionAvail.x, regionAvail.y);
if(Engine::GetCurrentWindow()->GetFrameBuffer()->GetSize() != viewportPanelSize)
Engine::GetCurrentWindow()->GetFrameBuffer()->UpdateSize(viewportPanelSize);
Engine::GetCurrentWindow()->GetFrameBuffer()->QueueResize(viewportPanelSize);
Ref<Texture> texture = Engine::GetCurrentWindow()->GetFrameBuffer()->GetTexture();
ImGui::Image((void*)texture->GetID(), regionAvail, ImVec2(0, 1), ImVec2(1, 0));
@@ -321,16 +322,22 @@ void EditorInterface::DrawEntityPropreties()
}
if (ImGui::BeginPopup("add_component_popup"))
{
if (ImGui::MenuItem("Lua script") && !m_SelectedEntity.HasComponent<LuaScriptComponent>())
m_SelectedEntity.AddComponent<LuaScriptComponent>();
if (ImGui::MenuItem("Light Component") && !m_SelectedEntity.HasComponent<LightComponent>())
m_SelectedEntity.AddComponent<LightComponent>();
if (ImGui::MenuItem("Mesh Component") && !m_SelectedEntity.HasComponent<MeshComponent>())
m_SelectedEntity.AddComponent<MeshComponent>();
if (ImGui::MenuItem("Wren Script") && !m_SelectedEntity.HasComponent<WrenScriptComponent>())
m_SelectedEntity.AddComponent<WrenScriptComponent>();
ImGui::Separator();
if (ImGui::MenuItem("Camera Component") && !m_SelectedEntity.HasComponent<CameraComponent>())
m_SelectedEntity.AddComponent<CameraComponent>();
ImGui::Separator();
if (ImGui::MenuItem("Light Component") && !m_SelectedEntity.HasComponent<LightComponent>())
m_SelectedEntity.AddComponent<LightComponent>();
ImGui::Separator();
if (ImGui::MenuItem("Mesh Component") && !m_SelectedEntity.HasComponent<MeshComponent>())
m_SelectedEntity.AddComponent<MeshComponent>();
if (ImGui::MenuItem("Quake map Component") && !m_SelectedEntity.HasComponent<QuakeMapComponent>())
m_SelectedEntity.AddComponent<QuakeMapComponent>();
ImGui::Separator();
if (ImGui::MenuItem("Character controller") && !m_SelectedEntity.HasComponent<CharacterControllerComponent>())
m_SelectedEntity.AddComponent<CharacterControllerComponent>();
if (ImGui::MenuItem("Rigidbody Component") && !m_SelectedEntity.HasComponent<RigidBodyComponent>())
{
m_SelectedEntity.AddComponent<RigidBodyComponent>();
@@ -377,32 +384,43 @@ void EditorInterface::DrawEntityPropreties()
}
if (m_SelectedEntity.HasComponent<LuaScriptComponent>()) {
if (m_SelectedEntity.HasComponent<WrenScriptComponent>()) {
std::string icon = ICON_FA_FILE;
if (ImGui::CollapsingHeader((icon + " " + "Lua script").c_str(), ImGuiTreeNodeFlags_DefaultOpen))
if (ImGui::CollapsingHeader((icon + " " + "Wren Script").c_str(), ImGuiTreeNodeFlags_DefaultOpen))
{
auto& component = m_SelectedEntity.GetComponent<LuaScriptComponent>();
auto& component = m_SelectedEntity.GetComponent<WrenScriptComponent>();
// Path
std::string path = component.Script;
char pathBuffer[256];
memset(pathBuffer, 0, sizeof(pathBuffer));
std::strncpy(pathBuffer, path.c_str(), sizeof(pathBuffer));
if (ImGui::InputText("##ScriptPath", pathBuffer, sizeof(pathBuffer)))
{
path = std::string(pathBuffer);
}
path = FileSystem::AbsoluteToRelative(std::string(pathBuffer));
ImGui::SameLine();
if (ImGui::Button("Browse"))
{
path = FileDialog::OpenFile(".map");
}
path = FileSystem::AbsoluteToRelative(FileDialog::OpenFile(".wren"));
component.Script = path;
// Class
std::string module = component.Class;
char classBuffer[256];
memset(classBuffer, 0, sizeof(classBuffer));
std::strncpy(classBuffer, module.c_str(), sizeof(classBuffer));
if (ImGui::InputText("##ScriptModule", classBuffer, sizeof(classBuffer)))
module = std::string(classBuffer);
component.Class = module;
ImGui::Separator();
}
}
if (m_SelectedEntity.HasComponent<CameraComponent>()) {
@@ -416,6 +434,18 @@ void EditorInterface::DrawEntityPropreties()
}
if (m_SelectedEntity.HasComponent<CharacterControllerComponent>())
{
if (ImGui::CollapsingHeader("Character controller", ImGuiTreeNodeFlags_DefaultOpen))
{
auto& c = m_SelectedEntity.GetComponent<CharacterControllerComponent>();
ImGui::InputFloat("Height", &c.Height);
ImGui::InputFloat("Radius", &c.Radius);
ImGui::InputFloat("Mass", &c.Mass);
ImGui::Separator();
}
}
if (m_SelectedEntity.HasComponent<MeshComponent>()) {
std::string icon = ICON_FA_TREE;
@@ -907,17 +937,26 @@ void OpenProject()
Engine::LoadProject(project);
// Create new interface named test.
userInterface = UI::UserInterface::New("test");
//userInterface = UI::UserInterface::New("test");
// Set current interface running.
Engine::GetCurrentScene()->AddInterface(userInterface);
//Engine::GetCurrentScene()->AddInterface(userInterface);
}
void OpenScene()
{
// Parse the project and load it.
std::string projectPath = FileDialog::OpenFile(".scene");
Ref<Scene> scene = Scene::New();
if (!scene->Deserialize(FileSystem::ReadFile(projectPath, true))) {
Logger::Log("Error failed loading scene: " + projectPath);
return;
}
scene->Path = FileSystem::AbsoluteToRelative(projectPath);
Engine::LoadScene(scene);
}
void EditorInterface::DrawInit()
@@ -981,6 +1020,10 @@ void EditorInterface::Draw()
m_IsEntitySelected = false;
}
ImGui::Separator();
if (ImGui::MenuItem("Set current scene as default")) {
Engine::GetProject()->DefaultScene = Engine::GetCurrentScene();
}
ImGui::Separator();
if (ImGui::MenuItem("Open scene...", "CTRL+O"))
{
OpenScene();