Shooting is deleting all enemies

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Shooting is deleting all enemies

Post by fridays18 »

Hello, recently with some help of this forums users I was able to make a good bullet and enemy class system but for some reason when I shoot it kills all enemies rather than just the 1 the bullet hit

enemy.lua code

Code: Select all

--Makes enemy a class
Enemy = Object.extend(Object)

--calling enemy() plays this function
function Enemy.new(self)
    self.x = 0
    self.y = math.random(0, screeny)
    self.width = math.random(5,10)
    self.height = math.random(5,10)
    self.speed = math.random(50,100)
		self.health = math.random(1,50)
		self.alive = true
		self.check = false
		self.path1 = math.random(100,200)
		self.path2 = math.random(0, screenx)
		
end

function Enemy.update(self, dt)


	if self.x < self.path1 then
		self.x = self.x + self.speed * dt
	end
	if self.y < self.path2 and self.x >= self.path1 then
		self.y = self.y + self.speed * dt
	end
	if self.y > self.path2 and self.x >= self.path1 then
		self.y = self.y - self.speed * dt
	end
	if self.x >= self.path1 and self.x < screenx + 10 then
		self.x = self.x + self.speed * dt
	end
	


	if self.x > screenx then
		self.alive = false
	end
	
end

function Enemy.draw(self)
    love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end

main.lua relevant code

Code: Select all

function love.update(dt)
	Timer.update(dt)
	
	player:update(dt)
--bullet update
	for i,v in ipairs(listOfBullets) do
        v:update(dt)
				if not v.alive then
            --Remove it from the list
            table.remove(listOfEnemies, i)
        end
    end
	--enemy update
	for i,v in ipairs(listOfEnemies) do
        v:update(dt)
    end

	for i,v in ipairs(listOfBullets) do
  for i2, v2 in ipairs(listOfEnemies) do
    BuEn = checkCollision(v.x,v.y,2,2,v2.x,v2.y,v2.width,v2.height)
			if BuEn == true then
				BuEn = false
				table.remove(listOfBullets, v)
				table.remove(listOfEnemies, v2)
			end
  end
	end


end

function love.keypressed(key)
	if key == "e" then
        --Put a new instance of Bullet inside listOfBullets.
        table.insert(listOfBullets, Bullet(player.x, player.y))
    end
end
any help would be greatly appreciate :)
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Shooting is deleting all enemies

Post by fridays18 »

All is fixed, the issues were I was removing the entire table of enemies when a bullet died, and that I mixed up i and v
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Shooting is deleting all enemies

Post by darkfrei »

Code: Select all

love.window.setMode(1280, 800) -- Steam Deck resolution
Width, Height = love.graphics.getDimensions( )


function createBlocks (amount)
	for i = 1, amount do
		local w = math.random (10, 40)
		local h = math.random (10, 20)+math.random (10, 20)
		local x = math.random (Width/2-w)
		local y = math.random (Height-h)
		local vx = 0
		local vy = 10
		local block = {x=x, y=y, w=w, h=h, vx=vx, vy=vy, valid=true}
		table.insert(Blocks, block)
	end
end


function createBullets (amount)
	for i = 1, amount do
		local w = 10
		local h = 2
		local x = Width-2*w
		local y = math.random (Height-h)
		local vx = -300
		local vy = 0
		local bullet = {x=x, y=y, w=w, h=h, vx=vx, vy=vy, valid=true}
		table.insert(Bullets, bullet)
	end
end

function love.load()
	Blocks = {}
	createBlocks (40)
	
	Bullets = {}
	createBullets (10)
end

function CheckCollision(a, b)
	local x1,y1,w1,h1 = a.x,a.y,a.w,a.h
	local x2,y2,w2,h2 = b.x,b.y,b.w,b.h
	return 	x1 < x2+w2 and
			x2 < x1+w1 and
			y1 < y2+h2 and
			y2 < y1+h1
end

 
function love.update(dt)
	-- blocks
	for i, block in ipairs (Blocks) do
		block.x = block.x + dt*block.vx
		block.y = block.y + dt*block.vy
		if block.x < 0 or block.x+block.w > Width
			or block.y < 0 or block.y+block.h > Height then
			-- out of screen
			block.valid = false
		end
	end
	
	-- bullets
	for i, bullet in ipairs (Bullets) do
		bullet.x = bullet.x + dt*bullet.vx
		bullet.y = bullet.y + dt*bullet.vy
		if bullet.x < 0 or bullet.x+bullet.w > Width
			or bullet.y < 0 or bullet.y+bullet.h > Height then
			-- out of screen
			bullet.valid = false
		end
	end
	
	-- collisions
	for i, block in ipairs (Blocks) do
		if block.valid then
			for j, bullet in ipairs (Bullets) do
				if bullet.valid and CheckCollision (block, bullet) then
					block.valid = false
					bullet.valid = false
					break -- no other bullets
				end
			end
		end
	end
	
	-- cleaning
	-- blocks
	for i = #Blocks, 1, -1 do
		if not Blocks[i].valid then
			table.remove(Blocks, i)
			createBlocks (1)
		end
	end
	-- bullets
	for i = #Bullets, 1, -1 do
		if not Bullets[i].valid then
			table.remove(Bullets, i)
			createBullets (1)
		end
	end
end


function love.draw()
	-- blocks
	love.graphics.setColor(1,1,1)
	for i, block in ipairs (Blocks) do
		love.graphics.rectangle('line', block.x, block.y, block.w, block.h)
	end
	
	-- bullets
	love.graphics.setColor(1,1,0)
	for i, bullet in ipairs (Bullets) do
		love.graphics.rectangle('line', bullet.x, bullet.y, bullet.w, bullet.h)
	end
end
2022-11-17T17_44_57-Untitled.png
2022-11-17T17_44_57-Untitled.png (14.58 KiB) Viewed 545 times
Screenshot_20221117-195943.jpg
Screenshot_20221117-195943.jpg (54.4 KiB) Viewed 511 times
Attachments
shooting-bullets-01.love
(998 Bytes) Downloaded 30 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 45 guests