Weapons / Multiple Enemies / ** Now Bullets **

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.
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Weapons / Multiple Enemies / ** Now Bullets **

Hi guys, I was curious on how to create a basic weapon. I was looking at Jasoco's post here http://love2d.org/forums/viewtopic.php?f=4&t=2061, though I didn't quite understand how everything worked. I think I could create a basic weapon but it wouldn't be very efficient, and wouldn't be able to create or destroy bullets. Could anyone walk me thorough this?

Edit:

I was also curious on how to spawn multiple enemies.

Currently I have them set up like this. It also has a "state" to handle the animation that is played.

love.load

Code: Select all

	enemy = {
hp = 100,
status = "ALIVE",
x = 600,
y = 150,
w = 14,
h = 16,
speed = 20,
}

enemy_state = enemy_anim.enemy_walkleft

love.draw

Code: Select all


function love.draw()
enemy_state:draw(enemy.x, enemy.y)
end


And some of which is handled by these functions:

Code: Select all


function CheckCollision(obj1, obj2)

if obj1.x > obj2.x + obj2.w - 1 or
obj1.y > obj2.y + obj2.h - 1 or
obj2.x > obj1.x + obj1.w - 1 or
obj2.y > obj1.y + obj1.h - 1
then

return false             -- No collision. Yay!
else
return true         -- Yes collision. Ouch!
end

end

function CheckDirection(obj1, obj2)

if obj1.x > obj2.x + obj2.w - 1 then
enemy_direction = "right"
return false

elseif obj1.y > obj2.y + obj2.h - 1 then
enemy_direction = "down"
return false

elseif obj2.x > obj1.x + obj1.w - 1 then
enemy_direction = "left"
return false

elseif obj2.y > obj1.y + obj1.h - 1 then
enemy_direction = "up"
return false

else
return true         -- Yes collision. Ouch!
end

end


I was thinking that using my current code would work, since the enemy.x, and enemy.y would have to be independent variables for each of the enemies. Is there an easy way to handle this?
Last edited by Ryne on Mon Nov 15, 2010 1:56 am, edited 2 times in total.
@rynesaur

ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Weapons?

Maybe I can help a little. With anything, you should always start by defining what exactly you're going to be modeling in code, so:

A weapon has what sort of properties? We can start with a really simple model.
• Firing rate
Damage done

some sort of "onFire" action, presumably a bullet spawner.
And each bullet flying through the air, they're going to have properties too, some simple ones might be:
• x location,
y location,
x velocity _
y velocity _ \__ These two together are also known as it's "velocity vector"

some sort of "checkCollision" member.
some sort of "updateLocation" member
A very simple beginning model would be to have a table of "active bullets," where new bullets are inserted into when our player fires his/her weapon. These bullets would be table.remove()'d when they hit something or leave the screen.

Maybe this will give you a few ideas?

Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Weapons?

ninwa wrote:Maybe I can help a little. With anything, you should always start by defining what exactly you're going to be modeling in code, so:

A weapon has what sort of properties? We can start with a really simple model.
• Firing rate
Damage done

some sort of "onFire" action, presumably a bullet spawner.
And each bullet flying through the air, they're going to have properties too, some simple ones might be:
• x location,
y location,
x velocity _
y velocity _ \__ These two together are also known as it's "velocity vector"

some sort of "checkCollision" member.
some sort of "updateLocation" member
A very simple beginning model would be to have a table of "active bullets," where new bullets are inserted into when our player fires his/her weapon. These bullets would be table.remove()'d when they hit something or leave the screen.

Maybe this will give you a few ideas?
This helps somewhat, I'll see what I can put together, and post back If I need more help. Thanks!
@rynesaur

ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Weapons / Multiple Enemies.

Instead of having your enemy details in a one level table like that, do something like this:

Code: Select all


enemies = {}
function spawnEnemy(x,y, {other details})
table.insert(enemies, { x = x, y = y, other details = other details})
end


Replace other details with all of the parameters you want to give a new enemy, and remove the brackets.

And from here you can reference your enemies using

Code: Select all



