Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------- weaponlib v1.5 - (c) 2011 xerpi -------------------------
- weapon={}
- function weapon.create(tableimg,vel,tiempo)
- weaponobj = {img={},size={width={},height={}}, tim = tiempo, tim_act = timer.new(),vel = vel , status = "stop",current=1,maxshoots=100}
- weaponobj.img["up"] = tableimg["up"] or "none"
- weaponobj.img["down"] = tableimg["down"] or "none"
- weaponobj.img["right"] = tableimg["right"] or "none"
- weaponobj.img["left"] = tableimg["left"] or "none"
- weaponobj.size.width["up"] = (tableimg["up"] or image.create(0,0) ):width() or 0
- weaponobj.size.width["down"] = (tableimg["down"] or image.create(0,0) ):width() or 0
- weaponobj.size.width["right"] = (tableimg["right"] or image.create(0,0) ):width() or 0
- weaponobj.size.width["left"] = (tableimg["left"] or image.create(0,0) ):width() or 0
- weaponobj.size.height["up"] = (tableimg["up"] or image.create(0,0) ):height() or 0
- weaponobj.size.height["down"] = (tableimg["down"] or image.create(0,0) ):height() or 0
- weaponobj.size.height["right"] = (tableimg["right"] or image.create(0,0) ):height() or 0
- weaponobj.size.height["left"] = (tableimg["left"] or image.create(0,0) ):height() or 0
- weaponobj.bullets={}
- for i = 1, 100 do
- table.insert(weaponobj.bullets,{x=0,y=0,dir="",status="dead"})
- end
- return weaponobj
- end
- function weapon.toblit(obj,num)
- if obj and obj.bullets[num].status == "alive" and obj.img[obj.bullets[num].dir] != "none" then
- local direction = obj.bullets[num].dir
- obj.img[obj.bullets[num].dir]:blit(obj.bullets[num].x,obj.bullets[num].y)
- if obj.bullets[num].dir == "up" then
- obj.bullets[num].y = obj.bullets[num].y - obj.vel;
- elseif obj.bullets[num].dir == "down" then
- obj.bullets[num].y = obj.bullets[num].y + obj.vel;
- elseif obj.bullets[num].dir == "right" then
- obj.bullets[num].x = obj.bullets[num].x + obj.vel;
- elseif obj.bullets[num].dir == "left" then
- obj.bullets[num].x = obj.bullets[num].x - obj.vel;
- end
- if obj.bullets[num].y+obj.size.height[direction] < 0 or obj.bullets[num].y >272 or obj.bullets[num].x+obj.size.width[direction] < 0 or obj.bullets[num].x >480 then
- obj.bullets[num].status = "dead"
- end
- end
- end
- function weapon.tocoll(obj,num,x,y,w,h,deadcollision)
- local cw, ch = obj.size.width[obj.bullets[num].dir], obj.size.height[obj.bullets[num].dir]
- if obj.bullets[num].status == "alive" then
- if obj.bullets[num].x + cw >= x and obj.bullets[num].x <= x+w and
- obj.bullets[num].y + ch >= y and obj.bullets[num].y <= y+h then
- if deadcollision then obj.bullets[num].status = "dead" end
- return true
- end
- end
- end
- function weapon.blit(obj)
- for i = 1, 100 do
- weapon.toblit(obj,i)
- end
- end
- function weapon.shoot(obj,x,y,dir,funct,sonido)
- if obj.status == "stop" then obj.status = "run" obj.tim_act:start() end
- if obj.status == "run" then
- if obj.tim_act:time() >= obj.tim then
- if sonido then sonido:play() end
- obj.bullets[obj.current]={x=x,y=y,dir=dir,status="alive"}
- obj.current=obj.current+1
- if obj.current >obj.maxshoots then obj.current = 1 end
- obj.tim_act:reset()
- if funct then funct() end
- end
- end
- end
- function weapon.collision(obj,x,y,w,h,deadcollision)
- for i = 1, 100 do
- if weapon.tocoll(obj,i,x,y,w,h,deadcollision) then return true end
- end
- end
- function weapon.action(obj,action)
- if action == "start" then obj.status = "run" obj.tim_act:start() end
- if action == "stop" then obj.status = "stop" obj.tim_act:reset() obj.tim_act:stop() end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement