Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------- weaponlib v2.0 - (c) 2011 xerpi -------------------------
- weapon={}
- --{x=0,y=0,ang=alpha,poly}
- function math.getpoly(x,y,w,h)
- --return {{ x - w/2, y - h/2 },{ x + w/2, y - h/2 },{ x + w/2, y + h/2 },{ x - w/2, y + h/2 }}
- return {x=x,y=y,w=w,h=h}
- end
- function math.outofscreen(x,y,w,h,ang)
- if x<0 or x>480 or y<0 or y>272 then return true end
- end
- function math.collision(x,y,w,h,x1,y1,w1,h1)
- if x + w >= x1 and x <= x1 +w1 and
- y + h >= y1 and y <= y1 + h1 then
- return true
- end
- end
- function weapon.create(img,time)
- return {img=img, time = time,w = img:width(),bullets={}, h = img:height(), time_act = timer.new(), status = "stop",current=1,dec=0} or {}
- end
- function weapon.blit(obj)
- local dec,i = 0,0
- for b = 1, #obj.bullets do
- i = b-dec
- obj.bullets[i].x = obj.bullets[i].x + math.cos(math.rad(obj.bullets[i].ang))*obj.bullets[i].vel
- obj.bullets[i].y = obj.bullets[i].y + math.sin(math.rad(obj.bullets[i].ang))*obj.bullets[i].vel
- if math.outofscreen(obj.bullets[i].x,obj.bullets[i].y) then
- table.remove(obj.bullets,i)
- dec=dec+1
- continue
- end
- obj.img:rotate(obj.bullets[i].ang)
- --math.poly.rotate(0,0,0,obj.bullets[i].poly,obj.bullets[i].ang);
- obj.img:blit(obj.bullets[i].x,obj.bullets[i].y)
- obj.img:reset()
- end
- end
- function weapon.shoot(obj,x,y,vel,ang)
- if obj then
- if obj.status == "stop" then obj.status = "run" obj.time_act:start() end
- if obj.status == "run" then
- if obj.time_act:time() >= obj.time then
- table.insert(obj.bullets,{vel=vel,x=x,y=y,ang=ang,poly=math.getpoly(x,x,obj.w,obj.h)})
- obj.time_act:reset()
- end
- end
- end
- end
- function weapon.collision(obj,x,y,w,h,deadcollision)
- local dec,i = 0,0
- for b = 1, #obj.bullets do
- i = b-dec
- --obj.bullets[i].x = obj.bullets[i].x + math.cos(math.rad(obj.bullets[i].ang))*obj.bullets[i].vel
- --obj.bullets[i].y = obj.bullets[i].y + math.sin(math.rad(obj.bullets[i].ang))*obj.bullets[i].vel
- --math.poly.rotate(0,0,0,obj.bullets[i].poly,obj.bullets[i].ang);
- if math.collision(obj.bullets[i].x,obj.bullets[i].y,obj.bullets[i].poly.w,obj.bullets[i].poly.h,x,y,w,h) then
- if deadcollision then
- table.remove(obj.bullets,i)
- dec=dec+1
- end
- return true
- end
- end
- end
- function weapon.action(obj,action)
- if action == "start" then obj.status = "run" obj.time_act:start() end
- if action == "stop" then obj.status = "stop" obj.time_act:reset() obj.time_act:stop() end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement