Tesseract-Engine/assets/scripts/camera.lua

117 lines
3.6 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
2025-01-03 15:58:58 +00:00
local currentRotation = 0 -- Current Y-axis rotation in degrees
2025-01-03 15:58:58 +00:00
local rotationSpeed = 120 -- Degrees per second
local moveSpeed = 600 -- Units per second
2025-01-01 07:36:53 +00:00
function OnInit()
local gameObject = Engine.GetGameObjectByTag("Camera")
2025-01-03 15:58:58 +00:00
if not gameObject then
Engine.Log("Error: Camera GameObject not found!")
return
end
2025-01-02 20:36:22 +00:00
2025-01-03 15:58:58 +00:00
transform = gameObject:GetComponent("Transform")
if not transform then
Engine.Log("Error: Transform component not found on Camera!")
return
end
-- Initialize currentRotation based on the transform's current rotation
local rotation = transform:GetRotation()
currentRotation = rotation.y
Engine.Expose("Position X", rotation.x)
Engine.Expose("Position Y", rotation.y)
Engine.Expose("Rotation Y", currentRotation)
Engine.Log("Init Done")
end
function OnUpdate(deltaTime)
2025-01-03 15:58:58 +00:00
if not transform then
Engine.Log("Warning: Transform is not initialized.")
return
end
2025-01-02 20:36:22 +00:00
2025-01-03 15:58:58 +00:00
-- Retrieve current rotation and position
local rotation = transform:GetRotation()
local position = transform:GetPosition()
2025-01-01 07:36:53 +00:00
2025-01-03 15:58:58 +00:00
-- Update currentRotation based on input
if Engine.KeyDown(KeyCode.Q) then
currentRotation = currentRotation - (deltaTime * rotationSpeed)
2025-01-01 07:36:53 +00:00
end
2025-01-03 15:58:58 +00:00
if Engine.KeyDown(KeyCode.E) then
currentRotation = currentRotation + (deltaTime * rotationSpeed)
2025-01-01 07:36:53 +00:00
end
2025-01-03 15:58:58 +00:00
-- Normalize rotation to [0, 360) degrees
if currentRotation >= 360 then
currentRotation = currentRotation - 360
elseif currentRotation < 0 then
currentRotation = currentRotation + 360
end
2025-01-03 04:16:50 +00:00
2025-01-03 15:58:58 +00:00
-- Apply the updated rotation to the Transform component
local newRotation = {
x = rotation.x, -- Preserving existing rotation on X-axis
y = currentRotation, -- Updated rotation on Y-axis
z = rotation.z -- Preserving existing rotation on Z-axis
}
transform:SetRotation(newRotation)
2025-01-02 20:36:22 +00:00
2025-01-03 15:58:58 +00:00
-- Calculate movement direction based on currentRotation
local rotationRad = math.rad(currentRotation)
2025-01-01 07:36:53 +00:00
2025-01-03 15:58:58 +00:00
-- Forward vector (positive Z direction)
local forward = {
x = math.cos(rotationRad),
z = math.sin(rotationRad)
}
2025-01-01 07:36:53 +00:00
2025-01-03 15:58:58 +00:00
-- Right vector (positive X direction)
local right = {
x = math.sin(rotationRad),
z = -math.cos(rotationRad)
2025-01-01 07:36:53 +00:00
}
2025-01-03 04:16:50 +00:00
2025-01-03 15:58:58 +00:00
-- Initialize movement deltas
local deltaX = 0
local deltaZ = 0
2025-01-03 04:16:50 +00:00
2025-01-03 15:58:58 +00:00
if Engine.KeyDown(KeyCode.W) then
-- Move forward
deltaX = deltaX + forward.x * moveSpeed * deltaTime
deltaZ = deltaZ + forward.z * moveSpeed * deltaTime
2025-01-03 04:16:50 +00:00
end
2025-01-03 15:58:58 +00:00
if Engine.KeyDown(KeyCode.S) then
-- Move backward
deltaX = deltaX - forward.x * moveSpeed * deltaTime
deltaZ = deltaZ - forward.z * moveSpeed * deltaTime
end
if Engine.KeyDown(KeyCode.A) then
-- Move right
deltaX = deltaX + right.x * moveSpeed * deltaTime
deltaZ = deltaZ + right.z * moveSpeed * deltaTime
2025-01-03 04:16:50 +00:00
end
2025-01-03 15:58:58 +00:00
if Engine.KeyDown(KeyCode.D) then
-- Move left
deltaX = deltaX - right.x * moveSpeed * deltaTime
deltaZ = deltaZ - right.z * moveSpeed * deltaTime
2025-01-01 07:36:53 +00:00
end
2025-01-03 15:58:58 +00:00
-- Update position based on calculated deltas
position.x = position.x + deltaX
position.z = position.z + deltaZ
transform:SetPosition(position)
2025-01-03 20:18:56 +00:00
-- Expose updated position and rotation
2025-01-03 15:58:58 +00:00
Engine.Expose("Position X", position.x)
Engine.Expose("Position Y", position.y)
Engine.Expose("Rotation Y", currentRotation)
2025-01-01 07:36:53 +00:00
end