Better testscene
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user