Bullet collision with enemies using bump
Posted: Mon Dec 05, 2022 4:45 pm
After a lot of forum help with bump ive become relatively proficient with it. Or so I thought before trying to incorporate basic enemy collision with bullets. The goal is that when a bullet hits a enemy the bullet and enemy get cleared from the bump world and list holding them. Currently a bullet breaks if it hits an enemy or wall(good) but the enemy has no change(not good). Heres the code any help is appreciated!
This is the entirety of bullet.lua
The specific issue in bullet.lua update()
This is the entirety of bullet.lua
Code: Select all
Bullet = Object:extend()
function Bullet:new(x, y)
self.r = 1
self.x = x
self.y = y
self.speed = 700
self.w = 10
self.h = 10
self.path = 0
self.vx = 0
self.vy = 0
self.dead = false
if player.d == 4 then
self.path = 4
self.y = self.y + 10
elseif player.d == 1 then
self.path = 1
self.x = self.x - 10
elseif player.d == 2 then
self.path = 2
self.x = self.x + 10
elseif player.d == 3 then
self.path = 3
self.y = self.y - 10
end
world:add(self, self.x, self.y, self.w, self.h)
end
function Bullet:update(dt)
self.vx = 0
self.vy = 0
if self.x > screenx or self.x < 0 or self.y > screeny or self.y < 0 then
self.dead = true
end
if self.path == 4 then
self.vy = self.vy + self.speed * dt
elseif self.path == 1 then
self.vx = self.vx - self.speed * dt
elseif self.path == 2 then
self.vx = self.vx + self.speed * dt
elseif self.path == 3 then
self.vy = self.vy - self.speed * dt
end
if self.dead == false then
self.x, self.y = world:move(self, self.x+self.vx, self.y+self.vy)
local targetX, targetY = self.x+self.vx*dt, self.y+self.vy*dt
local newX, newY = world:move(self, targetX, targetY)
if newX == targetX and newY == targetY then -- no collision
self.x, self.y = newX, newY
else -- collision
self.dead = true
world:remove(self)
--enemy update
for i,v in ipairs(listOfEnemies) do
enHIT = CheckCollision(self.x ,self.y,self.w,self.h,v.x,v.y,v.width,v.height)
if enHIT == true then
v.dead = true
world:remove(v)
table.remove(listOfEnemies, i)
enHIT = false
end
end
end
end
end
function Bullet:draw()
if self.dead == true then
love.graphics.circle("line",self.x,self.y,29)
end
--love.graphics.circle("fill", self.x, self.y,self.r)
love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
end
Code: Select all
local targetX, targetY = self.x+self.vx*dt, self.y+self.vy*dt
local newX, newY = world:move(self, targetX, targetY)
if newX == targetX and newY == targetY then -- no collision
self.x, self.y = newX, newY
else -- collision
self.dead = true
world:remove(self)
--enemy update
for i,v in ipairs(listOfEnemies) do
enHIT = CheckCollision(self.x ,self.y,self.w,self.h,v.x,v.y,v.width,v.height)
if enHIT == true then
v.dead = true
world:remove(v)
table.remove(listOfEnemies, i)
enHIT = false
end
end
end
end