From 1bb67f9e91f66dba8af725dbdd097eb51d5814cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tur=C3=A1nszki=20J=C3=A1nos?= Date: Fri, 11 Nov 2022 12:00:34 +0100 Subject: [PATCH] added camera shake sample script; lua binding updates; --- .../ScriptingAPI-Documentation.md | 3 + Content/scripts/camera_shake.lua | 47 +++++++++++++++ Editor/Editor.cpp | 3 +- WickedEngine/wiScene_BindLua.cpp | 60 +++++++++++++++++++ WickedEngine/wiScene_BindLua.h | 3 + WickedEngine/wiVersion.cpp | 2 +- 6 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 Content/scripts/camera_shake.lua diff --git a/Content/Documentation/ScriptingAPI-Documentation.md b/Content/Documentation/ScriptingAPI-Documentation.md index 24b01ee41..81c5ca380 100644 --- a/Content/Documentation/ScriptingAPI-Documentation.md +++ b/Content/Documentation/ScriptingAPI-Documentation.md @@ -700,6 +700,9 @@ Describes an orientation in 3D space. - GetPosition() : Vector result - GetLookDirection() : Vector result - GetUpDirection() : Vector result +- SetPosition(Vector value) +- SetLookDirection(Vector value) +- SetUpDirection(Vector value) #### AnimationComponent - Timer : float diff --git a/Content/scripts/camera_shake.lua b/Content/scripts/camera_shake.lua new file mode 100644 index 000000000..49942963c --- /dev/null +++ b/Content/scripts/camera_shake.lua @@ -0,0 +1,47 @@ +-- This script will play a camera shake effect + +local camera = GetCamera() +local shake_offset = Vector() +local shake_x = -1 +local shake_y = -1 +local shake_elapsed = 0 + +-- Outside settable params: +scriptableCameraShakeAmount = 0.2 +scriptableCameraShakeFrequency = 0.05 +scriptableCameraShakeAggressiveness = 0.1 + +runProcess(function() + while true do + if input.Press(KEYBOARD_BUTTON_F6) then + if scriptableCameraShakeAmount > 0 then + scriptableCameraShakeAmount = 0 + else + scriptableCameraShakeAmount = 0.1 + end + end + if shake_elapsed > scriptableCameraShakeFrequency then + shake_elapsed = 0 + if scriptableCameraShakeAmount > 0 then + local up = camera.GetUpDirection() + local forward = camera.GetLookDirection() + local side = vector.Cross(up, forward) + shake_x = shake_x * -1 + shake_y = shake_y * -1 + shake_offset = vector.Add( + vector.Multiply(side, math.random() * shake_x * scriptableCameraShakeAmount), + vector.Multiply(up, math.random() * shake_y * scriptableCameraShakeAmount) + ) + else + shake_offset = Vector() + end + end + shake_elapsed = shake_elapsed + getDeltaTime() + + local pos = camera.GetPosition() + pos = vector.Lerp(pos, vector.Add(pos, shake_offset), scriptableCameraShakeAggressiveness) + camera.SetPosition(pos) + camera.UpdateCamera() + update() + end +end) diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 5fd7940bd..03848bf5d 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -326,11 +326,12 @@ void EditorComponent::Load() closeButton.Create(""); closeButton.SetShadowRadius(2); closeButton.font.params.shadowColor = wi::Color::Transparent(); - closeButton.SetTooltip("Close the current scene.\nThis will clear everything from the currently selected scene, and delete the scene.\nThis operation cannot be undone!"); + closeButton.SetTooltip("Close the current scene.\nThis will clear everything from the currently selected scene, delete the scene and kill all script processes.\nThis operation cannot be undone!"); closeButton.SetColor(wi::Color(255, 130, 100, 180), wi::gui::WIDGETSTATE::IDLE); closeButton.SetColor(wi::Color(255, 200, 150, 255), wi::gui::WIDGETSTATE::FOCUS); closeButton.OnClick([&](wi::gui::EventArgs args) { + wi::lua::KillProcesses(); componentsWnd.terrainWnd.terrain_preset = {}; translator.selected.clear(); diff --git a/WickedEngine/wiScene_BindLua.cpp b/WickedEngine/wiScene_BindLua.cpp index 6503cea63..56e933a1c 100644 --- a/WickedEngine/wiScene_BindLua.cpp +++ b/WickedEngine/wiScene_BindLua.cpp @@ -2899,6 +2899,9 @@ Luna::FunctionType CameraComponent_BindLua::methods[] = lunamethod(CameraComponent_BindLua, GetPosition), lunamethod(CameraComponent_BindLua, GetLookDirection), lunamethod(CameraComponent_BindLua, GetUpDirection), + lunamethod(CameraComponent_BindLua, SetPosition), + lunamethod(CameraComponent_BindLua, SetLookDirection), + lunamethod(CameraComponent_BindLua, SetUpDirection), { NULL, NULL } }; Luna::PropertyType CameraComponent_BindLua::properties[] = { @@ -3094,6 +3097,63 @@ int CameraComponent_BindLua::GetUpDirection(lua_State* L) Luna::push(L, new Vector_BindLua(component->GetUp())); return 1; } +int CameraComponent_BindLua::SetPosition(lua_State* L) +{ + int argc = wi::lua::SGetArgCount(L); + if (argc > 0) + { + Vector_BindLua* v = Luna::lightcheck(L, 1); + if (v) + { + component->Eye.x = v->data.x; + component->Eye.y = v->data.y; + component->Eye.z = v->data.z; + } + } + else + { + wi::lua::SError(L, "SetPosition(Vector value) not enough arguments!"); + } + return 1; +} +int CameraComponent_BindLua::SetLookDirection(lua_State* L) +{ + int argc = wi::lua::SGetArgCount(L); + if (argc > 0) + { + Vector_BindLua* v = Luna::lightcheck(L, 1); + if (v) + { + component->At.x = v->data.x; + component->At.y = v->data.y; + component->At.z = v->data.z; + } + } + else + { + wi::lua::SError(L, "SetLookDirection(Vector value) not enough arguments!"); + } + return 1; +} +int CameraComponent_BindLua::SetUpDirection(lua_State* L) +{ + int argc = wi::lua::SGetArgCount(L); + if (argc > 0) + { + Vector_BindLua* v = Luna::lightcheck(L, 1); + if (v) + { + component->Up.x = v->data.x; + component->Up.y = v->data.y; + component->Up.z = v->data.z; + } + } + else + { + wi::lua::SError(L, "SetUpDirection(Vector value) not enough arguments!"); + } + return 1; +} diff --git a/WickedEngine/wiScene_BindLua.h b/WickedEngine/wiScene_BindLua.h index 2edc52696..042dae9d8 100644 --- a/WickedEngine/wiScene_BindLua.h +++ b/WickedEngine/wiScene_BindLua.h @@ -309,6 +309,9 @@ namespace wi::lua::scene int GetPosition(lua_State* L); int GetLookDirection(lua_State* L); int GetUpDirection(lua_State* L); + int SetPosition(lua_State* L); + int SetLookDirection(lua_State* L); + int SetUpDirection(lua_State* L); }; class AnimationComponent_BindLua diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index a97c5e60a..6a4b9c663 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wi::version // minor features, major updates, breaking compatibility changes const int minor = 71; // minor bug fixes, alterations, refactors, updates - const int revision = 89; + const int revision = 90; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);