AutoFindMonsterCommand.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. local AutoFindMonsterCommand = class('AutoFindMonsterCommand', framework.SimpleCommand)
  2. local proxyUtils = requireProxy("proxyUtils")
  3. local function squLen(x, y)
  4. return x * x + y * y
  5. end
  6. function AutoFindMonsterCommand:ctor()
  7. end
  8. function AutoFindMonsterCommand:execute(notification)
  9. local player = global.gamePlayerController:GetMainPlayer()
  10. if not player then
  11. return nil
  12. end
  13. local getMonsterVec = function (monsters, ncount)
  14. local monsterVec = {}
  15. local num = 0
  16. for i = 1, ncount do
  17. local monster = monsters[i]
  18. if proxyUtils.checkAutoTargetEnableByID(monster:GetID()) then
  19. num = num + 1
  20. monsterVec[num] = monster
  21. end
  22. end
  23. return monsterVec, num
  24. end
  25. local facade = global.Facade
  26. local autoProxy = facade:retrieveProxy(global.ProxyTable.Auto)
  27. local inputProxy = facade:retrieveProxy(global.ProxyTable.PlayerInputProxy)
  28. local targetIndex = autoProxy:GetTargetIndex()
  29. local targetType = autoProxy:GetTargetType()
  30. local monsterVec = {}
  31. local monsterVecNum = 0
  32. if targetType ~= global.MMO.ACTOR_MONSTER or not targetIndex or global.MMO.AUTO_FIND_TARGET_NONE == targetIndex or 0 == targetIndex then -- not target index,find nearst monster
  33. local monsters, ncount = global.monsterManager:FindMonsterInCurrViewField(true, true)
  34. monsterVec, monsterVecNum = getMonsterVec(monsters, ncount)
  35. else
  36. local monsters = {}
  37. local ncount = 0
  38. if type(targetIndex) == "number" then
  39. monsters, ncount = global.monsterManager:FindMonsterInCurrViewFieldByTypeIndex(targetIndex, true, true)
  40. elseif type(targetIndex) == "table" then
  41. monsters, ncount = global.monsterManager:FindMonsterInCurrViewFieldByTypeIndexTable(targetIndex, true, true)
  42. end
  43. monsterVec, monsterVecNum = getMonsterVec(monsters, ncount)
  44. if monsterVecNum < 1 then
  45. local monsters, ncount = global.monsterManager:FindMonsterInCurrViewField()
  46. monsterVec, monsterVecNum = getMonsterVec(monsters, ncount)
  47. end
  48. end
  49. if monsterVecNum < 1 then
  50. return false
  51. end
  52. -- find nearest monster
  53. local target = self:FindNearestMonster(monsterVec, monsterVecNum)
  54. if target then
  55. inputProxy:SetTargetID(target:GetID(), global.MMO.SELETE_TARGET_TYPE_FIND)
  56. end
  57. end
  58. function AutoFindMonsterCommand:FindNearestMonster(monsterVec, monsterVecNum)
  59. -- 不自动选: 守卫/石化状态下祖玛卫士
  60. local target = nil
  61. local cost = global.MMO.MAX_CONST
  62. local mainPlayer = global.gamePlayerController:GetMainPlayer()
  63. local pMapX = mainPlayer:GetMapX()
  64. local pMapY = mainPlayer:GetMapY()
  65. local mX = 0
  66. local mY = 0
  67. for i = 1, monsterVecNum do
  68. local monster = monsterVec[i]
  69. if monster then
  70. local mX = monster:GetMapX()
  71. local mY = monster:GetMapY()
  72. if not (mX == pMapX and mY == pMapY) and proxyUtils.checkAutoTargetEnableByID(monster:GetID()) then
  73. local len = squLen(mX - pMapX, mY - pMapY)
  74. if len < cost then
  75. target = monster
  76. cost = len
  77. end
  78. end
  79. end
  80. end
  81. return target
  82. end
  83. return AutoFindMonsterCommand