Tesseract-Engine/assets/scripts/camera.lua

73 lines
1.9 KiB
Lua
Raw Normal View History

local Engine = require("./assets/scripts/engine")
2025-01-01 07:36:53 +00:00
local KeyCode = require("./assets/scripts/keycode")
local transform = nil
local new_rotation = 0
2025-01-03 04:16:50 +00:00
local new_rotationSpeed = 120
local Move_Speec = 600
2025-01-01 07:36:53 +00:00
function OnInit()
local gameObject = Engine.GetGameObjectByTag("Camera")
transform = gameObject:GetComponent("Transform")
2025-01-02 20:36:22 +00:00
Engine.Expose("Position X", "nil")
Engine.Log("Init Done")
end
function OnUpdate(deltaTime)
2025-01-01 07:36:53 +00:00
local gameObject = Engine.GetGameObjectByTag("Camera")
transform = gameObject:GetComponent("Transform")
local old_rotation = transform:GetRotation()
2025-01-02 20:36:22 +00:00
2025-01-01 07:36:53 +00:00
local Position = transform:GetPosition()
if Engine.KeyDown(KeyCode.W) then
Position.x = Position.x + (deltaTime * Move_Speec)
end
if Engine.KeyDown(KeyCode.S) then
Position.x = Position.x - (deltaTime * Move_Speec)
end
if Engine.KeyDown(KeyCode.D) then
Position.z = Position.z + (deltaTime * Move_Speec)
end
if Engine.KeyDown(KeyCode.A) then
Position.z = Position.z - (deltaTime * Move_Speec)
end
transform:SetPosition(Position)
2025-01-02 20:36:22 +00:00
Engine.Expose("Position X", Position.x)
2025-01-03 04:16:50 +00:00
Engine.Expose("Position Y", Position.y)
Engine.Expose("Rotation", new_rotation)
2025-01-02 20:36:22 +00:00
2025-01-01 07:36:53 +00:00
local rotation = {
x = old_rotation.x, -- Preserving existing new_rotation on X-axis
y = new_rotation, -- Updated new_rotation on Y-axis for spinning
z = old_rotation.z -- Preserving existing new_rotation on Z-axis
}
-- Apply the new new_rotation to the Transform component
transform:SetRotation(rotation)
2025-01-03 04:16:50 +00:00
if Engine.KeyDown(KeyCode.Q) then
new_rotation = new_rotation - (deltaTime * new_rotationSpeed)
end
if Engine.KeyDown(KeyCode.E) then
new_rotation = new_rotation + (deltaTime * new_rotationSpeed)
end
2025-01-01 07:36:53 +00:00
if new_rotation >= 360 then
new_rotation = 0
end
end