From 91133a0396fc1921a24ac2ce69568e8cebea9ac9 Mon Sep 17 00:00:00 2001 From: OusmBlueNinja <89956790+OusmBlueNinja@users.noreply.github.com> Date: Fri, 3 Jan 2025 14:28:20 -0600 Subject: [PATCH] Updated Lua API To Have InteliSence --- assets/scripts/camera.lua | 2 + assets/scripts/engine.lua | 10 +- assets/scripts/keycode.lua | 127 +++++++++++++++++- assets/scripts/unitTests.lua | 240 ----------------------------------- imgui.ini | 26 ++-- scenes/Default.scene | 22 ++-- 6 files changed, 161 insertions(+), 266 deletions(-) delete mode 100644 assets/scripts/unitTests.lua diff --git a/assets/scripts/camera.lua b/assets/scripts/camera.lua index 79a4b3a..ce44d13 100644 --- a/assets/scripts/camera.lua +++ b/assets/scripts/camera.lua @@ -1,6 +1,8 @@ local Engine = require("./assets/scripts/engine") local KeyCode = require("./assets/scripts/keycode") + + local transform = nil local currentRotation = 0 -- Current Y-axis rotation in degrees diff --git a/assets/scripts/engine.lua b/assets/scripts/engine.lua index a031e32..8cf5ccb 100644 --- a/assets/scripts/engine.lua +++ b/assets/scripts/engine.lua @@ -1 +1,9 @@ -return _T_Engine_Table -- This just returns the Global Table that the engine creates \ No newline at end of file +local Engine = { + Log = _T_Engine_Table.Log, + Expose = _T_Engine_Table.Expose, + KeyDown = _T_Engine_Table.KeyDown, + ScriptName = _T_Engine_Table.ScriptName, + GetGameObjectByTag = _T_Engine_Table.GetGameObjectByTag +} + +return Engine diff --git a/assets/scripts/keycode.lua b/assets/scripts/keycode.lua index e472082..53117ac 100644 --- a/assets/scripts/keycode.lua +++ b/assets/scripts/keycode.lua @@ -1 +1,126 @@ -return _T_KeyCode_Table \ No newline at end of file + +local _Engine_KeyCodes = _T_KeyCode_Table +local KeyCodes = { + Space = _Engine_KeyCodes.Space, + Apostrophe = _Engine_KeyCodes.Apostrophe, + Comma = _Engine_KeyCodes.Comma, + Minus = _Engine_KeyCodes.Minus, + Period = _Engine_KeyCodes.Period, + Slash = _Engine_KeyCodes.Slash, + D0 = _Engine_KeyCodes.D0, + D1 = _Engine_KeyCodes.D1, + D2 = _Engine_KeyCodes.D2, + D3 = _Engine_KeyCodes.D3, + D4 = _Engine_KeyCodes.D4, + D5 = _Engine_KeyCodes.D5, + D6 = _Engine_KeyCodes.D6, + D7 = _Engine_KeyCodes.D7, + D8 = _Engine_KeyCodes.D8, + D9 = _Engine_KeyCodes.D9, + Semicolon = _Engine_KeyCodes.Semicolon, + Equal = _Engine_KeyCodes.Equal, + A = _Engine_KeyCodes.A, + B = _Engine_KeyCodes.B, + C = _Engine_KeyCodes.C, + D = _Engine_KeyCodes.D, + E = _Engine_KeyCodes.E, + F = _Engine_KeyCodes.F, + G = _Engine_KeyCodes.G, + H = _Engine_KeyCodes.H, + I = _Engine_KeyCodes.I, + J = _Engine_KeyCodes.J, + K = _Engine_KeyCodes.K, + L = _Engine_KeyCodes.L, + M = _Engine_KeyCodes.M, + N = _Engine_KeyCodes.N, + O = _Engine_KeyCodes.O, + P = _Engine_KeyCodes.P, + Q = _Engine_KeyCodes.Q, + R = _Engine_KeyCodes.R, + S = _Engine_KeyCodes.S, + T = _Engine_KeyCodes.T, + U = _Engine_KeyCodes.U, + V = _Engine_KeyCodes.V, + W = _Engine_KeyCodes.W, + X = _Engine_KeyCodes.X, + Y = _Engine_KeyCodes.Y, + Z = _Engine_KeyCodes.Z, + LeftBracket = _Engine_KeyCodes.LeftBracket, + Backslash = _Engine_KeyCodes.Backslash, + RightBracket = _Engine_KeyCodes.RightBracket, + GraveAccent = _Engine_KeyCodes.GraveAccent, + World1 = _Engine_KeyCodes.World1, + World2 = _Engine_KeyCodes.World2, + Escape = _Engine_KeyCodes.Escape, + Enter = _Engine_KeyCodes.Enter, + Tab = _Engine_KeyCodes.Tab, + Backspace = _Engine_KeyCodes.Backspace, + Insert = _Engine_KeyCodes.Insert, + Delete = _Engine_KeyCodes.Delete, + Right = _Engine_KeyCodes.Right, + Left = _Engine_KeyCodes.Left, + Down = _Engine_KeyCodes.Down, + Up = _Engine_KeyCodes.Up, + PageUp = _Engine_KeyCodes.PageUp, + PageDown = _Engine_KeyCodes.PageDown, + Home = _Engine_KeyCodes.Home, + End = _Engine_KeyCodes.End, + CapsLock = _Engine_KeyCodes.CapsLock, + ScrollLock = _Engine_KeyCodes.ScrollLock, + NumLock = _Engine_KeyCodes.NumLock, + PrintScreen = _Engine_KeyCodes.PrintScreen, + Pause = _Engine_KeyCodes.Pause, + F1 = _Engine_KeyCodes.F1, + F2 = _Engine_KeyCodes.F2, + F3 = _Engine_KeyCodes.F3, + F4 = _Engine_KeyCodes.F4, + F5 = _Engine_KeyCodes.F5, + F6 = _Engine_KeyCodes.F6, + F7 = _Engine_KeyCodes.F7, + F8 = _Engine_KeyCodes.F8, + F9 = _Engine_KeyCodes.F9, + F10 = _Engine_KeyCodes.F10, + F11 = _Engine_KeyCodes.F11, + F12 = _Engine_KeyCodes.F12, + F13 = _Engine_KeyCodes.F13, + F14 = _Engine_KeyCodes.F14, + F15 = _Engine_KeyCodes.F15, + F16 = _Engine_KeyCodes.F16, + F17 = _Engine_KeyCodes.F17, + F18 = _Engine_KeyCodes.F18, + F19 = _Engine_KeyCodes.F19, + F20 = _Engine_KeyCodes.F20, + F21 = _Engine_KeyCodes.F21, + F22 = _Engine_KeyCodes.F22, + F23 = _Engine_KeyCodes.F23, + F24 = _Engine_KeyCodes.F24, + F25 = _Engine_KeyCodes.F25, + KP0 = _Engine_KeyCodes.KP0, + KP1 = _Engine_KeyCodes.KP1, + KP2 = _Engine_KeyCodes.KP2, + KP3 = _Engine_KeyCodes.KP3, + KP4 = _Engine_KeyCodes.KP4, + KP5 = _Engine_KeyCodes.KP5, + KP6 = _Engine_KeyCodes.KP6, + KP7 = _Engine_KeyCodes.KP7, + KP8 = _Engine_KeyCodes.KP8, + KP9 = _Engine_KeyCodes.KP9, + KPDecimal = _Engine_KeyCodes.KPDecimal, + KPDivide = _Engine_KeyCodes.KPDivide, + KPMultiply = _Engine_KeyCodes.KPMultiply, + KPSubtract = _Engine_KeyCodes.KPSubtract, + KPAdd = _Engine_KeyCodes.KPAdd, + KPEnter = _Engine_KeyCodes.KPEnter, + KPEqual = _Engine_KeyCodes.KPEqual, + LeftShift = _Engine_KeyCodes.LeftShift, + LeftControl = _Engine_KeyCodes.LeftControl, + LeftAlt = _Engine_KeyCodes.LeftAlt, + LeftSuper = _Engine_KeyCodes.LeftSuper, + RightShift = _Engine_KeyCodes.RightShift, + RightControl = _Engine_KeyCodes.RightControl, + RightAlt = _Engine_KeyCodes.RightAlt, + RightSuper = _Engine_KeyCodes.RightSuper, + Menu = _Engine_KeyCodes.Menu +} + +return KeyCodes diff --git a/assets/scripts/unitTests.lua b/assets/scripts/unitTests.lua deleted file mode 100644 index fd0e901..0000000 --- a/assets/scripts/unitTests.lua +++ /dev/null @@ -1,240 +0,0 @@ --- unitTests.lua --- Unit Tests for C++ Lua API and Game Engine - -local Math = require("./assets/scripts/math") -local Engine = require("./assets/scripts/engine") - - -local function OnInit() - -- Log a message with a custom red color - Engine.Log("This is a red message.", {1.0, 0.0, 0.0, 1.0}) - - -- Log a message with a custom green color - Engine.Log("This is a green message.", {0.0, 1.0, 0.0, 1.0}) - - -- Log a message with a custom blue color - Engine.Log("This is a blue message.", {0.0, 0.0, 1.0, 1.0}) - - - - -end - - - --- Simple Test Framework - -local Tests = {} -local passed = 0 -local failed = 0 - --- Assertion Functions -local function assertEquals(expected, actual, message) - if expected ~= actual then - error(message or string.format("Assertion Failed: expected '%s', got '%s'", tostring(expected), tostring(actual))) - end -end - -local function assertTableEquals(expected, actual, message) - if type(expected) ~= "table" or type(actual) ~= "table" then - error(message or "Assertion Failed: One of the values is not a table.") - end - for k, v in pairs(expected) do - if actual[k] ~= v then - error(message or string.format("Assertion Failed: For key '%s', expected '%s', got '%s'", tostring(k), tostring(v), tostring(actual[k]))) - end - end -end - -local function assertNotNil(value, message) - if value == nil then - error(message or "Assertion Failed: value is nil.") - end -end - -local function assertTrue(condition, message) - if not condition then - error(message or "Assertion Failed: condition is not true.") - end -end - --- Helper Function to Capture Logs -local capturedLogs = {} -local originalLog = Engine.Log - -local function mockLog(message, color) - table.insert(capturedLogs, {message = message, color = color}) -end - --- Register Test Cases -function Tests.testEngineLog() - -- Replace Engine.Log with mockLog - Engine.Log = mockLog - - -- Call OnInit to generate logs - OnInit() - - -- Restore original Engine.Log - Engine.Log = originalLog - - -- Assertions - assertEquals(3, #capturedLogs, "OnInit should log exactly 3 messages.") - - -- Check first log (Red Message) - assertEquals("This is a red message.", capturedLogs[1].message) - assertTableEquals({1.0, 0.0, 0.0, 1.0}, capturedLogs[1].color, "First log color mismatch.") - - -- Check second log (Green Message) - assertEquals("This is a green message.", capturedLogs[2].message) - assertTableEquals({0.0, 1.0, 0.0, 1.0}, capturedLogs[2].color, "Second log color mismatch.") - - -- Check third log (Blue Message) - assertEquals("This is a blue message.", capturedLogs[3].message) - assertTableEquals({0.0, 0.0, 1.0, 1.0}, capturedLogs[3].color, "Third log color mismatch.") -end - -function Tests.testGetGameObjectByTag() - -- Assume "Player" and "Gun" GameObjects exist - local player = Engine.GetGameObjectByTag("Player") - assertNotNil(player, "Player GameObject should exist.") - - local gun = Engine.GetGameObjectByTag("Gun") - assertNotNil(gun, "Gun GameObject should exist.") - - -- Test for a non-existent tag - local nonExistent = Engine.GetGameObjectByTag("NonExistentTag") - assertEquals(nil, nonExistent, "Non-existent GameObject should return nil.") -end - -function Tests.testGameObjectGetName() - local player = Engine.GetGameObjectByTag("Player") - assertNotNil(player, "Player GameObject should exist.") - - local name = player:GetName() - assertEquals("Player", name, "Player GameObject name should be 'Player'.") -end - -function Tests.testGameObjectGetComponent() - local player = Engine.GetGameObjectByTag("Player") - assertNotNil(player, "Player GameObject should exist.") - - local transform = player:GetComponent("Transform") - assertNotNil(transform, "Player should have a Transform component.") - - local nonExistentComponent = player:GetComponent("NonExistentComponent") - assertEquals(nil, nonExistentComponent, "Non-existent component should return nil.") -end - -function Tests.testTransformComponentPosition() - local player = Engine.GetGameObjectByTag("Player") - assertNotNil(player, "Player GameObject should exist.") - - local transform = player:GetComponent("Transform") - assertNotNil(transform, "Player should have a Transform component.") - - -- Get initial position - local position = transform:GetPosition() - assertNotNil(position, "Transform:GetPosition should return a position table.") - assertEquals(0.0, position.x, "Initial X position should be 0.0.") - assertEquals(2.0, position.y, "Initial Y position should be 2.0.") - assertEquals(-12.0, position.z, "Initial Z position should be -12.0.") - - -- Set new position - local newPosition = {x = 5.0, y = 10.0, z = -15.0} - transform:SetPosition(newPosition) - - -- Get updated position - local updatedPosition = transform:GetPosition() - assertTableEquals(newPosition, updatedPosition, "Transform:SetPosition did not update position correctly.") -end - -function Tests.testTransformComponentRotation() - local player = Engine.GetGameObjectByTag("Player") - assertNotNil(player, "Player GameObject should exist.") - - local transform = player:GetComponent("Transform") - assertNotNil(transform, "Player should have a Transform component.") - - -- Get initial rotation - local rotation = transform:GetRotation() - assertNotNil(rotation, "Transform:GetRotation should return a rotation table.") - - -- Set new rotation - local newRotation = {x = 45.0, y = 90.0, z = 135.0} - transform:SetRotation(newRotation) - - -- Get updated rotation - local updatedRotation = transform:GetRotation() - assertTableEquals(newRotation, updatedRotation, "Transform:SetRotation did not update rotation correctly.") -end - -function Tests.testOnUpdateFunctionality() - -- Mock GameObject and Transform - local mockPlayer = { - GetName = function() return "Player" end, - GetComponent = function(componentName) - if componentName == "Transform" then - return { - GetRotation = function() - return {x = new_rotation, y = new_rotation, z = new_rotation} - end, - SetRotation = function(rotation) - new_rotation = rotation.x - end - } - end - return nil - end - } - - -- Replace Engine.GetGameObjectByTag to return mockPlayer - local originalGetGameObjectByTag = Engine.GetGameObjectByTag - Engine.GetGameObjectByTag = function(tag) - if tag == "Player" then - return mockPlayer - end - return originalGetGameObjectByTag(tag) - end - - -- Initialize rotation - new_rotation = 0 - speed = 50 - - -- Call OnUpdate with deltaTime = 1 - OnUpdate(1) - - -- Check new_rotation - assertEquals(50, new_rotation, "After first OnUpdate, new_rotation should be 50.") - - -- Call OnUpdate with deltaTime = 14 (to exceed 720) - OnUpdate(14) - - -- new_rotation should reset to 0 after exceeding 720 - assertEquals(720 + 50 - 720, new_rotation, "After exceeding 720, new_rotation should reset appropriately.") - - -- Restore original Engine.GetGameObjectByTag - Engine.GetGameObjectByTag = originalGetGameObjectByTag -end - --- Add more test functions below as needed - --- Test Runner -local function runTests() - print("Starting Unit Tests...\n") - for testName, testFunc in pairs(Tests) do - io.write(string.format("Running %s... ", testName)) - local status, err = pcall(testFunc) - if status then - print("PASSED") - passed = passed + 1 - else - print("FAILED") - print(" Error:", err) - failed = failed + 1 - end - end - print(string.format("\nUnit Tests Completed: %d Passed, %d Failed", passed, failed)) -end - --- Execute Tests -runTests() diff --git a/imgui.ini b/imgui.ini index a73f4d8..d508698 100644 --- a/imgui.ini +++ b/imgui.ini @@ -1,6 +1,6 @@ [Window][DockSpace] Pos=0,0 -Size=1920,1177 +Size=1280,720 Collapsed=0 [Window][Debug##Default] @@ -80,8 +80,8 @@ Collapsed=0 DockId=0x0000001F,0 [Window][Performance##performance] -Pos=8,702 -Size=335,467 +Pos=8,432 +Size=335,280 Collapsed=0 DockId=0x0000001C,0 @@ -105,7 +105,7 @@ DockId=0x0000000F,0 [Window][Scene Window##SceneWindow] Pos=8,28 -Size=335,672 +Size=335,402 Collapsed=0 DockId=0x0000001B,0 @@ -134,26 +134,26 @@ Collapsed=0 DockId=0x0000001E,0 [Window][ Logger##logger] -Pos=345,846 -Size=1189,323 +Pos=345,389 +Size=549,323 Collapsed=0 DockId=0x00000025,0 [Window][ Editor##EditorWindow] Pos=345,28 -Size=1189,816 +Size=549,359 Collapsed=0 DockId=0x0000001F,0 [Window][ Inspector##InspectorWindow] -Pos=1536,28 -Size=376,1141 +Pos=896,28 +Size=376,684 Collapsed=0 DockId=0x00000022,0 [Window][ Profiler] -Pos=345,846 -Size=1189,323 +Pos=345,389 +Size=549,323 Collapsed=0 DockId=0x00000025,1 @@ -170,7 +170,7 @@ Column 2 Weight=0.9665 Column 3 Weight=0.6950 [Docking][Data] -DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,51 Size=1904,1141 Split=X Selected=0xF7365A5A +DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=198,241 Size=1264,684 Split=X Selected=0xF7365A5A DockNode ID=0x00000020 Parent=0x14621557 SizeRef=886,684 Split=X DockNode ID=0x00000013 Parent=0x00000020 SizeRef=335,1142 Split=Y Selected=0x818D04BB DockNode ID=0x0000001B Parent=0x00000013 SizeRef=264,672 HiddenTabBar=1 Selected=0x1D5D92B6 @@ -193,7 +193,7 @@ DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,51 Size= DockNode ID=0x00000017 Parent=0x0000000D SizeRef=1202,776 Split=Y Selected=0xDFF75B3F DockNode ID=0x0000001D Parent=0x00000017 SizeRef=518,720 Split=Y Selected=0x9A7B23B9 DockNode ID=0x0000001F Parent=0x0000001D SizeRef=549,359 CentralNode=1 Selected=0x9A7B23B9 - DockNode ID=0x00000025 Parent=0x0000001D SizeRef=549,323 Selected=0x7A66B86B + DockNode ID=0x00000025 Parent=0x0000001D SizeRef=549,323 Selected=0x1F29F1F5 DockNode ID=0x0000001E Parent=0x00000017 SizeRef=518,417 Selected=0xC74E1AEE DockNode ID=0x00000018 Parent=0x0000000D SizeRef=1202,364 Split=X Selected=0x1C0788A1 DockNode ID=0x00000019 Parent=0x00000018 SizeRef=601,364 Selected=0x1C0788A1 diff --git a/scenes/Default.scene b/scenes/Default.scene index 99df807..dfed3fe 100644 --- a/scenes/Default.scene +++ b/scenes/Default.scene @@ -2,6 +2,10 @@ Entities: - ID: 0 Name: Bacround Components: + Transform: + Position: [0, 300, 0] + Rotation: [0, 0, 0] + Scale: [1, 1, 1] Mesh: MeshPath: assets/models/sponza.obj submeshes_len: 26 @@ -156,23 +160,19 @@ Entities: - id: 26 type: texture_diffuse path: textures/vase_hanging.tga - Transform: - Position: [0, 300, 0] - Rotation: [0, 0, 0] - Scale: [1, 1, 1] - ID: 1 Name: Camera Components: + ScriptComponent: + ScriptPath: assets/scripts/camera.lua + Transform: + Position: [206.62178, 200, 9.92187023] + Rotation: [0, 356.40271, 0] + Scale: [1, 1, 1] CameraComponent: IsPerspective: true DefaultRuntimeCamera: true FOV: 45 AspectRatio: 1.75 NearPlane: 0.100000001 - FarPlane: 7000 - Transform: - Position: [-130.615494, 200, -13.0714865] - Rotation: [0, -360.790955, 0] - Scale: [1, 1, 1] - ScriptComponent: - ScriptPath: assets/scripts/camera.lua \ No newline at end of file + FarPlane: 7000 \ No newline at end of file