Help with multiple "enemies" [Solved]

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
Leonardo2450
Prole
Posts: 15
Joined: Wed Jul 17, 2019 12:45 am
Location: Esta xd

Help with multiple "enemies" [Solved]

Post by Leonardo2450 »

I am creating a game where one has to eliminate immovable "enemies" so to speak. I need you to tell me how I can create several of them and delete them when they touch the player.
Last edited by Leonardo2450 on Sun Feb 09, 2020 2:29 am, edited 2 times in total.
-Leonardo2450-
[DATA EXPUNGED] Argentinian looking for something to do in his spare time. :awesome:
Sky_Render
Prole
Posts: 48
Joined: Fri Jul 05, 2019 2:59 am

Re: Help with multiple "enemies"

Post by Sky_Render »

How that will look depends a great deal on how you implement those enemies, and on how far you want to go with "deleting" them. If you just want to remove them from play (but keep them in memory), a simple boolean variable that the game looks for when rendering and doing collision detection will suffice:

Code: Select all

while j <= #enemy_present do
if enemy_present[j] == true then
(your code)
end
j = j + 1
end
Where j goes through the whole table of all enemies. Of course, that has some problems if those enemies need to be gone for good. If that's the case, it's best to make use of Lua's table architecture and use the table.remove command on the relevant data entries. Either way, as you can see, you'll probably be wanting to use tables to keep track of data on the enemies! Tables are easy. Here's how you set one up:

Code: Select all

enemy_name = {}
That's it. Then you can add to it like this:

Code: Select all

table.insert(enemy_name, "Foo")
Where Foo is the name you're using, obviously. A bunch of aligned tables like this can be very useful for making large arrays of things that all have related functions or code run on them! I suggest you read up on Lua table structures, they are powerful.
JJSax
Prole
Posts: 47
Joined: Fri Apr 04, 2014 3:59 am

Re: Help with multiple "enemies"

Post by JJSax »

As stated by Sky_Render, there is a lot of ways to implement this and I too recommend reading up on Lua Tables I find using metatables very handy for stuff like this, but that is more advanced and might be confusing if you're new to Lua.

My understanding of your game is similar to games where you move the player around and collect coins that are stationary. Below is some pseudocode.

Code: Select all


-- Collision detection function;
-- Returns true if two boxes overlap, false if they don't;
-- x1,y1 are the top-left coords of the first box, while w1,h1 are its width and height;
-- x2,y2,w2 & h2 are the same, but for the second box.
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2) -- taken from https://www.love2d.org/wiki/BoundingBox.lua
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end

function love.load()
	enemyType1 = { 
		-- just a list of important things about your enemy.
		-- This example is just dimensions
		width = 20,
		height = 20
	}
	enemies = {} -- empty to start here

	player = {
		x = 150,
		y = 150,
		width = 50,
		height = 50
	}

	local spawn = 10 -- total number of enemies to spawn
	for i = 1, spawn do
		-- put new enemies information into table.
		table.insert(enemies, {
			x = math.random(1, width), -- set coords
			y = math.random(1, height),
			width = enemyType1.width, -- set dimensions
			height = enemyType1.height
		})
	end

end

function love.update(dt)
	for k,v in pairs(enemies) do -- loop through all enemies.
		if CheckCollision(player.x, player.y, player.width, player.height, -- pass player info
		v.x, v.y, v.width, v.height) then -- pass info about each enemy
			table.remove(enemies, k) -- remove the enemy from the list.
		end
	end
end

function love.draw()
	-- draw it how you'd like
end

There are certainly better ways to do it, but this would work. The CheckCollision is only for two rectangles. Other algorithms will be needed for other types of geometry collisions.
User avatar
Leonardo2450
Prole
Posts: 15
Joined: Wed Jul 17, 2019 12:45 am
Location: Esta xd

Re: Help with multiple "enemies"

Post by Leonardo2450 »

Sky_Render wrote: Sat Feb 08, 2020 3:13 am How that will look depends a great deal on how you implement those enemies, and on how far you want to go with "deleting" them. If you just want to remove them from play (but keep them in memory), a simple boolean variable that the game looks for when rendering and doing collision detection will suffice:

Code: Select all

while j <= #enemy_present do
if enemy_present[j] == true then
(your code)
end
j = j + 1
end
Where j goes through the whole table of all enemies. Of course, that has some problems if those enemies need to be gone for good. If that's the case, it's best to make use of Lua's table architecture and use the table.remove command on the relevant data entries. Either way, as you can see, you'll probably be wanting to use tables to keep track of data on the enemies! Tables are easy. Here's how you set one up:

Code: Select all

enemy_name = {}
That's it. Then you can add to it like this:

Code: Select all

table.insert(enemy_name, "Foo")
Where Foo is the name you're using, obviously. A bunch of aligned tables like this can be very useful for making large arrays of things that all have related functions or code run on them! I suggest you read up on Lua table structures, they are powerful.
JJSax wrote: Sat Feb 08, 2020 6:34 am As stated by Sky_Render, there is a lot of ways to implement this and I too recommend reading up on Lua Tables I find using metatables very handy for stuff like this, but that is more advanced and might be confusing if you're new to Lua.