enemies[1] -- This is our first enemy
enemies[2] -- This would be our second enemy, you can refer to him using this.

-- You can refer to all enemies using this
for i, #enemies do
enemies[i] -- This is a loop that goes through all of our enemies.
end


Cheers,
Ninwa

Disclaimer: A little drunk right now.

Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Weapons / Multiple Enemies.

ninwa wrote:Instead of having your enemy details in a one level table like that, do something like this:

Code: Select all


enemies = {}
function spawnEnemy(x,y, {other details})
table.insert(enemies, { x = x, y = y, other details = other details})
end


[/code]

Cheers,
Ninwa

Disclaimer: A little drunk right now.
I'm having some trouble with that. For this section:

Code: Select all


enemies = {}

function spawnEnemy(x,y, {other details})
table.insert(enemies, { x = x, y = y, other details = other details})
end


I'm not sure exatly how this works. Should it look like this after I change the values?

Code: Select all

function spawnEnemy(x,y, hp)
table.insert(enemies, x = 100, y = 100, hp = 100)
end

Creating and using functions is still relatively new to me. I was also wondering how to refer back to each of those enemies. So if the first enemy is enemy[1] how would I print enemy[1]'s HP on screen? enemy[1].hp?

Thanks again.
@rynesaur

ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Weapons / Multiple Enemies.

Ryne: http://codepad.org/XM4QwNYP

Codepad fucked the formatting up a little bit, do your best to ignore that.

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Weapons / Multiple Enemies.

Ryne wrote:I'm not sure exatly how this works. Should it look like this after I change the values?

Code: Select all

function spawnEnemy(x,y, hp)
table.insert(enemies, x = 100, y = 100, hp = 100)
end
Yes, if you call spawnEnemy(100, 100, 100).
Ryne wrote:Creating and using functions is still relatively new to me. I was also wondering how to refer back to each of those enemies. So if the first enemy is enemy[1] how would I print enemy[1]'s HP on screen? enemy[1].hp?
Yup.

It works with arbitrarily nested tables:

Code: Select all

f = {{{{{"Bonus"}}}}}
print(f[1][1][1][1][1]) -- prints "Bonus"
Help us help you: attach a .love.

ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Weapons / Multiple Enemies.

Specifically addressing:
Ryne wrote:

Code: Select all

function spawnEnemy(x,y, hp)
table.insert(enemies, x = 100, y = 100, hp = 100)
end

This should be:

Code: Select all

function spawnEnemy(x,y, hp)
table.insert(enemies, {x = x, y = y, hp = hp})
end

I didn't mean to be confusing, but the { and } within table.insert was meant to stay. Sorry, that was my fault. Also, as Robin said, from the code I just posted, you could now do spawnEnemy(100,100,100) and spawn an enemy at location x = 100, y = 100 with 100 hp.

And yes, enemies[1].hp would give you the hp of your first spawned enemy.

Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Weapons / Multiple Enemies.

I just created an extensive thread asking 3 questions, at the completion of which I exited my browser rather casually instead of actually submitting the reply. My rage is attempting to take control and force me to refrain from reposting the questions, while at the same time my OCD is pushing to resubmit. Alas, I will go with the OCD and try to remember what I typed.

First of all, thanks. I appreciate you typing everything out like that, I completely understand how it works now. I would also re-ask those 2 questions now, however stupid they may be.

Question 1:

Here you have the "enemies" table outside of love.load. I thought that ALL tables had to be in love.load(). I guess that isn't the case, though I was just wondering if the table would still work, even if it were in love.load.

Question 2:

You created a separate function to handle enemy drawing (function drawEnemies()) Obviously the code is nicer like this, but could the contents of this function just have easily been dropped into love.draw instead?

This reply is significantly shorter than my previous one.

Thanks again!
@rynesaur

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Weapons / Multiple Enemies.

1: It's not really that different in effect, it only changes the moment at which it is set (or emptied, depending on previous state). However, if you keep everything in love.load() then you can just rerun that to restart the game, which is quite a big advantage.

2: Sure.

Who is online

Users browsing this forum: pgimeno and 27 guests