Files
sampleproj/Assets/lua/scene.lua
2026-03-29 13:18:13 +02:00

201 lines
6.2 KiB
Lua

-- ============================================================================
-- scene.lua - Scene-level script for the PSXSplash test scene
-- ============================================================================
-- Tests: onSceneCreationStart, onSceneCreationEnd, Persist, UI, Audio,
-- Controls, Cutscene, Scene, Timer, Debug, PSXMath, Vec3
-- 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
Cutscene.Play("ambient_spin", {loop = true})
-- Test PSXMath functions (all integer args)
local clamped = PSXMath.Clamp(150, 0, 100)
local lerped = PSXMath.Lerp(0, 10, 1/2)
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: 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: add=" .. c.x .. "," .. c.y .. "," .. c.z
.. " cross=" .. d.x .. "," .. d.y .. "," .. d.z
.. " dist=" .. dist)
local scaled = Vec3.mul(a, 3)
local dotVal = Vec3.dot(a, b)
local lenSq = Vec3.lengthSq(Vec3.new(3, 4, 0))
local norm = Vec3.normalize(Vec3.new(0, 5, 0))
Debug.Log("Vec3: mul=" .. scaled.x .. "," .. scaled.y .. "," .. scaled.z
.. " dot=" .. dotVal .. " lenSq=" .. lenSq
.. " norm=" .. norm.x .. "," .. norm.y .. "," .. norm.z)
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
-- ============================================================================
-- Health system (called from trigger scripts)
-- ============================================================================
function applyDamage(amount)
health = PSXMath.Clamp(health - amount, 0, 100)
updateHealthBar()
if health <= 0 then
setStatus("You died! Reloading...")
Controls.SetEnabled(false)
Persist.Set("score", 0)
Scene.Load(Scene.GetIndex())
end
end
function applyHeal(amount)
health = PSXMath.Clamp(health + amount, 0, 100)
updateHealthBar()
end
-- ============================================================================
-- Helpers (globals accessible from other scripts)
-- ============================================================================
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)
if health > 50 then
UI.SetProgressColors(healthBar, 0, 40, 0, 0, 255, 0)
elseif health > 25 then
UI.SetProgressColors(healthBar, 40, 40, 0, 255, 255, 0)
else
UI.SetProgressColors(healthBar, 40, 0, 0, 255, 0, 0)
end
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
-- ============================================================================
-- Export shared functions to _G so per-object scripts can call them.
-- Each script gets its own environment with __index = _G as fallback.
-- Functions defined here live in the scene's environment, not _G itself,
-- so we must explicitly publish them.
-- ============================================================================
_G.setStatus = setStatus
_G.addScore = addScore
_G.startDialogue = startDialogue
_G.advanceDialogue = advanceDialogue
_G.isInDialogue = isInDialogue
_G.endDialogue = endDialogue
_G.applyDamage = applyDamage
_G.applyHeal = applyHeal
_G.updateScoreDisplay = updateScoreDisplay
_G.updateHealthBar = updateHealthBar