feat: add target-base movement
This commit is contained in:
parent
0d33d8b9c8
commit
58e68f0191
1 changed files with 52 additions and 2 deletions
|
@ -4,6 +4,7 @@ local Movable = ParentObject:extend()
|
|||
local MOVEMENT_NONE = "none"
|
||||
local MOVEMENT_TWEENER = "tweener"
|
||||
local MOVEMENT_MOTION = "motion"
|
||||
local MOVEMENT_TARGET = "target"
|
||||
|
||||
local ZGRAVITY = 0.2
|
||||
|
||||
|
@ -37,6 +38,7 @@ function Movable:initMovementSystem()
|
|||
self.movementType = MOVEMENT_NONE
|
||||
|
||||
self:initJump()
|
||||
self:resetTarget()
|
||||
end
|
||||
|
||||
function Movable:resetMovementType()
|
||||
|
@ -59,10 +61,10 @@ end
|
|||
function Movable:updateMovement(dt)
|
||||
if (self.movementType == MOVEMENT_TWEENER) then
|
||||
self:updateTweenerSpeed(dt)
|
||||
elseif (self.movementType == MOVEMENT_MOTION) then
|
||||
self.speed = math.sqrt(self.xspeed^2 + self.yspeed^2)
|
||||
elseif (self.movementType == MOVEMENT_MOTION) or (self.movementType == MOVEMENT_TARGET) then
|
||||
self:updateMotion(dt)
|
||||
end
|
||||
self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2)
|
||||
|
||||
self:updateDirection()
|
||||
self:updateJump(dt)
|
||||
|
@ -122,6 +124,7 @@ function Movable:setMotionToPoint(speed, dx, dy)
|
|||
end
|
||||
|
||||
function Movable:updateMotion(dt)
|
||||
self:checkTarget()
|
||||
self.xspeed, self.yspeed = utils.math.lengthdir(self.speed, self.angle)
|
||||
self.x = self.x + (self.xspeed) * dt
|
||||
self.y = self.y + (self.yspeed) * dt
|
||||
|
@ -133,6 +136,53 @@ function Movable:endMotion()
|
|||
self.yspeed = 0
|
||||
end
|
||||
|
||||
-- Target handling
|
||||
|
||||
function Movable:setTarget(x, y, z)
|
||||
self.target = {}
|
||||
self.target.x = x
|
||||
self.target.y = y
|
||||
self.target.z = z
|
||||
end
|
||||
|
||||
function Movable:isTargetActive()
|
||||
return (self.target ~= nil)
|
||||
end
|
||||
|
||||
function Movable:resetTarget()
|
||||
self.target = nil
|
||||
end
|
||||
|
||||
function Movable:isNearTarget(distance)
|
||||
if (not self:isTargetActive()) then
|
||||
return false
|
||||
end
|
||||
|
||||
if (self.target.z == nil) then
|
||||
return (utils.math.pointDistance(self.x, self.y, self.target.x, self.target.z) <= distance)
|
||||
else
|
||||
return (utils.math.pointDistance3D(self.x, self.y, self.z, self.target.x, self.target.y, self.target.z) <= distance)
|
||||
end
|
||||
end
|
||||
|
||||
function Movable:checkTarget()
|
||||
if (self:isTargetActive()) then
|
||||
local isNearTarget = self:isNearTarget(self.speed)
|
||||
if (isNearTarget) then
|
||||
-- TODO: add a one-time signal to target handling
|
||||
self:resetTarget()
|
||||
if (self.movementType == MOVEMENT_TARGET) then
|
||||
self.x = self.target.x
|
||||
self.y = self.target.y
|
||||
if (self.target.z ~= nil) then
|
||||
self.z = self.target.z
|
||||
end
|
||||
self:stopMoving()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Direction handling
|
||||
|
||||
function Movable:updateDirection()
|
||||
|
|
Loading…
Reference in a new issue