target.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. Target = class()
  2. function Target:init()
  3. end
  4. function Target:getClosestTarget(source)
  5. local closestTarget, distance
  6. local kind = getmetatable(source).__index
  7. local super = getmetatable(kind).__index
  8. if super == Enemy or super == Minion then
  9. kind = super
  10. end
  11. if (kind == Enemy) then
  12. closestTarget, distance = self:compareDistance(source,self:compareDistance(source,ctx.player,ctx.shrine),self:getClosestMinion(source))
  13. elseif (kind == Minion) then
  14. closestTarget, distance = self:compareDistance(source,self:compareDistance(source,ctx.player,ctx.shrine),self:getClosestEnemy(source))
  15. elseif (kind == Player) then
  16. closestTarget, distance = self:compareDistance(source,ctx.shrine,self:compareDistance(source,self:getClosestEnemy(source),self:getClosestMinion(source)))
  17. else
  18. closestTarget, distance = self:compareDistance(source,self:compareDistance(source,ctx.player,ctx.shrine),self:compareDistance(source,self:getClosestEnemy(source),self:getClosestMinion(source)))
  19. end
  20. return closestTarget, distance
  21. end
  22. function Target:getClosestNPC(source)
  23. local closestTarget, distance
  24. local kind = getmetatable(source).__index
  25. local super = getmetatable(kind).__index
  26. if super == Enemy or super == Minion then
  27. kind = super
  28. end
  29. if (kind == Enemy) then
  30. closestTarget, distance = self:compareDistance(source,ctx.shrine,self:getClosestMinion(source))
  31. elseif (kind == Minion) then
  32. closestTarget, distance = self:compareDistance(source,ctx.shrine,self:getClosestEnemy(source))
  33. else
  34. closestTarget, distance = self:compareDistance(source,ctx.shrine,self:compareDistance(source,self:getClosestEnemy(source),self:getClosestMinion(source)))
  35. end
  36. return closestTarget, distance
  37. end
  38. function Target:getClosestEnemy(source)
  39. if not next(ctx.enemies.enemies) then
  40. return nil
  41. end
  42. local closestEnemy
  43. local enemyDistance = math.huge
  44. table.each(ctx.enemies.enemies, function(e)
  45. if e ~= source and not e.dead then
  46. local distance = math.abs(source.x - e.x)
  47. if distance < enemyDistance then
  48. enemyDistance = distance
  49. closestEnemy = e
  50. end
  51. end
  52. end)
  53. return closestEnemy, enemyDistance
  54. end
  55. function Target:getClosestMinion(source)
  56. if not next(ctx.minions.minions) then
  57. return nil
  58. end
  59. local closestMinion
  60. local minionDistance = math.huge
  61. table.each(ctx.minions.minions, function(m)
  62. if source ~= m and not m.dead then
  63. local distance = math.abs(source.x - m.x)
  64. if distance < minionDistance then
  65. minionDistance = distance
  66. closestMinion = m
  67. end
  68. end
  69. end)
  70. return closestMinion, minionDistance
  71. end
  72. function Target:getMinionsInRange(source, range)
  73. local minions = {}
  74. table.each(ctx.minions.minions, function(m)
  75. local dif = math.abs(source.x - m.x)
  76. if dif <= range + m.width / 2 then
  77. table.insert(minions, m)
  78. end
  79. end)
  80. return minions
  81. end
  82. function Target:getEnemiesInRange(source, range)
  83. if not next(ctx.enemies.enemies) then
  84. return nil
  85. end
  86. local enemiesInRange = {}
  87. table.each(ctx.enemies.enemies, function(e)
  88. local distance = math.abs(source.x - e.x)
  89. if e ~= source and distance <= range + e.width / 2 then
  90. table.insert(enemiesInRange,e)
  91. end
  92. end)
  93. return enemiesInRange
  94. end
  95. function Target:getPlayer(source)
  96. local distance = math.abs(source.x - ctx.player.x)
  97. return ctx.player, distance
  98. end
  99. function Target:getShrine(source)
  100. local distance = math.abs(source.x - ctx.shrine.x)
  101. return ctx.shrine, distance
  102. end
  103. function Target:compareDistance(source, unit1, unit2)
  104. if unit1 == nil then
  105. return unit2
  106. end
  107. if unit2 == nil then
  108. return unit1
  109. end
  110. local unit1Distance = math.abs(source.x - unit1.x)
  111. local unit2Distance = math.abs(source.x - unit2.x)
  112. local closerUnit = unit1
  113. local distance = unit1Distance
  114. if unit1Distance > unit2Distance then
  115. closerUnit = unit2
  116. distance = unit2Distance
  117. end
  118. return closerUnit, distance
  119. end