Better testscene
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
-- ============================================================================
|
||||
-- collectible.lua - Pickup item
|
||||
-- ============================================================================
|
||||
-- Tests: onCreate, onDestroy, onCollideWithPlayer, onEnable, onDisable,
|
||||
-- Tests: onCreate, onDestroy, onEnable, onDisable, onCollideWithPlayer,
|
||||
-- Entity.SetActive, Audio.Play, Persist, self.active, self.position
|
||||
|
||||
local collected = false
|
||||
|
||||
function onCreate(self)
|
||||
collected = false
|
||||
Debug.Log("Collectible created at " .. self.position.x .. "," .. self.position.y .. "," .. self.position.z)
|
||||
Debug.Log("Collectible created at "
|
||||
.. self.position.x .. "," .. self.position.y .. "," .. self.position.z)
|
||||
end
|
||||
|
||||
function onDestroy(self)
|
||||
@@ -28,9 +27,8 @@ function onCollideWithPlayer(self)
|
||||
collected = true
|
||||
|
||||
Audio.Play("collect", 127, 64)
|
||||
|
||||
-- Update score via scene script global
|
||||
Entity.SetActive(self, false)
|
||||
|
||||
addScore(100)
|
||||
setStatus("Collected! +100 points")
|
||||
Entity.SetActive(self, false)
|
||||
end
|
||||
|
||||
18
Assets/lua/cutscene_trigger.lua
Normal file
18
Assets/lua/cutscene_trigger.lua
Normal file
@@ -0,0 +1,18 @@
|
||||
-- cutscene_trigger.lua - Plays a camera flyover cutscene when player enters
|
||||
-- Attach to a PSXTriggerBox in the cutscene area
|
||||
|
||||
local played = false
|
||||
|
||||
function onTriggerEnter()
|
||||
if played then return end
|
||||
played = true
|
||||
setStatus("Cutscene playing...")
|
||||
Controls.SetEnabled(false)
|
||||
Cutscene.Play("camera_flyover", {
|
||||
onComplete = function()
|
||||
Controls.SetEnabled(true)
|
||||
setStatus("Cutscene complete!")
|
||||
played = false -- allow replay
|
||||
end
|
||||
})
|
||||
end
|
||||
7
Assets/lua/cutscene_trigger.lua.meta
Normal file
7
Assets/lua/cutscene_trigger.lua.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
13
Assets/lua/damage_trigger.lua
Normal file
13
Assets/lua/damage_trigger.lua
Normal file
@@ -0,0 +1,13 @@
|
||||
-- damage_trigger.lua - Damages player on entry, heals on exit area
|
||||
-- Attach to a PSXTriggerBox in a hazard zone
|
||||
|
||||
function onTriggerEnter()
|
||||
Debug.Log("Entered damage zone")
|
||||
setStatus("Ouch! Taking damage!")
|
||||
applyDamage(25)
|
||||
end
|
||||
|
||||
function onTriggerExit()
|
||||
Debug.Log("Exited damage zone")
|
||||
setStatus("Escaped the danger!")
|
||||
end
|
||||
7
Assets/lua/damage_trigger.lua.meta
Normal file
7
Assets/lua/damage_trigger.lua.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,34 +1,43 @@
|
||||
-- ============================================================================
|
||||
-- entity_scanner.lua - Scans all entities on interact (Entity.ForEach test)
|
||||
-- ============================================================================
|
||||
-- entity_scanner.lua - Scans all entities on interact
|
||||
-- Tests: Entity.ForEach, Entity.GetPosition, Entity.IsActive, Entity.GetCount,
|
||||
-- Entity.FindByIndex, onInteract
|
||||
-- Entity.FindByIndex, Entity.FindByScriptIndex, Vec3.lerp, Vec3.distanceSq
|
||||
|
||||
function onInteract(self)
|
||||
setStatus("Scanning all entities...")
|
||||
Debug.Log("=== Entity Scan ===")
|
||||
Debug.Log("Total entities: " .. Entity.GetCount())
|
||||
|
||||
local total = Entity.GetCount()
|
||||
Debug.Log("Total entities: " .. total)
|
||||
|
||||
local activeCount = 0
|
||||
local inactiveCount = 0
|
||||
|
||||
Entity.ForEach(function(obj)
|
||||
Entity.ForEach(function(obj, index)
|
||||
local pos = Entity.GetPosition(obj)
|
||||
local active = Entity.IsActive(obj)
|
||||
if active then
|
||||
activeCount = activeCount + 1
|
||||
else
|
||||
inactiveCount = inactiveCount + 1
|
||||
end
|
||||
activeCount = activeCount + 1
|
||||
Debug.Log(" [" .. index .. "] pos="
|
||||
.. pos.x .. "," .. pos.y .. "," .. pos.z
|
||||
.. " active=" .. tostring(Entity.IsActive(obj)))
|
||||
end)
|
||||
|
||||
Debug.Log("Active: " .. activeCount .. " Inactive: " .. inactiveCount)
|
||||
setStatus("Scan: " .. activeCount .. " active, " .. inactiveCount .. " inactive")
|
||||
Debug.Log("Active: " .. activeCount .. " of " .. total)
|
||||
setStatus("Scan: " .. activeCount .. "/" .. total .. " active")
|
||||
|
||||
-- Test FindByIndex
|
||||
local first = Entity.FindByIndex(0)
|
||||
if first then
|
||||
local pos = Entity.GetPosition(first)
|
||||
Debug.Log("Entity 0 at " .. pos.x .. "," .. pos.y .. "," .. pos.z)
|
||||
end
|
||||
|
||||
local byScript = Entity.FindByScriptIndex(0)
|
||||
if byScript then
|
||||
Debug.Log("Found entity using script 0")
|
||||
end
|
||||
|
||||
local a = Vec3.new(0, 0, 0)
|
||||
local b = Vec3.new(10, 20, 30)
|
||||
local mid = Vec3.lerp(a, b, 1/2)
|
||||
Debug.Log("Vec3.lerp midpoint: " .. mid.x .. "," .. mid.y .. "," .. mid.z)
|
||||
|
||||
local dsq = Vec3.distanceSq(a, b)
|
||||
Debug.Log("Vec3.distanceSq: " .. dsq)
|
||||
end
|
||||
|
||||
14
Assets/lua/heal_trigger.lua
Normal file
14
Assets/lua/heal_trigger.lua
Normal file
@@ -0,0 +1,14 @@
|
||||
-- heal_trigger.lua - Heals player when entering the zone
|
||||
-- Attach to a PSXTriggerBox near a health pickup
|
||||
|
||||
function onTriggerEnter()
|
||||
Debug.Log("Entered heal zone")
|
||||
setStatus("Healing!")
|
||||
applyHeal(50)
|
||||
Audio.Play("heal", 100, 64)
|
||||
end
|
||||
|
||||
function onTriggerExit()
|
||||
Debug.Log("Left heal zone")
|
||||
setStatus("Left the healing area")
|
||||
end
|
||||
7
Assets/lua/heal_trigger.lua.meta
Normal file
7
Assets/lua/heal_trigger.lua.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,43 +1,52 @@
|
||||
-- ============================================================================
|
||||
-- movable.lua - Object that can be pushed with button presses
|
||||
-- ============================================================================
|
||||
-- Tests: onButtonPress, onButtonRelease, Entity.SetPosition, Vec3 math,
|
||||
-- Camera.GetPosition, Input constants, self.position
|
||||
-- movable.lua - Object that teleports on button press (event-driven)
|
||||
-- Tests: onButtonPress, Entity.SetPosition, Vec3 math, Camera.GetPosition,
|
||||
-- Input constants, self.position, self.rotationY
|
||||
|
||||
local pushing = false
|
||||
local selected = false
|
||||
|
||||
function onCreate(self)
|
||||
pushing = false
|
||||
Debug.Log("Movable object created")
|
||||
selected = false
|
||||
Debug.Log("Movable object created at "
|
||||
.. self.position.x .. "," .. self.position.y .. "," .. self.position.z)
|
||||
end
|
||||
|
||||
function onInteract(self)
|
||||
selected = not selected
|
||||
if selected then
|
||||
setStatus("Object selected! D-pad to move, Triangle to deselect.")
|
||||
else
|
||||
setStatus("Object deselected.")
|
||||
end
|
||||
end
|
||||
|
||||
function onButtonPress(self, button)
|
||||
if button == Input.SQUARE then
|
||||
pushing = true
|
||||
if not selected then return end
|
||||
|
||||
local pos = self.position
|
||||
local step = 1
|
||||
|
||||
if button == Input.UP then
|
||||
Entity.SetPosition(self, Vec3.new(pos.x, pos.y, pos.z + step))
|
||||
elseif button == Input.DOWN then
|
||||
Entity.SetPosition(self, Vec3.new(pos.x, pos.y, pos.z - step))
|
||||
elseif button == Input.LEFT then
|
||||
Entity.SetPosition(self, Vec3.new(pos.x - step, pos.y, pos.z))
|
||||
elseif button == Input.RIGHT then
|
||||
Entity.SetPosition(self, Vec3.new(pos.x + step, pos.y, pos.z))
|
||||
elseif button == Input.L1 then
|
||||
Entity.SetPosition(self, Vec3.new(pos.x, pos.y + step, pos.z))
|
||||
elseif button == Input.R1 then
|
||||
Entity.SetPosition(self, Vec3.new(pos.x, pos.y - step, pos.z))
|
||||
elseif button == Input.TRIANGLE then
|
||||
selected = false
|
||||
setStatus("Object deselected.")
|
||||
elseif button == Input.SQUARE then
|
||||
local rot = Entity.GetRotationY(self)
|
||||
Entity.SetRotationY(self, rot + 1/2)
|
||||
end
|
||||
end
|
||||
|
||||
function onButtonRelease(self, button)
|
||||
if button == Input.SQUARE then
|
||||
pushing = false
|
||||
end
|
||||
end
|
||||
|
||||
function onUpdate(self, dt)
|
||||
if not pushing then return end
|
||||
|
||||
-- Move toward the camera position (player) slowly
|
||||
local myPos = self.position
|
||||
local camPos = Camera.GetPosition()
|
||||
local dir = Vec3.sub(camPos, myPos)
|
||||
local dist = Vec3.length(dir)
|
||||
|
||||
if dist > 1 then
|
||||
local norm = Vec3.normalize(dir)
|
||||
local step = Vec3.mul(norm, dt)
|
||||
local newPos = Vec3.add(myPos, step)
|
||||
-- Keep Y the same (don't float)
|
||||
newPos.y = myPos.y
|
||||
Entity.SetPosition(self, newPos)
|
||||
end
|
||||
local diff = Vec3.sub(self.position, camPos)
|
||||
local dist = Vec3.length(diff)
|
||||
Debug.Log("Movable moved. Dist to camera: " .. dist)
|
||||
end
|
||||
|
||||
8
Assets/lua/portal_trigger.lua
Normal file
8
Assets/lua/portal_trigger.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
-- portal_trigger.lua - Scene transition when player enters
|
||||
-- Attach to a PSXTriggerBox at a doorway/exit
|
||||
|
||||
function onTriggerEnter()
|
||||
setStatus("Loading next scene...")
|
||||
Persist.Set("came_from", Scene.GetIndex())
|
||||
Scene.Load(0) -- reload scene 0 as demo
|
||||
end
|
||||
7
Assets/lua/portal_trigger.lua.meta
Normal file
7
Assets/lua/portal_trigger.lua.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,8 +1,8 @@
|
||||
-- ============================================================================
|
||||
-- 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
|
||||
-- Tests: onSceneCreationStart, onSceneCreationEnd, Persist, UI, Audio,
|
||||
-- Controls, Cutscene, Scene, Timer, Debug, PSXMath, Vec3
|
||||
|
||||
-- HUD handles (resolved in onSceneCreationEnd)
|
||||
local hudCanvas = -1
|
||||
@@ -48,17 +48,17 @@ function onSceneCreationEnd()
|
||||
UI.SetCanvasVisible(dialogueCanvas, false)
|
||||
end
|
||||
|
||||
-- Play ambient looping cutscene (tests loop feature)
|
||||
-- Play ambient looping cutscene
|
||||
Cutscene.Play("ambient_spin", {loop = true})
|
||||
|
||||
-- Test PSXMath functions
|
||||
-- Test PSXMath functions (all integer args)
|
||||
local clamped = PSXMath.Clamp(150, 0, 100)
|
||||
local lerped = PSXMath.Lerp(0, 10, 1)
|
||||
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 tests: clamp=" .. clamped .. " lerp=" .. lerped
|
||||
Debug.Log("PSXMath: clamp=" .. clamped .. " lerp=" .. lerped
|
||||
.. " sign=" .. sign .. " abs=" .. abs .. " min=" .. mn .. " max=" .. mx)
|
||||
|
||||
-- Test Vec3 functions
|
||||
@@ -67,55 +67,17 @@ function onSceneCreationEnd()
|
||||
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
|
||||
Debug.Log("Vec3: 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
|
||||
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
|
||||
|
||||
-- ============================================================================
|
||||
@@ -163,7 +125,27 @@ function isInDialogue()
|
||||
end
|
||||
|
||||
-- ============================================================================
|
||||
-- Helpers
|
||||
-- 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()
|
||||
@@ -176,6 +158,13 @@ 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
|
||||
|
||||
@@ -191,3 +180,21 @@ function addScore(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
|
||||
|
||||
@@ -1,25 +1,24 @@
|
||||
-- ============================================================================
|
||||
-- spinner.lua - Continuously rotating object (onUpdate demo)
|
||||
-- ============================================================================
|
||||
-- Tests: onUpdate, self.rotationY, Entity.GetRotationY, Entity.SetRotationY,
|
||||
-- Timer.GetFrameCount, self.position, Entity.GetPosition
|
||||
|
||||
local speed = 1 -- pi-units per frame
|
||||
-- spinner.lua - Object that spins using a looping cutscene
|
||||
-- Tests: onCreate, Cutscene.Play with loop, Cutscene.Stop, Cutscene.IsPlaying,
|
||||
-- onInteract, onCollideWithPlayer, Entity.GetRotationY, Timer
|
||||
|
||||
function onCreate(self)
|
||||
Debug.Log("Spinner created, initial rotY: " .. self.rotationY)
|
||||
Cutscene.Play("spin_loop", {loop = true})
|
||||
end
|
||||
|
||||
function onUpdate(self, dt)
|
||||
-- Rotate the object by speed * dt each frame
|
||||
local current = Entity.GetRotationY(self)
|
||||
Entity.SetRotationY(self, current + speed * dt)
|
||||
|
||||
-- Every 300 frames, log position and rotation (test Timer)
|
||||
if Timer.GetFrameCount() % 300 == 0 then
|
||||
local pos = Entity.GetPosition(self)
|
||||
Debug.Log("Spinner at frame " .. Timer.GetFrameCount()
|
||||
.. " pos=" .. pos.x .. "," .. pos.y .. "," .. pos.z
|
||||
.. " rotY=" .. Entity.GetRotationY(self))
|
||||
function onInteract(self)
|
||||
if Cutscene.IsPlaying() then
|
||||
Cutscene.Stop()
|
||||
setStatus("Spinner stopped!")
|
||||
Debug.Log("Spinner stopped at rotY=" .. Entity.GetRotationY(self)
|
||||
.. " frame=" .. Timer.GetFrameCount())
|
||||
else
|
||||
Cutscene.Play("spin_loop", {loop = true})
|
||||
setStatus("Spinner started!")
|
||||
end
|
||||
end
|
||||
|
||||
function onCollideWithPlayer(self)
|
||||
Debug.Log("Player touched spinner at frame " .. Timer.GetFrameCount())
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user