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_NONE = "none"
|
||||||
local MOVEMENT_TWEENER = "tweener"
|
local MOVEMENT_TWEENER = "tweener"
|
||||||
local MOVEMENT_MOTION = "motion"
|
local MOVEMENT_MOTION = "motion"
|
||||||
|
local MOVEMENT_TARGET = "target"
|
||||||
|
|
||||||
local ZGRAVITY = 0.2
|
local ZGRAVITY = 0.2
|
||||||
|
|
||||||
|
@ -37,6 +38,7 @@ function Movable:initMovementSystem()
|
||||||
self.movementType = MOVEMENT_NONE
|
self.movementType = MOVEMENT_NONE
|
||||||
|
|
||||||
self:initJump()
|
self:initJump()
|
||||||
|
self:resetTarget()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Movable:resetMovementType()
|
function Movable:resetMovementType()
|
||||||
|
@ -59,10 +61,10 @@ end
|
||||||
function Movable:updateMovement(dt)
|
function Movable:updateMovement(dt)
|
||||||
if (self.movementType == MOVEMENT_TWEENER) then
|
if (self.movementType == MOVEMENT_TWEENER) then
|
||||||
self:updateTweenerSpeed(dt)
|
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)
|
self:updateMotion(dt)
|
||||||
end
|
end
|
||||||
self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2)
|
|
||||||
|
|
||||||
self:updateDirection()
|
self:updateDirection()
|
||||||
self:updateJump(dt)
|
self:updateJump(dt)
|
||||||
|
@ -122,6 +124,7 @@ function Movable:setMotionToPoint(speed, dx, dy)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Movable:updateMotion(dt)
|
function Movable:updateMotion(dt)
|
||||||
|
self:checkTarget()
|
||||||
self.xspeed, self.yspeed = utils.math.lengthdir(self.speed, self.angle)
|
self.xspeed, self.yspeed = utils.math.lengthdir(self.speed, self.angle)
|
||||||
self.x = self.x + (self.xspeed) * dt
|
self.x = self.x + (self.xspeed) * dt
|
||||||
self.y = self.y + (self.yspeed) * dt
|
self.y = self.y + (self.yspeed) * dt
|
||||||
|
@ -133,6 +136,53 @@ function Movable:endMotion()
|
||||||
self.yspeed = 0
|
self.yspeed = 0
|
||||||
end
|
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
|
-- Direction handling
|
||||||
|
|
||||||
function Movable:updateDirection()
|
function Movable:updateDirection()
|
||||||
|
|
Loading…
Reference in a new issue