My understanding of your game is similar to games where you move the player around and collect coins that are stationary. Below is some pseudocode.

Code: Select all


-- Collision detection function;
-- Returns true if two boxes overlap, false if they don't;
-- x1,y1 are the top-left coords of the first box, while w1,h1 are its width and height;
-- x2,y2,w2 & h2 are the same, but for the second box.
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2) -- taken from https://www.love2d.org/wiki/BoundingBox.lua
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end

function love.load()
	enemyType1 = { 
		-- just a list of important things about your enemy.
		-- This example is just dimensions
		width = 20,
		height = 20
	}
	enemies = {} -- empty to start here

	player = {
		x = 150,
		y = 150,
		width = 50,
		height = 50
	}

	local spawn = 10 -- total number of enemies to spawn
	for i = 1, spawn do
		-- put new enemies information into table.
		table.insert(enemies, {
			x = math.random(1, width), -- set coords
			y = math.random(1, height),
			width = enemyType1.width, -- set dimensions
			height = enemyType1.height
		})
	end

end

function love.update(dt)
	for k,v in pairs(enemies) do -- loop through all enemies.
		if CheckCollision(player.x, player.y, player.width, player.height, -- pass player info
		v.x, v.y, v.width, v.height) then -- pass info about each enemy
			table.remove(enemies, k) -- remove the enemy from the list.
		end
	end
end

function love.draw()
	-- draw it how you'd like
end

There are certainly better ways to do it, but this would work. The CheckCollision is only for two rectangles. Other algorithms will be needed for other types of geometry collisions.
Thank you very much for your reply. They have really helped me a lot. I'm still learning a lot from this programming language. I started a long time ago but I still have to learn about meta tables and other things. Thank you!
-Leonardo2450-
[DATA EXPUNGED] Argentinian looking for something to do in his spare time. :awesome:
User avatar
Leonardo2450
Prole
Posts: 15
Joined: Wed Jul 17, 2019 12:45 am
Location: Esta xd

Re: Help with multiple "enemies"

Post by Leonardo2450 »

Sky_Render wrote: Sat Feb 08, 2020 3:13 am How that will look depends a great deal on how you implement those enemies, and on how far you want to go with "deleting" them. If you just want to remove them from play (but keep them in memory), a simple boolean variable that the game looks for when rendering and doing collision detection will suffice:

Code: Select all

while j <= #enemy_present do
if enemy_present[j] == true then
(your code)
end
j = j + 1
end
Where j goes through the whole table of all enemies. Of course, that has some problems if those enemies need to be gone for good. If that's the case, it's best to make use of Lua's table architecture and use the table.remove command on the relevant data entries. Either way, as you can see, you'll probably be wanting to use tables to keep track of data on the enemies! Tables are easy. Here's how you set one up:

Code: Select all

enemy_name = {}
That's it. Then you can add to it like this:

Code: Select all

table.insert(enemy_name, "Foo")
Where Foo is the name you're using, obviously. A bunch of aligned tables like this can be very useful for making large arrays of things that all have related functions or code run on them! I suggest you read up on Lua table structures, they are powerful.
JJSax wrote: Sat Feb 08, 2020 6:34 am As stated by Sky_Render, there is a lot of ways to implement this and I too recommend reading up on Lua Tables I find using metatables very handy for stuff like this, but that is more advanced and might be confusing if you're new to Lua.

My understanding of your game is similar to games where you move the player around and collect coins that are stationary. Below is some pseudocode.

Code: Select all


-- Collision detection function;
-- Returns true if two boxes overlap, false if they don't;
-- x1,y1 are the top-left coords of the first box, while w1,h1 are its width and height;
-- x2,y2,w2 & h2 are the same, but for the second box.
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2) -- taken from https://www.love2d.org/wiki/BoundingBox.lua
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end

function love.load()
	enemyType1 = { 
		-- just a list of important things about your enemy.
		-- This example is just dimensions
		width = 20,
		height = 20
	}
	enemies = {} -- empty to start here

	player = {
		x = 150,
		y = 150,
		width = 50,
		height = 50
	}

	local spawn = 10 -- total number of enemies to spawn
	for i = 1, spawn do
		-- put new enemies information into table.
		table.insert(enemies, {
			x = math.random(1, width), -- set coords
			y = math.random(1, height),
			width = enemyType1.width, -- set dimensions
			height = enemyType1.height
		})
	end

end

function love.update(dt)
	for k,v in pairs(enemies) do -- loop through all enemies.
		if CheckCollision(player.x, player.y, player.width, player.height, -- pass player info
		v.x, v.y, v.width, v.height) then -- pass info about each enemy
			table.remove(enemies, k) -- remove the enemy from the list.
		end
	end
end

function love.draw()
	-- draw it how you'd like
end

There are certainly better ways to do it, but this would work. The CheckCollision is only for two rectangles. Other algorithms will be needed for other types of geometry collisions.
Thank you very much for your reply. They have really helped me a lot. I'm still learning a lot from this programming language. I started a long time ago but I still have to learn about meta tables and other things. Thank you!
-Leonardo2450-
[DATA EXPUNGED] Argentinian looking for something to do in his spare time. :awesome:
Post Reply

Who is online

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