gameActorStateMoveBase.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. local gameActorState = require( "actor/gameActorState")
  2. local gameActorStateMoveBase = class('gameActorStateMoveBase', gameActorState)
  3. local mMin = math.min
  4. local mAbs = math.abs
  5. local mFloor = math.floor
  6. local mRound = math.round
  7. function gameActorStateMoveBase:ctor()
  8. gameActorStateMoveBase.super.ctor(self)
  9. end
  10. function gameActorStateMoveBase:GetStateID()
  11. return global.MMO.ACTION_WALK
  12. end
  13. function gameActorStateMoveBase:GetMoveTime( actor )
  14. return 0.5
  15. end
  16. function gameActorStateMoveBase:Tick(dt, actor)
  17. if IsSmoothMove(actor) then
  18. self:smooth_tick(dt, actor)
  19. else
  20. self:mir_tick(dt, actor)
  21. end
  22. end
  23. function gameActorStateMoveBase:mir_tick(dt, actor)
  24. actor.mCurrentActT = actor.mCurrentActT - dt
  25. if not actor.mIsArrived then
  26. if global.actorManager:IsMoveTime() then
  27. local percent = (actor.mMoveTime - actor.mCurrentActT) / actor.mMoveTime
  28. -- percent = mMin(1, percent)
  29. local disp = percent * actor.mDistanceToTarget
  30. local dispV = cc.pAdd( cc.p(actor.originPosition), cc.pMul(actor.mMoveOrient, disp) )
  31. -- PC影子闪
  32. if actor:GetDirection() % 2 == 0 then
  33. dispV.x = mFloor(dispV.x)
  34. dispV.y = mFloor(dispV.y)
  35. -- dispV.x = mRound(dispV.x)
  36. -- dispV.y = mRound(dispV.y)
  37. dispV.x = (dispV.x%2 == 0) and dispV.x or dispV.x-1
  38. dispV.y = (dispV.y%2 == 0) and dispV.y or dispV.y-1
  39. else
  40. dispV.x = mFloor(dispV.x)
  41. dispV.y = mFloor(dispV.y)
  42. end
  43. actor:setPosition( dispV.x, dispV.y )
  44. end
  45. if actor.mCurrentActT <= 0.0 then
  46. -- actor:setPosition( actor.mTargePos.x, actor.mTargePos.y )
  47. actor:SetIsArrived(true)
  48. end
  49. end
  50. if actor.mActionHandler and actor.mIsArrived and actor.mIsConfirmed then
  51. actor.mActionHandler:handleActionCompleted( actor, self:GetStateID() )
  52. end
  53. -- confirmed timeout
  54. if actor.mCurrentActT <= -5.0 then
  55. actor:SetConfirmed(true)
  56. end
  57. end
  58. function gameActorStateMoveBase:smooth_tick(dt, actor)
  59. actor.mCurrentActT = actor.mCurrentActT - dt
  60. if not actor.mIsArrived then
  61. -- Timing...
  62. if actor.mCurrentActT <= 0.0 then
  63. actor.mIsArrived = true
  64. end
  65. -- displacement
  66. local percent = (actor.mMoveTime - actor.mCurrentActT) / actor.mMoveTime
  67. -- percent = mMin(1, percent)
  68. local disp = percent * actor.mDistanceToTarget
  69. local dispV = cc.pAdd( cc.p(actor.originPosition), cc.pMul(actor.mMoveOrient, disp) )
  70. if global.isWinPlayMode and actor:IsPlayer() and actor:IsMainPlayer() then
  71. dispV.x = mRound(dispV.x)
  72. dispV.y = mRound(dispV.y)
  73. -- if actor:GetDirection() % 2 == 0 then
  74. -- dispV.x = (dispV.x%2 == 0) and dispV.x or dispV.x+1
  75. -- dispV.y = (dispV.y%2 == 0) and dispV.y or dispV.y+1
  76. -- end
  77. end
  78. actor:setPosition( dispV.x, dispV.y )
  79. end
  80. if actor.mActionHandler and actor.mIsArrived and actor.mIsConfirmed then
  81. actor.mActionHandler:handleActionCompleted( actor, self:GetStateID() )
  82. end
  83. -- confirmed timeout
  84. if actor.mCurrentActT <= -5.0 then
  85. actor:SetConfirmed(true)
  86. end
  87. end
  88. function gameActorStateMoveBase:OnEnter(actor)
  89. if actor.mMoveTo then
  90. self:moveToNextGrid( actor )
  91. actor.mIsArrived = false
  92. actor:SetConfirmed(false)
  93. actor.mCurrentActT = self:GetMoveTime( actor )
  94. actor.mMoveTime = self:GetMoveTime( actor )
  95. actor.originPosition = actor:getPosition()
  96. else
  97. actor.mIsArrived = true
  98. actor.mIsConfirmed = true
  99. actor:SetAction( global.MMO.ACTION_IDLE )
  100. end
  101. -- 试玩模式,直接确认
  102. if global.IsVisitor then
  103. actor:SetConfirmed(true)
  104. end
  105. end
  106. function gameActorStateMoveBase:OnExit(actor)
  107. actor.mCurrentActT = 0
  108. end
  109. function gameActorStateMoveBase:ChangeState(newState, actor, isTerminate)
  110. actor.mCurrentState = newState
  111. return true
  112. end
  113. function gameActorStateMoveBase:moveToNextGrid(actor)
  114. local gridPoint = actor.mMoveTo
  115. local currPos = cc.p(actor:getPosition())
  116. local targPos = global.sceneManager:MapPos2WorldPos( gridPoint.x, gridPoint.y, true )
  117. actor.mTargePos = targPos
  118. actor.mMoveOrient = cc.pSub(targPos, currPos)
  119. actor.mDistanceToTarget = cc.pGetLength( actor.mMoveOrient )
  120. actor.mMoveOrient = cc.pNormalize(actor.mMoveOrient)
  121. -- calcuating the orientation for Animation
  122. local currMapPos = cc.p( global.sceneManager:WorldPos2MapPos( currPos ) )
  123. local lastOri = actor.mOrient
  124. actor.mOrient = CalcActorDirByPos( currMapPos, gridPoint, lastOri )
  125. -- store grid position
  126. global.actorManager:SetActorMapXY( actor, gridPoint.x, gridPoint.y )
  127. -- gird pos changed
  128. if actor.mActionHandler then
  129. actor.mActionHandler:handleActionBegin( actor, self:GetStateID() )
  130. end
  131. if lastOri ~= actor.mOrient then
  132. actor:dirtyAnimFlag()
  133. local actorID = actor:GetID()
  134. global.BuffManager:UpdateBuffSfxDir(actorID)
  135. global.ActorEffectManager:UpdateEffectDir(actor, actorID)
  136. end
  137. end
  138. return gameActorStateMoveBase