44 lines
1.2 KiB
Lua
44 lines
1.2 KiB
Lua
-- ============================================================================
|
|
-- movable.lua - Object that can be pushed with button presses
|
|
-- ============================================================================
|
|
-- Tests: onButtonPress, onButtonRelease, Entity.SetPosition, Vec3 math,
|
|
-- Camera.GetPosition, Input constants, self.position
|
|
|
|
local pushing = false
|
|
|
|
function onCreate(self)
|
|
pushing = false
|
|
Debug.Log("Movable object created")
|
|
end
|
|
|
|
function onButtonPress(self, button)
|
|
if button == Input.SQUARE then
|
|
pushing = true
|
|
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
|
|
end
|