Tesseract-Engine/assets/scripts/camera.lua

56 lines
1.5 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
local new_rotationSpeed = 15
2025-01-01 07:36:53 +00:00
local Move_Speec = 300
2025-01-01 07:36:53 +00:00
function OnInit()
local gameObject = Engine.GetGameObjectByTag("Camera")
transform = gameObject:GetComponent("Transform")
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()
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)
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)
new_rotation = new_rotation + (deltaTime * new_rotationSpeed)
if new_rotation >= 360 then
new_rotation = 0
end
end