------------------------- WeaponLib v4.1 - (c) 2011 xerpi ------------------------- dofile("timer.lua") weapon = {}; function weapon.new(image,time) local obj = {img = image, time = time,bullets={}, nbullets=0,time_act = timer.new(), running = false,current=1,dec=0}; function obj:blit() for k,v in ipairs(self.bullets) do self.img:rotate(v.ang); self.img:blit(v.x,v.y); end end function obj:start() self.running = true; self.time_act:start(); end function obj:stop() self.running = false; self.time_act:stop(); self.time_act:reset(); end function obj:shoot(x,y,vel,ang) if self.running then if self.time_act:time() > self.time then 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))}); self.time_act:reset(); end else self:start(); end end function obj:move() for k,v in ipairs(self.bullets) do v.x = v.x + v.inc_x * v.vel; v.y = v.y + v.inc_y * v.vel; if v.x > 480 or v.x < 0 or v.y > 272 or v.y < 0 then table.remove(self.bullets,k); end end end function obj:collision(x,y,w,h,deadcollision) for k,v in ipairs(self.bullets) do if v.x >= x and v.x <= x+w and v.y >= y and v.y <= y+h then if deadcollision then table.remove(obj.bullets,k); end return true; end end end return obj; end