Guest User

Untitled

a guest
May 26th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. -------------------------------------------------------------------------------
  2. -------------------------------------------------------------------------------
  3. --
  4. -- OrbitBot, a simple robot that finds the nearest TestItem and orbits it
  5. -- Note that this relies on line-of-sight navigation, so only good for open levels
  6. --
  7. -------------------------------------------------------------------------------
  8. -------------------------------------------------------------------------------
  9. -------------------------------------------------------------------------------
  10. -------------------------------------------------------------------------------
  11.  
  12.  
  13. -------------------------------------------------------------------------------
  14. -- Setup; all vars declared here are global unless declared with "local" keyword
  15.  
  16. function main()
  17. orbitAng = 0
  18. orbitRadius = 900
  19. isInOrbit = false
  20. targetItem = nil
  21. end
  22.  
  23.  
  24. -------------------------------------------------------------------------------
  25. -- This function is called once and should return the robot's name
  26.  
  27. function getName()
  28. return( "OrbitBot" )
  29. end
  30.  
  31.  
  32. -------------------------------------------------------------------------------
  33. -- This is called by the robot's idle routine each tick.
  34. -- This function must be present for the robot to work!
  35.  
  36. items = { }
  37.  
  38. function onTick(deltaTime)
  39.  
  40. -- if(targetItem == nil) then
  41. -- table.clear(items)
  42. -- targetItem = findClosest(findGlobalObjects(items, ObjType.SoccerBallItem))
  43. --end
  44.  
  45.  
  46.  
  47. local targetItems = findGlobalObjects(items, ObjType.SoccerBallItem);
  48. -- table.insert(targetItems,0,bot:getShip());
  49.  
  50. local count = 0;
  51. for key, targetItem in pairs(targetItems) do
  52. local cent = point.new(0,0);
  53. local pos = targetItem:getLoc();
  54. local delt = pos-cent;
  55. local deltdir = point.normalize(delt);
  56.  
  57. local ang = point.angleTo(cent, pos);
  58. local nang = ang+(math.pi/2);
  59. local ndir = point.new(math.cos(nang), math.sin(nang));
  60.  
  61. local force = -2000;
  62. local nvel = ndir * -1000 + deltdir*force;
  63. local cvel = targetItem:getVel();
  64. local p = 0.05;--TODO use a time delta for this later
  65. local lerped = (((nvel-cvel)*p)+cvel);
  66.  
  67. local dst = point.length(delt)
  68. if (dst < 470) then
  69. if (dst > 280) then
  70. targetItem:setLoc(deltdir*270);
  71. elseif (dst < 150) then
  72. targetItem:setLoc(deltdir*5000);
  73. end
  74. end
  75.  
  76.  
  77.  
  78. --targetItem:setLoc(point.new(100,100));
  79. targetItem:setVel(lerped);
  80.  
  81. end
  82.  
  83.  
  84.  
  85.  
  86.  
  87. -- bot:setThrustToPt( dest ) -- Travel towards calculated point
  88. --bot:setAngle( point.angleTo( botLoc, itemLoc ) ) -- Aim ship that way too
  89. end
Advertisement
Add Comment
Please, Sign In to add comment