AutoFindCollectionCommand.lua 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. local AutoFindCollectionCommand = class('AutoFindCollectionCommand', framework.SimpleCommand)
  2. local function squLen( x, y )
  3. return x * x + y * y
  4. end
  5. function AutoFindCollectionCommand:ctor()
  6. end
  7. function AutoFindCollectionCommand:execute(notification)
  8. local player = global.gamePlayerController:GetMainPlayer()
  9. if not player then
  10. return nil
  11. end
  12. local facade = global.Facade
  13. local inputProxy = facade:retrieveProxy( global.ProxyTable.PlayerInputProxy )
  14. local autoProxy = facade:retrieveProxy( global.ProxyTable.Auto )
  15. local collectionID = inputProxy:GetTargetID()
  16. local target = nil
  17. local pMapX = player:GetMapX()
  18. local pMapY = player:GetMapY()
  19. -- check current target's race
  20. if collectionID then
  21. target = global.actorManager:GetActor( collectionID )
  22. if not target or not target:IsCollection() then
  23. collectionID = nil
  24. target = nil
  25. end
  26. end
  27. -- collecting current select
  28. if not target then
  29. local cost = global.MMO.MAX_CONST
  30. local monsterVec, nMonster = global.monsterManager:FindMonsterInCurrViewFieldByRace( global.MMO.ACTOR_RACE_COLLECTION )
  31. for i = 1, nMonster do
  32. local monster = monsterVec[i]
  33. if not monster:IsDie() then
  34. local len = squLen( monster:GetMapX() - pMapX, monster:GetMapY() - pMapY )
  35. if len < cost then
  36. target = monster
  37. cost = len
  38. end
  39. end
  40. end
  41. end
  42. -- oh, can't find collection
  43. -- wait
  44. if not target then
  45. -- clear auto state
  46. facade:sendNotification(global.NoticeTable.ClearAllInputState)
  47. facade:sendNotification(global.NoticeTable.ClearAllAutoState)
  48. facade:sendNotification(global.NoticeTable.InputIdle)
  49. autoProxy:ClearAllState()
  50. return
  51. end
  52. -- change target
  53. inputProxy:SetTargetID( target:GetID(), global.MMO.SELETE_TARGET_TYPE_FIND )
  54. -- check range
  55. local targetX = target:GetMapX()
  56. local targetY = target:GetMapY()
  57. local len = squLen( targetX - pMapX, targetY - pMapY )
  58. local range = (SL:GetMetaValue("GAME_DATA","AutoMoveRange_Collection") or 0) + 1
  59. local rangeSq = range * range
  60. if len < rangeSq then
  61. -- input idle
  62. facade:sendNotification(global.NoticeTable.ClearAllInputState)
  63. facade:sendNotification(global.NoticeTable.ClearAllAutoState)
  64. facade:sendNotification(global.NoticeTable.InputIdle)
  65. autoProxy:ClearAllState()
  66. inputProxy:RequestCollect()
  67. else
  68. local moveData =
  69. {
  70. x = targetX,
  71. y = targetY,
  72. targetType = global.MMO.ACTOR_COLLECTION,
  73. autoMoveType = global.MMO.AUTO_MOVE_TYPE_TARGET
  74. }
  75. facade:sendNotification( global.NoticeTable.AutoMoveBegin, moveData )
  76. end
  77. end
  78. return AutoFindCollectionCommand