Advertisement
xerpi

WeaponLib (c) xerpi v1.5

May 26th, 2011
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.15 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. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  23. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  24. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  25. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  26. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  27. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  28. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  29. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  30. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  31. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  32. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  33. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  34. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  35. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  36. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  37. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  38. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  39. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  40. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  41. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  42. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  43. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  44. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  45. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},
  46. {x=0,y=0,dir="",status="dead"}, {x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"},{x=0,y=0,dir="",status="dead"}
  47. }
  48.  
  49. return weaponobj
  50. end
  51.  
  52. function weapon.toblit(obj,num)
  53. if obj and obj.bullets[num].status == "alive" and obj.img[obj.bullets[num].dir] != "none" then
  54. local direction = obj.bullets[num].dir
  55.     obj.img[obj.bullets[num].dir]:blit(obj.bullets[num].x,obj.bullets[num].y)
  56.     if obj.bullets[num].dir == "up" then
  57.         obj.bullets[num].y = obj.bullets[num].y - obj.vel;
  58.     elseif obj.bullets[num].dir == "down" then
  59.         obj.bullets[num].y = obj.bullets[num].y + obj.vel;
  60.     elseif obj.bullets[num].dir == "right" then
  61.         obj.bullets[num].x = obj.bullets[num].x + obj.vel;
  62.     elseif obj.bullets[num].dir == "left" then
  63.         obj.bullets[num].x = obj.bullets[num].x - obj.vel;
  64.     end
  65.     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
  66.         obj.bullets[num].status = "dead"
  67.     end
  68. end
  69. end
  70.  
  71. function weapon.tocoll(obj,num,x,y,w,h,deadcollision)
  72. local cw, ch = obj.size.width[obj.bullets[num].dir], obj.size.height[obj.bullets[num].dir]
  73. if obj.bullets[num].status == "alive" then
  74.     if obj.bullets[num].x + cw >= x and  obj.bullets[num].x <= x+w and
  75.        obj.bullets[num].y + ch >= y and obj.bullets[num].y <= y+h then
  76.             if deadcollision then obj.bullets[num].status = "dead" end                         
  77.             return true
  78.     end
  79. end
  80. end
  81.  
  82. function weapon.blit(obj)
  83. weapon.toblit(obj,1) weapon.toblit(obj,2) weapon.toblit(obj,3) weapon.toblit(obj,4) weapon.toblit(obj,5)
  84. weapon.toblit(obj,6) weapon.toblit(obj,7) weapon.toblit(obj,8) weapon.toblit(obj,9) weapon.toblit(obj,10)
  85. weapon.toblit(obj,11) weapon.toblit(obj,12) weapon.toblit(obj,13) weapon.toblit(obj,14) weapon.toblit(obj,15)
  86. weapon.toblit(obj,16) weapon.toblit(obj,17) weapon.toblit(obj,18) weapon.toblit(obj,19) weapon.toblit(obj,20)
  87. weapon.toblit(obj,21) weapon.toblit(obj,22) weapon.toblit(obj,23) weapon.toblit(obj,24) weapon.toblit(obj,25)
  88. weapon.toblit(obj,26) weapon.toblit(obj,27) weapon.toblit(obj,28) weapon.toblit(obj,29) weapon.toblit(obj,30)
  89. weapon.toblit(obj,31) weapon.toblit(obj,32) weapon.toblit(obj,33) weapon.toblit(obj,34) weapon.toblit(obj,35)
  90. weapon.toblit(obj,36) weapon.toblit(obj,37) weapon.toblit(obj,38) weapon.toblit(obj,39) weapon.toblit(obj,40)
  91. weapon.toblit(obj,41) weapon.toblit(obj,42) weapon.toblit(obj,43) weapon.toblit(obj,44) weapon.toblit(obj,45)
  92. weapon.toblit(obj,46) weapon.toblit(obj,47) weapon.toblit(obj,48) weapon.toblit(obj,49) weapon.toblit(obj,50)
  93. weapon.toblit(obj,51) weapon.toblit(obj,52) weapon.toblit(obj,53) weapon.toblit(obj,54) weapon.toblit(obj,55)
  94. weapon.toblit(obj,56) weapon.toblit(obj,57) weapon.toblit(obj,58) weapon.toblit(obj,59) weapon.toblit(obj,60)
  95. weapon.toblit(obj,61) weapon.toblit(obj,62) weapon.toblit(obj,63) weapon.toblit(obj,64) weapon.toblit(obj,65)
  96. weapon.toblit(obj,66) weapon.toblit(obj,67) weapon.toblit(obj,68) weapon.toblit(obj,69) weapon.toblit(obj,70)
  97. weapon.toblit(obj,71) weapon.toblit(obj,72) weapon.toblit(obj,73) weapon.toblit(obj,74) weapon.toblit(obj,75)
  98. weapon.toblit(obj,76) weapon.toblit(obj,77) weapon.toblit(obj,78) weapon.toblit(obj,79) weapon.toblit(obj,80)
  99. weapon.toblit(obj,81) weapon.toblit(obj,82) weapon.toblit(obj,83) weapon.toblit(obj,84) weapon.toblit(obj,85)
  100. weapon.toblit(obj,86) weapon.toblit(obj,87) weapon.toblit(obj,88) weapon.toblit(obj,89) weapon.toblit(obj,90)
  101. weapon.toblit(obj,91) weapon.toblit(obj,92) weapon.toblit(obj,93) weapon.toblit(obj,94) weapon.toblit(obj,95)
  102. weapon.toblit(obj,96) weapon.toblit(obj,97) weapon.toblit(obj,98) weapon.toblit(obj,99) weapon.toblit(obj,100)
  103. end
  104.  
  105. function weapon.shoot(obj,x,y,dir,funct,sonido)
  106.     if obj.status == "stop" then obj.status = "run" obj.tim_act:start() end
  107.     if obj.status == "run" then
  108.         if obj.tim_act:time() >= obj.tim then
  109.             if sonido then sonido:play() end
  110.             obj.bullets[obj.current]={x=x,y=y,dir=dir,status="alive"}
  111.             obj.current=obj.current+1
  112.             if obj.current >obj.maxshoots then obj.current = 1 end
  113.             obj.tim_act:reset()
  114.             if funct then funct() end
  115.         end
  116.     end
  117. end
  118.  
  119. function weapon.collision(obj,x,y,w,h,deadcollision)
  120. if weapon.tocoll(obj,1,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,2,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,3,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,4,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,5,x,y,w,h,deadcollision) then return true end
  121. if weapon.tocoll(obj,6,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,7,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,8,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,9,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,10,x,y,w,h,deadcollision) then return true end
  122. if weapon.tocoll(obj,11,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,12,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,13,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,14,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,15,x,y,w,h,deadcollision) then return true end
  123. if weapon.tocoll(obj,16,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,17,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,18,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,19,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,20,x,y,w,h,deadcollision) then return true end
  124. if weapon.tocoll(obj,21,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,22,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,23,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,24,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,25,x,y,w,h,deadcollision) then return true end
  125. if weapon.tocoll(obj,26,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,27,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,28,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,29,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,30,x,y,w,h,deadcollision) then return true end
  126. if weapon.tocoll(obj,31,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,32,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,33,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,34,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,35,x,y,w,h,deadcollision) then return true end
  127. if weapon.tocoll(obj,36,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,37,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,38,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,39,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,40,x,y,w,h,deadcollision) then return true end
  128. if weapon.tocoll(obj,41,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,42,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,43,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,44,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,45,x,y,w,h,deadcollision) then return true end
  129. if weapon.tocoll(obj,46,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,47,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,48,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,49,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,50,x,y,w,h,deadcollision) then return true end
  130. if weapon.tocoll(obj,51,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,52,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,53,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,54,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,55,x,y,w,h,deadcollision) then return true end
  131. if weapon.tocoll(obj,56,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,57,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,58,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,59,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,60,x,y,w,h,deadcollision) then return true end
  132. if weapon.tocoll(obj,61,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,62,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,63,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,64,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,65,x,y,w,h,deadcollision) then return true end
  133. if weapon.tocoll(obj,66,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,67,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,68,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,69,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,70,x,y,w,h,deadcollision) then return true end
  134. if weapon.tocoll(obj,71,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,72,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,73,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,74,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,75,x,y,w,h,deadcollision) then return true end
  135. if weapon.tocoll(obj,76,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,77,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,78,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,79,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,80,x,y,w,h,deadcollision) then return true end
  136. if weapon.tocoll(obj,81,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,82,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,83,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,84,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,85,x,y,w,h,deadcollision) then return true end
  137. if weapon.tocoll(obj,86,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,87,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,88,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,89,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,90,x,y,w,h,deadcollision) then return true end
  138. if weapon.tocoll(obj,91,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,92,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,93,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,94,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,95,x,y,w,h,deadcollision) then return true end
  139. if weapon.tocoll(obj,96,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,97,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,98,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,99,x,y,w,h,deadcollision) then return true end if weapon.tocoll(obj,100,x,y,w,h,deadcollision) then return true end
  140. end
  141.  
  142. function weapon.action(obj,action)
  143.     if action == "start" then obj.status = "run" obj.tim_act:start() end
  144.     if action == "stop" then obj.status = "stop" obj.tim_act:reset() obj.tim_act:stop() end
  145. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement