Advertisement
xerpi

WeaponLib 4.0 Lua (c) xerpi 2011

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