Files
sampleproj/Assets/lua/scene.lua
Jan Racek 1fdb223f5d lul
2026-03-29 09:34:46 +02:00

194 lines
5.8 KiB
Lua

-- ============================================================================
-- scene.lua - Scene-level script for the PSXSplash test scene
-- ============================================================================
-- Tests: onSceneCreationStart, onSceneCreationEnd, onTriggerEnter/Exit (scene),
-- Persist, UI, Audio, Controls, Cutscene, Scene, Timer, Debug, PSXMath
-- HUD handles (resolved in onSceneCreationEnd)
local hudCanvas = -1
local scoreText = -1
local statusText = -1
local healthBar = -1
local dialogueCanvas = -1
local dialogueText = -1
-- State
local health = 100
local inDialogue = false
local dialogueLines = {}
local dialogueLine = 0
function onSceneCreationStart()
Debug.Log("=== PSXSplash Test Scene ===")
Debug.Log("Scene index: " .. Scene.GetIndex())
-- Restore score from previous scene (tests Persist)
local score = Persist.Get("score") or 0
Debug.Log("Loaded score from persist: " .. score)
end
function onSceneCreationEnd()
Debug.Log("Scene creation complete at frame " .. Timer.GetFrameCount())
-- Resolve UI handles
hudCanvas = UI.FindCanvas("HUD")
if hudCanvas >= 0 then
scoreText = UI.FindElement(hudCanvas, "ScoreText")
statusText = UI.FindElement(hudCanvas, "StatusText")
healthBar = UI.FindElement(hudCanvas, "HealthBar")
UI.SetCanvasVisible(hudCanvas, true)
updateScoreDisplay()
updateHealthBar()
setStatus("Welcome! Explore the test room.")
end
dialogueCanvas = UI.FindCanvas("Dialogue")
if dialogueCanvas >= 0 then
dialogueText = UI.FindElement(dialogueCanvas, "DialogueText")
UI.SetCanvasVisible(dialogueCanvas, false)
end
-- Play ambient looping cutscene (tests loop feature)
Cutscene.Play("ambient_spin", {loop = true})
-- Test PSXMath functions
local clamped = PSXMath.Clamp(150, 0, 100)
local lerped = PSXMath.Lerp(0, 10, 1)
local sign = PSXMath.Sign(-42)
local abs = PSXMath.Abs(-7)
local mn = PSXMath.Min(3, 5)
local mx = PSXMath.Max(3, 5)
Debug.Log("PSXMath tests: clamp=" .. clamped .. " lerp=" .. lerped
.. " sign=" .. sign .. " abs=" .. abs .. " min=" .. mn .. " max=" .. mx)
-- Test Vec3 functions
local a = Vec3.new(1, 0, 0)
local b = Vec3.new(0, 1, 0)
local c = Vec3.add(a, b)
local d = Vec3.cross(a, b)
local dist = Vec3.distance(a, b)
Debug.Log("Vec3 tests: add=" .. c.x .. "," .. c.y .. "," .. c.z
.. " cross=" .. d.x .. "," .. d.y .. "," .. d.z
.. " dist=" .. dist)
end
-- ============================================================================
-- Scene-level trigger callbacks (PSXTriggerBox)
-- ============================================================================
function onTriggerEnter(triggerIndex)
Debug.Log("Trigger entered: " .. triggerIndex)
if triggerIndex == 0 then
-- Trigger 0: Cutscene zone
setStatus("Cutscene playing...")
Controls.SetEnabled(false)
Cutscene.Play("camera_flyover", {
onComplete = function()
Controls.SetEnabled(true)
setStatus("Cutscene complete!")
end
})
elseif triggerIndex == 1 then
-- Trigger 1: Damage zone
health = PSXMath.Clamp(health - 25, 0, 100)
updateHealthBar()
setStatus("Ouch! Health: " .. health)
if health <= 0 then
setStatus("You died! Reloading...")
Controls.SetEnabled(false)
Persist.Set("score", 0)
Scene.Load(Scene.GetIndex())
end
elseif triggerIndex == 2 then
-- Trigger 2: Scene transition portal
setStatus("Loading next scene...")
Persist.Set("came_from", Scene.GetIndex())
Scene.Load(0) -- Reload scene 0 as demo
end
end
function onTriggerExit(triggerIndex)
Debug.Log("Trigger exited: " .. triggerIndex)
if triggerIndex == 0 then
setStatus("Left cutscene zone")
elseif triggerIndex == 1 then
setStatus("Escaped damage zone")
end
end
-- ============================================================================
-- Dialogue system (called from NPC scripts)
-- ============================================================================
function startDialogue(lines)
if inDialogue then return end
inDialogue = true
dialogueLines = lines
dialogueLine = 1
Controls.SetEnabled(false)
showDialogueLine()
end
function showDialogueLine()
if dialogueCanvas >= 0 then
UI.SetCanvasVisible(dialogueCanvas, true)
if dialogueText >= 0 and dialogueLine <= #dialogueLines then
UI.SetText(dialogueText, dialogueLines[dialogueLine])
end
end
end
function advanceDialogue()
if not inDialogue then return end
dialogueLine = dialogueLine + 1
if dialogueLine > #dialogueLines then
endDialogue()
else
showDialogueLine()
end
end
function endDialogue()
inDialogue = false
Controls.SetEnabled(true)
if dialogueCanvas >= 0 then
UI.SetCanvasVisible(dialogueCanvas, false)
end
end
function isInDialogue()
return inDialogue
end
-- ============================================================================
-- Helpers
-- ============================================================================
function updateScoreDisplay()
if scoreText >= 0 then
local score = Persist.Get("score") or 0
UI.SetText(scoreText, "Score: " .. score)
end
end
function updateHealthBar()
if healthBar >= 0 then
UI.SetProgress(healthBar, health)
end
end
function setStatus(msg)
Debug.Log("[Status] " .. msg)
if statusText >= 0 then
UI.SetText(statusText, msg)
end
end
function addScore(amount)
local score = (Persist.Get("score") or 0) + amount
Persist.Set("score", score)
updateScoreDisplay()
end