AutoFindPlayerCommand.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. local AutoFindPlayerCommand = class('AutoFindPlayerCommand', framework.SimpleCommand)
  2. local proxyUtils = requireProxy("proxyUtils")
  3. local function squLen(x, y)
  4. return x * x + y * y
  5. end
  6. function AutoFindPlayerCommand:ctor()
  7. end
  8. function AutoFindPlayerCommand:execute(notification)
  9. local facade = global.Facade
  10. local inputProxy = facade:retrieveProxy(global.ProxyTable.PlayerInputProxy)
  11. local mainPlayer = global.gamePlayerController:GetMainPlayer()
  12. if not mainPlayer then
  13. return nil
  14. end
  15. -- 暂时用来 自动战斗锁定人形怪
  16. local target = nil
  17. local cost = global.MMO.MAX_CONST
  18. local pMapX = mainPlayer:GetMapX()
  19. local pMapY = mainPlayer:GetMapY()
  20. local playerVec, nPlayer = global.playerManager:GetPlayersInCurrViewField()
  21. for i = 1, nPlayer do
  22. local player = playerVec[i]
  23. if player and player:IsHumanoid() and not (player:GetMapX() == pMapX and player:GetMapY() == pMapY) then
  24. if true == proxyUtils.checkAutoTargetEnableByID(player:GetID()) then
  25. local len = squLen(player:GetMapX() - pMapX, player:GetMapY() - pMapY)
  26. if len < cost then
  27. target = player
  28. cost = len
  29. end
  30. end
  31. end
  32. end
  33. if target then
  34. inputProxy:SetTargetID(target:GetID(), global.MMO.SELETE_TARGET_TYPE_FIND)
  35. else
  36. -- oh, can't find monster, find special player
  37. facade:sendNotification(global.NoticeTable.AutoFindMonster)
  38. end
  39. end
  40. return AutoFindPlayerCommand