Code: Select all
local phys = love.physics
local graph = love.graphics
local time = love.timer
local aud = love.audio
local bullets = {}
function load()
world = phys.newWorld(1024, 768)
world:setGravity(0, 15)
graph.setBackgroundColor(100,100,255)
LCannonBody = phys.newBody(world, 256, 768, 0)
LCannonShape = phys.newCircleShape(LCannonBody, 15)
LCannonShape:setData("LeftCannon");
LBarrelBody = phys.newBody(world, 256, 758, 0)
LBarrelShape = phys.newRectangleShape(LBarrelBody, 45, 5)
LBarrelShape:setData("LeftBarrel")
LBarrelBody:setAngle(0)
MCannonBody = phys.newBody(world, 512,768,0)
MCannonShape = phys.newCircleShape(MCannonBody, 15)
MCannonShape:setData("MidCannon");
MBarrelBody = phys.newBody(world, 512, 758,0)
MBarrelShape = phys.newRectangleShape(MBarrelBody, 45, 5)
MBarrelShape:setData("MidBarrel")
MBarrelBody:setAngle(0)
RCannonBody = phys.newBody(world, 768,768,0)
RCannonShape = phys.newCircleShape(RCannonBody, 15)
RCannonShape:setData("RightCannon");
RBarrelBody = phys.newBody(world, 768, 758,0)
RBarrelShape = phys.newRectangleShape(RBarrelBody, 45, 5)
RBarrelShape:setData("RightBarrel")
RBarrelBody:setAngle(0)
local f = love.graphics.newFont(love.default_font, 14)
love.graphics.setFont(f)
mob = phys.newBody(world, math.random(50,984), 0)
mobDamage = 0
mob:setMass(0,0,200,0)
mobShape = phys.newCircleShape(mob, 5)
mobShape:setData("Mob")
nextDamage = 0
mob:applyImpulse(math.random(-15000,15000),75)
cannonFire = aud.newSound("sounds/cannon.wav")
end
function update(dt)
world:update(dt)
CurTime = love.timer.getTime()
mPosX, mPosY = love.mouse.getPosition()
lbPosX, lbPosY = LBarrelBody:getPosition()
mbPosX, mbPosY = MBarrelBody:getPosition()
rbPosX, rbPosY = RBarrelBody:getPosition()
LBarrelBody:setAngle(math.deg(math.atan2(lbPosY-mPosY,lbPosX-mPosX)))
MBarrelBody:setAngle(math.deg(math.atan2(mbPosY-mPosY,mbPosX-mPosX)))
RBarrelBody:setAngle(math.deg(math.atan2(rbPosY-mPosY,rbPosX-mPosX)))
for k, v in pairs(bullets) do
if v.b:getX() > 1024 or v.b:getX() < 0 or v.b:getY() < 0 then
table.remove(bullets, k)
end
end
end
function dist(x1,y1,x2,y2)
return ((x1^2+y1^2)^.5) - ((x2^2+y2^2)^.5)
end
text = ""
function draw()
graph.draw(text, 50, 50)
graph.setColor(200, 200, 200);
graph.polygon(love.draw_line, LBarrelShape:getPoints())
graph.polygon(love.draw_line, MBarrelShape:getPoints())
graph.polygon(love.draw_line, RBarrelShape:getPoints())
graph.circle(0,LCannonBody:getX(), LCannonBody:getY(), 15, 360)
graph.circle(0,MCannonBody:getX(), MCannonBody:getY(), 15, 360)
graph.circle(0,RCannonBody:getX(), RCannonBody:getY(), 15, 360)
for k,v in ipairs(bullets) do
graph.circle(2, v.b:getX(), v.b:getY(), 2, 360)
end
graph.setColor(255,100,100)
graph.circle(0, mob:getX(), mob:getY(), 10, 4)
graph.setColor(255,255,255)
graph.rectangle(0, mob:getX() - 13.5, mob:getY() - 19, 27, 7)
graph.setColor(255,50,50)
mobHealth = (25 - mobDamage)
graph.rectangle(0, mob:getX() - 12.5, mob:getY() - 18, mobHealth, 5)
graph.draw(mobDamage, 100, 100)
end
function keypressed(k)
if k == love.key_r then
love.system.restart()
end
end
lastclick = 0
function mousepressed(x1, y1, button)
if button == love.mouse_left and lastclick + 0.1 <= love.timer.getTime() then--or button == love.mouse_wheeldown or button == love.mouse_wheelup then --
lastclick = love.timer.getTime()
local spawner
if x1 < 384 then spawner = LBarrelBody elseif x1 < 640 then spawner = MBarrelBody else spawner = RBarrelBody end
local x2, y2 = spawner:getPosition()
local sPosX, sPosY = 512, 768
local sAngle = spawner:getAngle()
sPosX = x2 + math.cos(math.rad(sAngle-180)) * 30
sPosY = y2 + math.sin(math.rad(sAngle-180)) * 30
t = {}
bulletShapes = {}
t.b = phys.newBody(world, sPosX, sPosY, 1)
t.s = phys.newCircleShape(t.b, 3)
table.insert(bulletShapes, t.s)
--t.b:setBullet(true)
t.s:setData("Bullet")
table.insert(bullets, t)
t.b:setVelocity(math.cos(math.rad(sAngle-180)) * 5000000,math.sin(math.rad(sAngle-180)) * 5000000)
aud.play(cannonFire)
end
end
function collision(a,b,c)
local f, r = c:getFriction(), c:getRestitution()
local s = c:getSeparation()
local px, py = c:getPosition()
local vx, vy = c:getVelocity()
local nx, ny = c:getNormal()
--for k, v in pairs(bulletShapes) do
if a == "Bullet" or b == "Bullet" then
if b == "Mob" or a == "Mob" then
mobDamage = mobDamage + 2
end
bulletIndex = k
a:destroy()
b:destroy()
bullets[k]:destroy()
end
--end
end
I wasn't sure if you could reference the shapes directly in the collision function or if you had to pass them with their setData value. Either way it still didn't work.
And I know you have to destroy them in the right order, and I believe I am doing it correctly, that's not what i'm saying. :destroy() is simply throwing an error saying it doesnt exist whenever i try to use it.