Advertisement
xerpi

WeaponLib 4.1

Aug 3rd, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.38 KB | None | 0 0
  1. ------------------------- WeaponLib v4.1 - (c) 2011 xerpi -------------------------
  2. dofile("timer.lua")
  3.  
  4. weapon = {};
  5. function weapon.new(image,time)
  6.     local obj = {img = image, time = time,bullets={}, nbullets=0,time_act = timer.new(), running = false,current=1,dec=0};
  7.     function obj:blit()
  8.         for k,v in ipairs(self.bullets) do     
  9.             self.img:rotate(v.ang);
  10.             self.img:blit(v.x,v.y);
  11.         end
  12.     end
  13.     function obj:start()
  14.         self.running = true;
  15.         self.time_act:start();
  16.     end
  17.     function obj:stop()
  18.         self.running = false;
  19.         self.time_act:stop();
  20.         self.time_act:reset();
  21.     end
  22.     function obj:shoot(x,y,vel,ang)
  23.         if self.running then
  24.             if self.time_act:time() > self.time then
  25.                 table.insert(self.bullets,{vel=vel,x=x,y=y,ang=ang,inc_x = math.cos(math.rad(ang)),inc_y = math.sin(math.rad(ang))});  
  26.                 self.time_act:reset();
  27.             end
  28.         else
  29.             self:start();
  30.         end
  31.     end
  32.     function obj:move()
  33.         for k,v in ipairs(self.bullets) do
  34.             v.x = v.x + v.inc_x * v.vel;
  35.             v.y = v.y + v.inc_y * v.vel;
  36.             if v.x > 480 or v.x < 0 or v.y > 272 or v.y < 0 then table.remove(self.bullets,k); end
  37.         end
  38.     end
  39.     function obj:collision(x,y,w,h,deadcollision)
  40.         for k,v in ipairs(self.bullets) do
  41.             if v.x >= x and v.x <= x+w and
  42.                v.y >= y and v.y <= y+h then
  43.                     if deadcollision then
  44.                         table.remove(obj.bullets,k);
  45.                     end
  46.                     return true;               
  47.             end
  48.         end
  49.     end
  50.     return obj;
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement