components/locomotor.lua
40414243444546474849505152535455
local pt = nil
if self.inst and self.inst.components.inventoryitem and self.inst.components.inventoryitem.owner then
pt = Point(self.inst.components.inventoryitem.owner.Transform:GetWorldPosition())
elseif self.inst then
pt = Point(self.inst.Transform:GetWorldPosition())
elseif self.world_offset then
pt = Point(self.world_offset.x,self.world_offset.y,self.world_offset.z)
else
pt = Vector3(0,0,0)
end
return pt
end
4041424344454647484950515253
local pt = nil
if self.inst and self.inst.components.inventoryitem and self.inst.components.inventoryitem.owner then
return self.inst.components.inventoryitem.owner.Transform:GetWorldPosition()
elseif self.inst then
return self.inst.Transform:GetWorldPosition()
elseif self.world_offset then
return self.world_offset.x,self.world_offset.y,self.world_offset.z
else
return 0, 0, 0
end
end
646566676869707172
self.walkspeed = TUNING.WILSON_WALK_SPEED -- 4
self.runspeed = TUNING.WILSON_RUN_SPEED -- 6
self.bonusspeed = 0
self.update_speed_multiplier_task = nil
self.throttle = 1
self.slowmultiplier = 0.6
self.fastmultiplier = 1.3
6263646566676869
self.walkspeed = TUNING.WILSON_WALK_SPEED -- 4
self.runspeed = TUNING.WILSON_RUN_SPEED -- 6
self.bonusspeed = 0
self.throttle = 1
self.creep_check_timeout = 0
self.slowmultiplier = 0.6
self.fastmultiplier = 1.3
81828384858687
function LocoMotor:StopMoving()
self.isrunning = false
self.inst.Physics:Stop()
self:StopCheckingForCreep()
end
function LocoMotor:SetSlowMultiplier(m)
787980818283
function LocoMotor:StopMoving()
self.isrunning = false
self.inst.Physics:Stop()
end
function LocoMotor:SetSlowMultiplier(m)
9596979899100101
function LocoMotor:EnableGroundSpeedMultiplier(enable)
self.enablegroundspeedmultiplier = enable
if not enable then
self:StopCheckingForCreep()
self.groundspeedmultiplier = 1
end
end
919293949596
function LocoMotor:EnableGroundSpeedMultiplier(enable)
self.enablegroundspeedmultiplier = enable
if not enable then
self.groundspeedmultiplier = 1
end
end
125126127128129130131132133134135136137138139
end
function LocoMotor:UpdateGroundSpeedMultiplier()
if not self.inst:IsValid() or not self.enablegroundspeedmultiplier then
if self.update_speed_multiplier_task then
--print("LocoMotor:UpdateSpeedMultiplier() cleaning up task")
self.update_speed_multiplier_task:Cancel()
self.update_speed_multiplier_task = nil
end
return
end
self.groundspeedmultiplier = 1
local ground = GetWorld()
120121122123124125
end
function LocoMotor:UpdateGroundSpeedMultiplier()
self.groundspeedmultiplier = 1
local ground = GetWorld()
166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
end
function LocoMotor:StartCheckingForCreep()
--print(self.inst, "StartCheckingForCreep()", debugstack())
if self.enablegroundspeedmultiplier and not self.update_speed_multiplier_task then
self.update_speed_multiplier_task = self.inst:DoPeriodicTask(0.1, function() self:UpdateGroundSpeedMultiplier() end, 0)
end
end
function LocoMotor:StopCheckingForCreep()
--print(self.inst, "StopCheckingForCreep()")
if self.update_speed_multiplier_task then
self.update_speed_multiplier_task:Cancel()
self.update_speed_multiplier_task = nil
end
end
function LocoMotor:WalkForward()
self.isrunning = false
self.inst.Physics:SetMotorVel(self:GetWalkSpeed(),0,0)
self:StartCheckingForCreep()
end
function LocoMotor:RunForward()
self.isrunning = true
self.inst.Physics:SetMotorVel(self:GetRunSpeed(),0,0)
self:StartCheckingForCreep()
end
function LocoMotor:Clear()
152153154155156157158159160161162163164165166167168
end
function LocoMotor:WalkForward()
self.isrunning = false
self.inst.Physics:SetMotorVel(self:GetWalkSpeed(),0,0)
self.inst:StartUpdatingComponent(self)
end
function LocoMotor:RunForward()
self.isrunning = true
self.inst.Physics:SetMotorVel(self:GetRunSpeed(),0,0)
self.inst:StartUpdatingComponent(self)
end
function LocoMotor:Clear()
286287288289290291292
self.wantstorun = run
--self.arrive_step_dist = ARRIVE_STEP
self.inst:StartUpdatingComponent(self)
self:StartCheckingForCreep()
end
function LocoMotor:GoToPoint(pt, bufferedaction, run)
258259260261262263
self.wantstorun = run
--self.arrive_step_dist = ARRIVE_STEP
self.inst:StartUpdatingComponent(self)
end
function LocoMotor:GoToPoint(pt, bufferedaction, run)
313314315316317318319320
self.wantstomoveforward = true
self:SetBufferedAction(bufferedaction)
self.inst:StartUpdatingComponent(self)
self:StartCheckingForCreep()
end
284285286287288289
self.wantstomoveforward = true
self:SetBufferedAction(bufferedaction)
self.inst:StartUpdatingComponent(self)
end
341342343344345346347348
self.inst:PushEvent("locomote")
self.inst:StopUpdatingComponent(self)
self:StopCheckingForCreep()
end
function LocoMotor:WalkInDirection(direction, should_run)
310311312313314315
self.inst:PushEvent("locomote")
self.inst:StopUpdatingComponent(self)
end
function LocoMotor:WalkInDirection(direction, should_run)
361362363364365366367368
self:WalkForward()
end
self.inst:PushEvent("locomote")
self.inst:StopUpdatingComponent(self)
self:StartCheckingForCreep()
end
328329330331332333334
self:WalkForward()
end
self.inst:PushEvent("locomote")
self.inst:StartUpdatingComponent(self)
end
388389390391392393394395396
self:RunForward()
end
self.inst:PushEvent("locomote")
self.inst:StopUpdatingComponent(self)
self:StartCheckingForCreep()
end
function LocoMotor:GetDebugString()
354355356357358359360
self:RunForward()
end
self.inst:PushEvent("locomote")
self.inst:StartUpdatingComponent(self)
end
function LocoMotor:GetDebugString()
436437438439440441
return
end
--Print(VERBOSITY.DEBUG, "OnUpdate", self.inst.prefab)
if self.dest then
--Print(VERBOSITY.DEBUG, " w dest")
400401402403404405406407408409410411412413414
return
end
if self.enablegroundspeedmultiplier then
self.creep_check_timeout = self.creep_check_timeout - dt
if self.creep_check_timeout < 0 then
self:UpdateGroundSpeedMultiplier()
self.creep_check_timeout = .5
end
end
--Print(VERBOSITY.DEBUG, "OnUpdate", self.inst.prefab)
if self.dest then
--Print(VERBOSITY.DEBUG, " w dest")
449450451452453454455456457458459460461
return
end
local destpos = self.dest:GetPoint()
local mypos = Point(self.inst.Transform:GetWorldPosition())
local dsq = distsq(destpos, mypos)
if dsq <= self.arrive_dist*self.arrive_dist then
--Print(VERBOSITY.DEBUG, "REACH DEST")
self.inst:PushEvent("onreachdestination", {target=self.dest.inst, pos=destpos})
if self.atdestfn then
self.atdestfn(self.inst)
end
422423424425426427428429430431432433434435
return
end
local destpos_x, destpos_y, destpos_z = self.dest:GetPoint()
local mypos_x, mypos_y, mypos_z= self.inst.Transform:GetWorldPosition()
local dsq = distsq(destpos_x, destpos_z, mypos_x, mypos_z)
local run_dist = self:GetRunSpeed()*dt*.5
if dsq <= math.max(run_dist*run_dist, self.arrive_dist*self.arrive_dist) then
--Print(VERBOSITY.DEBUG, "REACH DEST")
self.inst:PushEvent("onreachdestination", {target=self.dest.inst, pos=Point(destpos_x, destpos_y, destpos_z)})
if self.atdestfn then
self.atdestfn(self.inst)
end
463464465466467468469
if self.bufferedaction and self.bufferedaction ~= self.inst.bufferedaction then
if self.bufferedaction.target and self.bufferedaction.target.Transform then
self.inst:FacePoint(Vector3(self.bufferedaction.target.Transform:GetWorldPosition()))
end
self.inst:PushBufferedAction(self.bufferedaction)
end
437438439440441442443
if self.bufferedaction and self.bufferedaction ~= self.inst.bufferedaction then
if self.bufferedaction.target and self.bufferedaction.target.Transform then
self.inst:FacePoint(self.bufferedaction.target.Transform:GetWorldPosition())
end
self.inst:PushBufferedAction(self.bufferedaction)
end
513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
if not self.inst.sg or self.inst.sg:HasStateTag("canrotate") then
--Print(VERBOSITY.DEBUG, "CANROTATE")
local facepos = destpos
if self.path and self.path.steps and self.path.currentstep < #self.path.steps then
--Print(VERBOSITY.DEBUG, "FOLLOW PATH")
local step = self.path.steps[self.path.currentstep]
local steppos = Point(step.x, step.y, step.z)
--Print(VERBOSITY.DEBUG, string.format("CURRENT STEP %d/%d - %s", self.path.currentstep, #self.path.steps, tostring(steppos)))
local step_distsq = distsq(mypos, steppos)
if step_distsq <= (self.arrive_step_dist)*(self.arrive_step_dist) then
self.path.currentstep = self.path.currentstep + 1
if self.path.currentstep < #self.path.steps then
step = self.path.steps[self.path.currentstep]
steppos = Point(step.x, step.y, step.z)
--Print(VERBOSITY.DEBUG, string.format("NEXT STEP %d/%d - %s", self.path.currentstep, #self.path.steps, tostring(steppos)))
else
--Print(VERBOSITY.DEBUG, string.format("LAST STEP %s", tostring(destpos)))
steppos = destpos
end
end
facepos = steppos
end
local x,y,z = self.inst.Physics:GetMotorVel()
if x < 0 then
--Print(VERBOSITY.DEBUG, "SET ROT", facepos)
local angle = self.inst:GetAngleToPoint(facepos)
self.inst.Transform:SetRotation(180 + angle)
else
--Print(VERBOSITY.DEBUG, "FACE PT", facepos)
self.inst:FacePoint(facepos)
end
end
487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
if not self.inst.sg or self.inst.sg:HasStateTag("canrotate") then
--Print(VERBOSITY.DEBUG, "CANROTATE")
local facepos_x, facepos_y, facepos_z = destpos_x, destpos_y, destpos_z
if self.path and self.path.steps and self.path.currentstep < #self.path.steps then
--Print(VERBOSITY.DEBUG, "FOLLOW PATH")
local step = self.path.steps[self.path.currentstep]
local steppos_x, steppos_y, steppos_z = step.x, step.y, step.z
--Print(VERBOSITY.DEBUG, string.format("CURRENT STEP %d/%d - %s", self.path.currentstep, #self.path.steps, tostring(steppos)))
local step_distsq = distsq(mypos_x, mypos_z, steppos_x, steppos_z)
if step_distsq <= (self.arrive_step_dist)*(self.arrive_step_dist) then
self.path.currentstep = self.path.currentstep + 1
if self.path.currentstep < #self.path.steps then
step = self.path.steps[self.path.currentstep]
steppos_x, steppos_y, steppos_z = step.x, step.y, step.z
--Print(VERBOSITY.DEBUG, string.format("NEXT STEP %d/%d - %s", self.path.currentstep, #self.path.steps, tostring(steppos)))
else
--Print(VERBOSITY.DEBUG, string.format("LAST STEP %s", tostring(destpos)))
steppos_x, steppos_y, steppos_z = destpos_x, destpos_y, destpos_z
end
end
facepos_x, facepos_y, facepos_z = steppos_x, steppos_y, steppos_z
end
local x,y,z = self.inst.Physics:GetMotorVel()
if x < 0 then
--Print(VERBOSITY.DEBUG, "SET ROT", facepos)
local angle = self.inst:GetAngleToPoint(facepos_x, facepos_y, facepos_z)
self.inst.Transform:SetRotation(180 + angle)
else
--Print(VERBOSITY.DEBUG, "FACE PT", facepos)
self.inst:FacePoint(facepos_x, facepos_y, facepos_z)
end
end
571572573574575576577578579580581
local speed_mult = self:GetSpeedMultiplier()
local desired_speed = self.isrunning and self.runspeed or self.walkspeed
if self.dest and self.dest:IsValid() then
local destpos = self.dest:GetPoint()
local mypos = Point(self.inst.Transform:GetWorldPosition())
local dsq = distsq(destpos, mypos)
if dsq <= .25 then
speed_mult = math.max(.5, math.sqrt(dsq))
end
end
545546547548549550551552553554555
local speed_mult = self:GetSpeedMultiplier()
local desired_speed = self.isrunning and self.runspeed or self.walkspeed
if self.dest and self.dest:IsValid() then
local destpos_x, destpos_y, destpos_z = self.dest:GetPoint()
local mypos_x, mypos_y, mypos_z = self.inst.Transform:GetWorldPosition()
local dsq = distsq(destpos_x, destpos_z, mypos_x, mypos_z)
if dsq <= .25 then
speed_mult = math.max(.33, math.sqrt(dsq))
end
end
593594595596597598599
end
local p0 = Vector3(self.inst.Transform:GetWorldPosition())
local p1 = self.dest:GetPoint()
local dist = math.sqrt(distsq(p0, p1))
--Print(VERBOSITY.DEBUG, string.format(" %s -> %s distance %2.2f", tostring(p0), tostring(p1), dist))
567568569570571572573
end
local p0 = Vector3(self.inst.Transform:GetWorldPosition())
local p1 = Vector3(self.dest:GetPoint())
local dist = math.sqrt(distsq(p0, p1))
--Print(VERBOSITY.DEBUG, string.format(" %s -> %s distance %2.2f", tostring(p0), tostring(p1), dist))