This may help you:
Code: Select all
gun = {}
weapon = 1
time_last_bullet = 0
time_between_bullet = .1
function gun:load( x, y, r )
if love.timer.getTime() > time_last_bullet + time_between_bullet then
if weapon == 1 then
r = 3
s = 200
time_between_bullet = .1
d = 5
elseif weapon == 2 then
r = 2
s = 500
time_between_bullet = .01
d = 1
else
r = 5
s = 100
time_between_bullet = 1
d = 10
end
startX = player[1].x + player[1].w + r/2
startY = player[1].y + player[1].h/2 + r/2
dir = math.atan2(( y - startY ), ( x - startX ))
gunDx = s * math.cos(dir)
gunDy = s * math.sin(dir)
table.insert( gun, { x = startX, y = startY, dx = gunDx, dy = gunDy, id = true, r = r, t = weapon, d = d } )
time_last_bullet = love.timer.getTime()
end
end
function gun:switch_weapon()
weapon = weapon + 1
if weapon > 3 then
weapon = 1
end
print("Weapon: "..weapon)
end
function gun:draw()
for i, v in ipairs(gun) do
if v.id then
if gun[i].t == 1 then
love.graphics.setColor( 0, 0, 0 )
love.graphics.circle( "fill", math.floor(gun[i].x), math.floor(gun[i].y), gun[i].r )
elseif gun[i].t == 2 then
love.graphics.setColor( 255, 200, 0 )
love.graphics.circle( "fill", math.floor(gun[i].x), math.floor(gun[i].y), gun[i].r )
elseif gun[i].t == 3 then
love.graphics.setColor( 0, 0, 255 )
love.graphics.circle( "fill", math.floor(gun[i].x), math.floor(gun[i].y), gun[i].r )
end
end
end
end
function gun:update(dt)
for i, v in ipairs(gun) do
if v.id then
v.x = v.x + v.dx * dt
v.y = v.y + v.dy * dt
end
end
gun:collide()
end
function gun:collide()
for b = #gun, 1, -1 do
if gun[b].x < 0 + gun[b].r or gun[b].x > 800 - gun[b].r or gun[b].y < 0 + gun[b].r or gun[b].y > 600 - gun[b].r then
print("too far!")
table.remove(gun, b)
end
end
for b = #gun, 1, -1 do
for e = #enemy, 1, -1 do
if gun_and_enemy_overlap( gun[b].x, gun[b].y, gun[b].r, enemy[e].x, enemy[e].y, enemy[e].pic:getWidth(), enemy[e].pic:getHeight() ) then
print("hit!")
enemy[e].hp = enemy[e].hp - gun[b].d
table.remove(gun, b)
print("enemy hp: "..enemy[e].hp)
if enemy[e].hp <= 0 then
table.remove(enemy, e)
end
end
end
end
end
function gun_and_enemy_overlap( bx, by, bw, bh ex, ey, ew, eh )
if bx + bw > ex and
bx < ex + ew and
by + bh > ey and
by < ey + eh then
return true
end
end
function love.draw()
love.graphics.scale(1.5)
--Draw is for graphics
if gamestate == "playing" then
love.graphics.setColor(255, 255, 255)
love.graphics.draw(backfap, 0, 0)
drawMap()
playerDraw()
bulletDraw()
DrawEnemy()
setSpawn()
shadowDraw()
guiDraw()
end
if gamestate == "menu" then
menuDraw()
end
if gamestate == "options" then
end
if gamestate == "result" then
endScreen()
end
gun:draw()
end
function love.update(dt)
player.healthpercentage = math.floor((player.health/player.maxhealth)*100) --Update means it Updates every frame (Too much leads to lag)
if gamestate == "playing" then
backMenu()
playerMove(dt)
updateEnemy(dt)
bulletShoot(dt)
bulletUpdate(dt)
srdUPS()
shadowMath()
end
if gamestate == "menu" then
menuControl()
end
if gamestate == "options" then
end
if gamestate == "result" then
playerReset()
end
if love.mouse.isDown('l') then
gun:load( love.mouse.getX(), love.mouse.getY(), r )
end
gun:update(dt)
end
end

If you need any explanations, just ask!