enemy removal code (UPDATE: caused a hit detection bug)

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.
User avatar
GungnirDev
Party member
Posts: 119
Joined: Thu Jun 28, 2012 10:31 pm

Re: simple enemy removal code

Post by GungnirDev »

Thanks! I am a huge nüb when it comes to programming, but I wanted my first game to be something presentable to someone who might hire me out of college just as an example of my work. There's also supposed to be small cutscenes at some bits but that's all fluff so I'm leaving it for last. :3
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
User avatar
GungnirDev
Party member
Posts: 119
Joined: Thu Jun 28, 2012 10:31 pm

Re: simple enemy removal code (has caused a hit detection bu

Post by GungnirDev »

After much exploration I still cannot find the error in the hit detection at all, I am quite lost. Can Wojak or anyone else help?
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
User avatar
GungnirDev
Party member
Posts: 119
Joined: Thu Jun 28, 2012 10:31 pm

Re: enemy removal code (UPDATE: caused a hit detection bug)

Post by GungnirDev »

hello?
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: enemy removal code (UPDATE: caused a hit detection bug)

Post by Wojak »

like i pointed in the previous post You are using the enemies table in ship.lua so you will need to change the ipairs to pairs there as well

there are likely some other changes to be made, but i can't look into it right now, sorry

BTW: i tested the game on my home PC (previously i was using the crappy office one) and its to fast... This is why You should use "delta time"...
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: enemy removal code (UPDATE: caused a hit detection bug)

Post by Wojak »

ship.lua

Code: Select all

player = {
	width = 50,
	height = 40,
	x = 500,
	y = 700,
	}

	player.shots = {}
	
	playerscore = 0
	playerhealth = 100

function player.load()

	jet = love.graphics.newImage('shmuppics/flameon.png')
	fighter = love.graphics.newImage('shmuppics/dragship.png')
	fighterL = love.graphics.newImage('shmuppics/dragship_L.png')
	fighterR = love.graphics.newImage('shmuppics/dragship_R.png')
	bullit = love.graphics.newImage('shmuppics/bullet_2.png')
	drdoom = love.graphics.newImage('shmuppics/bullet_3.png')
	drdev = love.graphics.newImage('shmuppics/bullet_3.png')	
	dark = love.graphics.newImage('shmuppics/dark.png')
	leftwing = love.graphics.newImage('shmuppics/bladewing_l.png')
	rightwing = love.graphics.newImage('shmuppics/bladewing_r.png')
	fubar = love.graphics.newImage('shmuppics/gameover.png')
	haveanicedeath = "What...did you fall asleep or something?"
	zicon = love.graphics.newImage('shmuppics/weapon.png')
	alterz = love.graphics.newImage('shmuppics/alter.png')
	hpbar = love.graphics.newImage('shmuppics/dragometer.png')
	hpsym = love.graphics.newImage('shmuppics/hpsym.png')
	speedsym = love.graphics.newImage('shmuppics/speedsym.png')
	
	buzz = love.audio.newSource('shmupbgm/bladecut.ogg', "static")
	hum = love.audio.newSource('shmupbgm/bladeopen.ogg', "static")
	getnoise = love.audio.newSource ( 'shmupbgm/generatorget.ogg', "static" )
	hit = love.audio.newSource('shmupbgm/damage.ogg', "static")
	
	weapon = 0
	boomx = 0
	boomy = 0
	firerate = 3
	zcharge = 0
	zweapon = 0
	zbuff = 0
	boost = 0
	
	anim = newAnimation(jet, 67, 67, 0.5, 0)
	seq = newAnimation(alterz, 100, 100, 0.1, 0)
	seq:setMode("once")

end

function player.keypressed(key)

	if key == "x" then
	if zweapon > 0 then
	weapon = math.random(1, 3)
	zweapon = zweapon - 1
	seq:reset()
	seq:play()
	getnoise:rewind()
	getnoise:play()
	end
end

	if key == "a" then
	if zweapon > 0 then
	zbuff = math.random(1, 2)
	zweapon = zweapon - 1
	seq:reset()
	seq:play()
	getnoise:rewind()
	getnoise:play()
	end
end	
	if weapon == 0 then
	if key == "z" then
	shot = {}
	shot.img = bullit
	shot.x = player.x + 40
	shot.y = player.y
	shot.v = -700
	shot.u = 0
	table.insert(player.shots, shot)
	laser:setVolume(0.6)
	
	love.audio.rewind(laser)
	love.audio.play(laser)
	end
end
	
	if firerate > 1 then
	if weapon == 1 then
	if key == "z" then
	shot = {}
	shot.img = dark
	shot.x = player.x + 45
	shot.y = player.y
	shot.v = -500
	shot.u = 0
	table.insert(player.shots, shot)
	laser:setVolume(1)
	laser:setPitch(0.6)
	love.audio.rewind(laser)
	love.audio.play(laser)
	firerate = 0
		end
	end
end
	
	if firerate > 2 then
	if weapon == 2 then
	if key == "z" then
	shot = {}
	shot.img = drdoom
	shot.x = player.x + 45
	shot.y = player.y
	shot.v = -600
	shot.u = 0
	table.insert(player.shots, shot)
	love.audio.play(laser)
	firerate = 0
			end
		end
	end
end
	

function player.update(dt)

	anim:update(dt)
	seq:update(dt)

if zbuff == 1 then
playerhealth = playerhealth + 0.05
boost = 0
end

if zbuff == 2 then
boost = 3
end

if playerhealth < 1 then
	gamestate = "lose"
	love.audio.stop()
	doombgm:play()
end

if playerhealth > 100 then
	playerhealth = 100
end

	if playerscore > 100 then
	haveanicedeath = "What, you call THAT a rebellion?  You'll have to do better than that!"
end

	if playerscore > 10000 then
	haveanicedeath = "We shall rememeber this battle; Not unlike the timeless struggle battle between ant and boot."
end

	if playerscore > 50000 then
	haveanicedeath = "Most admirable! Your head is almost worthy to adorn my trophy room!"
end

	if gamestate == "menu" then
	player.x = 500
	player.y = 700
	playerhealth = 100
	playerscore = 0
	weapon = 0
	zcharge = 0
	zweapon = 0
	zbuff = 0
	progress = 0
	stage = 5
	bosshealth = 100
	doombgm:rewind()	
	for k,v in pairs(enemies) do 
	enemies[k]=nil 
	end
	for k,v in pairs(bullets) do
	bullets[k]=nil
	end
	for k,v in pairs(bosses) do
	bosses[k]=nil
	end
end
	
	wings = {}
	
	if weapon == 3 then
	l_wing = {}
	l_wing.img = leftwing
	l_wing.x = player.x - 60
	l_wing.y = player.y
	table.insert(wings, l_wing) 
		
	r_wing = {}
	r_wing.img = rightwing
	r_wing.x = player.x + 80
	r_wing.y = player.y
	table.insert(wings, r_wing)
	end
	
	
	if gamestate == "lose" then
	player.x = 500
	player.y = 700
	playerhealth = 100
	kombatbgm:rewind()
	introbgm:rewind()
	for k,v in pairs(enemies) do
	enemies[k]=nil
	end
end

	if gamestate == "winstage" then
	player.x = player.x + 200 * dt
	player.y = player.y - 600 * dt
end

	firerate = firerate + dt

if gamestate == "playing" then
	for _, enemy in pairs(enemies) do
	if CheckCollision(player.x, player.y, player.width, player.height, enemy.x, enemy.y, enemy.width, enemy.height) then
	playerhealth = playerhealth - 5
	hit:rewind()
	hit:play()
		end
	end

	for _, boss in pairs(bosses) do
	if CheckCollision(player.x, player.y, player.width, player.height, boss.x, boss.y, boss.width, boss.height) then
	playerhealth = playerhealth - 3
	hit:rewind()
	hit:play()
	end
end
	
end

	--remEnemy = {}
    remShot = {}
    
    -- mark shots that are not visible for removal
    for i,v in pairs(player.shots) do
    if v.y < -100 then
    table.insert(remShot, i)
	end
end
    
     -- update those shots
    for i,v in pairs(player.shots) do
    v.x = v.x + dt * v.u
	v.y = v.y + dt * v.v
    end
	
	
	if weapon == 0 then
    for i,v in pairs (player.shots) do
    for ii,vv in pairs(enemies) do
    if CheckCollision(v.x,v.y,10,10,vv.x,vv.y,vv.width,vv.height) then
    -- mark that enemy for removal
    table.insert(remEnemy, ii)
	enemies[ii]=nil
          adscore()
	-- mark the shot to be removed
    table.insert(remShot, i)
        	end
    	end
	end
	
		for i,v in pairs (player.shots) do
    for ii,vv in pairs(bosses) do
	if CheckCollision(v.x,v.y,10,10,vv.x,vv.y,vv.width,vv.height) then
	bosshealth = bosshealth - 1
			end
		end
	end
	
end

	if weapon == 1 then
	for i,v in pairs(player.shots) do
	v.y = v.y - (math.abs(math.cos(player.x) + math.sin(shot.y) * 30))
	v.x = v.x + math.sin(player.y) + math.cos(shot.x) * 100
	end
end
        
    -- check for collision with enemies
    if weapon == 1 then
    for i,v in pairs (player.shots) do
    for ii,vv in pairs(enemies) do
        if CheckCollision(v.x,v.y,100,100,vv.x,vv.y,vv.width,vv.height) then
                -- mark that enemy for removal
                table.insert(remEnemy, ii)
		enemies[ii]=nil
               adscore()
                -- mark the shot to be removed
			end
    	end
	end

	for i,v in pairs (player.shots) do
    for ii,vv in pairs(bosses) do
	if CheckCollision(v.x,v.y,70,70,vv.x,vv.y,vv.width,vv.height) then
	bosshealth = bosshealth - 2
			end
		end
	end
	
end

	if weapon == 2 then
	for i,v in pairs(player.shots) do
    v.y = v.y - dt * 700
    end
end

	if weapon == 2 then
	for i,v in pairs (player.shots) do
    for ii,vv in pairs(enemies) do
    if CheckCollision(v.x,v.y,10,10,vv.x,vv.y,vv.width,vv.height) then
    -- mark that enemy for removal
    table.insert(remEnemy, ii)
  enemies[ii]=nil
   adscore()
    -- mark the shot to be removed
    cron.after(0.01, function()
    
            shot = {}
   			shot.img = drdoom
   			shot.x = v.x
   			shot.y = v.y
			shot.u = -500
   			shot.v = -500
   			table.insert(player.shots, shot)
   			
   			shot = {}
   			shot.img = drdoom
   			shot.x = v.x
   			shot.y = v.y
			shot.u = 500
   			shot.v = -500
   			table.insert(player.shots, shot)
   			
   			shot = {}
   			shot.img = drdoom
   			shot.x = v.x
   			shot.y = v.y
			shot.u = -1000
   			shot.v = 0
   			table.insert(player.shots, shot)
   			
   			shot = {}
   			shot.img = drdoom
   			shot.x = v.x
   			shot.y = v.y
			shot.u = 1000
   			shot.v = 0
   			table.insert(player.shots, shot)
        		end)
        	end
        end
    end
    
    for i,v in pairs (player.shots) do
    for ii,vv in pairs(bosses) do
	if CheckCollision(v.x,v.y,10,10,vv.x,vv.y,vv.width,vv.height) then
	bosshealth = bosshealth - 3
			end
		end
	end
    
end 
	if weapon == 3 then
    for i,v in pairs (wings) do
    for ii,vv in pairs(enemies) do
    if CheckCollision(v.x,v.y,40,60,vv.x,vv.y,vv.width,vv.height) then
    -- mark that enemy for removal
    table.insert(remEnemy, ii)
    enemies[ii]=nil
   adscore()
    
    	end
	end
end
    
    for i,v in pairs (wings) do
    for ii,vv in pairs(bosses) do
    if CheckCollision(v.x,v.y,40,60,vv.x,vv.y,vv.width,vv.height) then
    -- mark that enemy for removal
    bosshealth = bosshealth - 1
    
    if weapon == 3 then
    hum:rewind()
    hum:play()
    buzz:rewind()
    buzz:play()
    end  
        	end
    	end
	end
end

if weapon == 0 or weapon == 1 or weapon == 2 then
hum:rewind()
end

	--[[for i, v in pairs(remEnemy) do
	table.remove(enemies, v)
	playerscore = playerscore + 100
	zcharge = zcharge + 100
	hit:rewind()
	hit:play()
	if stage == 2 then
	progress = progress + 1
	end
end]]
	 
	if zcharge > 1900 then
	zcharge = 0
	zweapon = zweapon + 1
	end
	
	if zweapon > 3 then
	zweapon = 3
	playerhealth = playerhealth + 10
	playerscore = playerscore + 1000
	end
	
	for i,v in pairs(remShot) do
	table.remove(player.shots, v)
end
	
	mousex = love.mouse.getX()
	mousey = love.mouse.getY()

	if gamestate == "menu" then
	button_check()
end

	-- Player Movement
	if gamestate == "playing" then
	if love.keyboard.isDown('right') then
	player.x = player.x + 7 + boost
end
	if love.keyboard.isDown('left') then
	player.x = player.x - 7 - boost
end
	if love.keyboard.isDown('up') then
	player.y = player.y - 5 - boost
end
	if love.keyboard.isDown('down') then
	player.y = player.y + 5 + boost
		end
	end
end
function adscore()
           playerscore = playerscore + 100
	zcharge = zcharge + 100
	hit:rewind()
	hit:play()
	if stage == 2 then
		progress = progress + 1
	end
end
function player.draw()

	seq:draw(player.x, player.y)

	if gamestate == 'playing' then
    
    love.graphics.draw(avengepic1, 100, 700)
	
	if weapon == 3 then
	love.graphics.draw(leftwing, player.x - 60, player.y)
	love.graphics.draw(rightwing, player.x + 80, player.y)
end
	love.graphics.setColor(255,0,0)
	love.graphics.rectangle("fill", 110, 125, playerhealth * 2.2, 20)
	
	love.graphics.setColor(150,50,255)
	love.graphics.rectangle("fill", 120, 105, zcharge * 0.11, 20)
		
	love.graphics.setColor(255,255,255)
	love.graphics.draw(hpbar,
 0, 30)
	
	love.graphics.setColor(255, 255, 255)
	
	love.graphics.print("Score:"..playerscore, 130, 102)
	love.graphics.print(weapon +1, 300, 150)
	
	love.graphics.print(progress, 500, 50)

	if love.keyboard.isDown('right') == false and love.keyboard.isDown('left') == false then
	love.graphics.setColor(255,255,255)
	love.graphics.draw(fighter, player.x, player.y)
	anim:draw(player.x + 25, player.y + 45)
end
	
	if love.keyboard.isDown('right') and love.keyboard.isDown('left') == false then
	love.graphics.draw(fighterR, player.x, player.y)
	anim:draw(player.x + 45, player.y + 50)
end
	if love.keyboard.isDown('left') and love.keyboard.isDown('right') == false then
	love.graphics.draw(fighterL, player.x, player.y)
	anim:draw(player.x + 5, player.y + 50)

end
	if love.keyboard.isDown('right') and love.keyboard.isDown('left') then
	love.graphics.setColor(255,255,255)
	love.graphics.draw(fighter, player.x, player.y)
end
	
	if zweapon > 0 then
	love.graphics.draw(zicon, 100, 160)
	end
	if zweapon > 1 then
	love.graphics.draw(zicon, 150, 160)
	end
	if zweapon > 2 then
	love.graphics.draw(zicon, 200, 160)
	end
	
	if zbuff == 1 then
	love.graphics.draw(hpsym, 70, 100)
	end
	
	if zbuff == 2 then
	love.graphics.draw(speedsym, 70, 100)
	end
	
end

	if gamestate == "lose" then
	love.graphics.draw(fubar, 250, 300)
	
	love.graphics.printf(haveanicedeath, 0, 315, 1100, 'center')
	love.graphics.printf("Final Score:"..playerscore, 0, 375, 1100, 'center')
	love.graphics.printf("Press Z to finish losing", 0, 400, 1100, 'center')
end

if gamestate == "winstage" then

	love.graphics.setColor(255,255,255)
	love.graphics.draw(fighter, player.x, player.y)
	anim:draw(player.x + 25, player.y + 45)
	
	
	love.graphics.printf("Stage" ..stage.. "Complete!", 0, 350, 1100, 'center')
	love.graphics.printf("Press Z to continue", 0, 375, 1100, 'center')
	love.graphics.printf("Score:"..playerscore, 0, 400, 1100, 'center')
	
end


end
I've tested this and the collisions enemy – bullets and it seams to be OK now, but unfortunately I can't guaranty that something else is not wrong...

list of changes:
-ipairs to pairs
-removed the code:

Code: Select all

for i, v in pairs(remEnemy) do
	table.remove(enemies, v)
	playerscore = playerscore + 100
	zcharge = zcharge + 100
	hit:rewind()
	hit:play()
	if stage == 2 then
	progress = progress + 1
	end
end
and created a new function:

Code: Select all

function adscore()
           playerscore = playerscore + 100
	zcharge = zcharge + 100
	hit:rewind()
	hit:play()
	if stage == 2 then
		progress = progress + 1
	end
end
-removed remEnemy = {} from ship.lua as it is already created in foe.lua

-changed the enemy remove code:

Code: Select all

table.insert(remEnemy, ii)
enemies[ii]=nil
adscore()

I've also checked the “dt” and its OK in case of enemy movement, bullets movement and bosses movement, but not used for player movement and (I think) when calculating the interval between enemy shots (this creates constant wall of bullets on my home PC)

I'm relay sorry that my suggestion messed up Your code so bad...
My solution is not the only way, so if You don't want this changes in the code or don't understand it, you should restore the original code and try something else...
User avatar
GungnirDev
Party member
Posts: 119
Joined: Thu Jun 28, 2012 10:31 pm

Re: enemy removal code (UPDATE: caused a hit detection bug)

Post by GungnirDev »

...You're a goddamn code superhero, bro. :ultrahappy:

Thanks a bunch!
Bullets are the beauty of the blistering sky
Bullets are the beauty and I don't know why
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 3 guests