Advertisement
xerpi

WeaponLib (c) xerpi v2.0

May 27th, 2011
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | None | 0 0
  1. ------------------------- weaponlib v2.0 - (c) 2011 xerpi -------------------------
  2.  
  3. weapon={}
  4. --{x=0,y=0,ang=alpha,poly}
  5.  
  6. function math.getpoly(x,y,w,h)
  7. --return {{ x - w/2, y - h/2 },{ x + w/2, y - h/2 },{ x + w/2, y + h/2 },{ x - w/2, y + h/2 }}
  8. return {x=x,y=y,w=w,h=h}
  9. end
  10. function math.outofscreen(x,y,w,h,ang)
  11. if x<0 or x>480 or y<0 or y>272 then return true end
  12. end
  13. function math.collision(x,y,w,h,x1,y1,w1,h1)
  14. if x + w >= x1 and x <= x1 +w1 and
  15. y + h >= y1 and y <= y1 + h1 then
  16. return true
  17. end
  18. end
  19.  
  20.  
  21. function weapon.create(img,time)
  22. return {img=img, time = time,w = img:width(),bullets={}, h = img:height(), time_act = timer.new(), status = "stop",current=1,dec=0} or {}
  23. end
  24.  
  25. function weapon.blit(obj)
  26.     local dec,i = 0,0
  27.     for b = 1, #obj.bullets do
  28.         i = b-dec
  29.         obj.bullets[i].x = obj.bullets[i].x + math.cos(math.rad(obj.bullets[i].ang))*obj.bullets[i].vel
  30.         obj.bullets[i].y = obj.bullets[i].y + math.sin(math.rad(obj.bullets[i].ang))*obj.bullets[i].vel
  31.         if math.outofscreen(obj.bullets[i].x,obj.bullets[i].y) then
  32.             table.remove(obj.bullets,i)
  33.             dec=dec+1
  34.             continue
  35.         end
  36.         obj.img:rotate(obj.bullets[i].ang)
  37.         --math.poly.rotate(0,0,0,obj.bullets[i].poly,obj.bullets[i].ang);
  38.         obj.img:blit(obj.bullets[i].x,obj.bullets[i].y)
  39.         obj.img:reset()
  40.     end
  41. end
  42.  
  43. function weapon.shoot(obj,x,y,vel,ang)
  44. if obj then
  45.     if obj.status == "stop" then obj.status = "run" obj.time_act:start() end
  46.     if obj.status == "run" then
  47.         if obj.time_act:time() >= obj.time then
  48.             table.insert(obj.bullets,{vel=vel,x=x,y=y,ang=ang,poly=math.getpoly(x,x,obj.w,obj.h)})     
  49.             obj.time_act:reset()
  50.         end
  51.     end
  52. end
  53. end
  54.  
  55. function weapon.collision(obj,x,y,w,h,deadcollision)
  56.     local dec,i = 0,0
  57.     for b = 1, #obj.bullets do
  58.         i = b-dec
  59.         --obj.bullets[i].x = obj.bullets[i].x + math.cos(math.rad(obj.bullets[i].ang))*obj.bullets[i].vel
  60.         --obj.bullets[i].y = obj.bullets[i].y + math.sin(math.rad(obj.bullets[i].ang))*obj.bullets[i].vel
  61.         --math.poly.rotate(0,0,0,obj.bullets[i].poly,obj.bullets[i].ang);
  62.         if math.collision(obj.bullets[i].x,obj.bullets[i].y,obj.bullets[i].poly.w,obj.bullets[i].poly.h,x,y,w,h) then
  63.             if deadcollision then
  64.                 table.remove(obj.bullets,i)
  65.                 dec=dec+1
  66.             end
  67.             return true
  68.         end
  69.     end
  70. end
  71.  
  72. function weapon.action(obj,action)
  73.     if action == "start" then obj.status = "run" obj.time_act:start() end
  74.     if action == "stop" then obj.status = "stop" obj.time_act:reset() obj.time_act:stop() end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement