Advertisement
xerpi

Weaponlib optimized

May 27th, 2011
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.43 KB | None | 0 0
  1. ------------------------- weaponlib v1.5 - (c) 2011 xerpi -------------------------
  2.  
  3. weapon={}
  4.  
  5.  
  6. function weapon.create(tableimg,vel,tiempo)
  7. weaponobj = {img={},size={width={},height={}}, tim = tiempo, tim_act = timer.new(),vel = vel , status = "stop",current=1,maxshoots=100}
  8. weaponobj.img["up"] = tableimg["up"] or "none"
  9. weaponobj.img["down"] = tableimg["down"] or "none"
  10. weaponobj.img["right"] = tableimg["right"] or "none"
  11. weaponobj.img["left"] = tableimg["left"] or "none"
  12. weaponobj.size.width["up"] = (tableimg["up"] or image.create(0,0) ):width() or 0
  13. weaponobj.size.width["down"] = (tableimg["down"] or image.create(0,0) ):width() or 0
  14. weaponobj.size.width["right"] = (tableimg["right"] or image.create(0,0) ):width() or 0
  15. weaponobj.size.width["left"] = (tableimg["left"] or image.create(0,0) ):width() or 0
  16. weaponobj.size.height["up"] = (tableimg["up"] or image.create(0,0) ):height() or 0
  17. weaponobj.size.height["down"] = (tableimg["down"] or image.create(0,0) ):height() or 0
  18. weaponobj.size.height["right"] = (tableimg["right"] or image.create(0,0) ):height() or 0
  19. weaponobj.size.height["left"] = (tableimg["left"] or image.create(0,0) ):height() or 0
  20.  
  21. weaponobj.bullets={}
  22. for i = 1, 100 do
  23. table.insert(weaponobj.bullets,{x=0,y=0,dir="",status="dead"})
  24. end
  25.  
  26. return weaponobj
  27. end
  28.  
  29. function weapon.toblit(obj,num)
  30. if obj and obj.bullets[num].status == "alive" and obj.img[obj.bullets[num].dir] != "none" then
  31. local direction = obj.bullets[num].dir
  32.     obj.img[obj.bullets[num].dir]:blit(obj.bullets[num].x,obj.bullets[num].y)
  33.     if obj.bullets[num].dir == "up" then
  34.         obj.bullets[num].y = obj.bullets[num].y - obj.vel;
  35.     elseif obj.bullets[num].dir == "down" then
  36.         obj.bullets[num].y = obj.bullets[num].y + obj.vel;
  37.     elseif obj.bullets[num].dir == "right" then
  38.         obj.bullets[num].x = obj.bullets[num].x + obj.vel;
  39.     elseif obj.bullets[num].dir == "left" then
  40.         obj.bullets[num].x = obj.bullets[num].x - obj.vel;
  41.     end
  42.     if obj.bullets[num].y+obj.size.height[direction] < 0 or obj.bullets[num].y  >272 or obj.bullets[num].x+obj.size.width[direction] < 0 or obj.bullets[num].x >480 then
  43.         obj.bullets[num].status = "dead"
  44.     end
  45. end
  46. end
  47.  
  48. function weapon.tocoll(obj,num,x,y,w,h,deadcollision)
  49. local cw, ch = obj.size.width[obj.bullets[num].dir], obj.size.height[obj.bullets[num].dir]
  50. if obj.bullets[num].status == "alive" then
  51.     if obj.bullets[num].x + cw >= x and  obj.bullets[num].x <= x+w and
  52.        obj.bullets[num].y + ch >= y and obj.bullets[num].y <= y+h then
  53.             if deadcollision then obj.bullets[num].status = "dead" end                         
  54.             return true
  55.     end
  56. end
  57. end
  58.  
  59. function weapon.blit(obj)
  60. for i = 1, 100 do
  61. weapon.toblit(obj,i)
  62. end
  63. end
  64.  
  65. function weapon.shoot(obj,x,y,dir,funct,sonido)
  66.     if obj.status == "stop" then obj.status = "run" obj.tim_act:start() end
  67.     if obj.status == "run" then
  68.         if obj.tim_act:time() >= obj.tim then
  69.             if sonido then sonido:play() end
  70.             obj.bullets[obj.current]={x=x,y=y,dir=dir,status="alive"}
  71.             obj.current=obj.current+1
  72.             if obj.current >obj.maxshoots then obj.current = 1 end
  73.             obj.tim_act:reset()
  74.             if funct then funct() end
  75.         end
  76.     end
  77. end
  78.  
  79. function weapon.collision(obj,x,y,w,h,deadcollision)
  80. for i = 1, 100 do
  81. if weapon.tocoll(obj,i,x,y,w,h,deadcollision) then return true end
  82. end
  83. end
  84.  
  85. function weapon.action(obj,action)
  86.     if action == "start" then obj.status = "run" obj.tim_act:start() end
  87.     if action == "stop" then obj.status = "stop" obj.tim_act:reset() obj.tim_act:stop() end
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement