Advertisement
xerpi

WeaponLib (c) xerpi

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