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.
User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Weapons / Multiple Enemies.

Post by ninwa »

Ryne wrote:
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.
I'm not entirely sure why love.load exists. It's probably an aesthetic decision, or I may not be privy to something, but it essentially, it doesn't matter where you declare something. Once it's declared it exists anywhere in your program unless you use the 'local' keyword, which will limit its use to that scope. Without getting into a lesson on scope, the short answer is: It doesn't matter, but putting it in love.load makes it "prettier."
Ryne wrote:
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?
Yes, but modularizing your code maintains readability and allows for flexibility. It's easy to over-do it, and in this example it's not even necessary. So yes, that for-loop works equally well within your draw function. Just be sure to comment it so that it's clear what it is exactly we're drawing.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Weapons / Multiple Enemies.

Post by bartbes »

ninwa wrote:I'm not entirely sure why love.load exists. [...] the short answer is: It doesn't matter, but putting it in love.load makes it "prettier."
As for the first part, love.load isn't called when the file is loaded, so there's a time difference, the most important thing is that the window is created just before love.load is called (iirc), and, as I pointed out before, an added bonus is that having a function makes it easier to reset in one go.
As for the second part, in this case it doesn't matter, but as I just said, it can matter.
User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Weapons / Multiple Enemies.

Post by ninwa »

bartbes wrote:
ninwa wrote:I'm not entirely sure why love.load exists. [...] the short answer is: It doesn't matter, but putting it in love.load makes it "prettier."
As for the first part, love.load isn't called when the file is loaded, so there's a time difference, the most important thing is that the window is created just before love.load is called (iirc), and, as I pointed out before, an added bonus is that having a function makes it easier to reset in one go.
As for the second part, in this case it doesn't matter, but as I just said, it can matter.
This makes sense. Your point about using it as a way of re-setting the applications state is completely valid.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Weapons / Multiple Enemies.

Post by Robin »

bartbes wrote:As for the first part, love.load isn't called when the file is loaded, so there's a time difference, the most important thing is that the window is created just before love.load is called (iirc),
boot.lua sayz no. Unless you harness the power of conf.lua, main.lua is only loaded after the screen is set up. The one thing that happens between main.lua is run and love.run() is called (and thus love.load()) is that the console could be opened on Windows.
Help us help you: attach a .love.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Weapons / Multiple Enemies.

Post by Ryne »

Thanks for the replies. I have another question. I was wondering how to refer to "Any" table value. For example, right now I have an "enemy_direction" and a series of "if statements" that change the direction the sprite is facing. I would like to give each of my enemies a direction so that they can independently turn themselves in relevance to player position, Since right now they turn and look at the character.

So heres what I have. First a function that will change enemy direction depending on where the player is. I know it's sloppy.

Code: Select all


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
and here are the if statements (i know there should be elseif's, will clean later):

Code: Select all


	if enemy_direction == "right" then
	enemy_state = enemy_anim.enemy_walkright
	end

	if enemy_direction == "left" then
	enemy_state = enemy_anim.enemy_walkleft
	end

	if enemy_direction == "up" then
	enemy_state = enemy_anim.enemy_walkup
	end

	if enemy_direction == "down" then
	enemy_state = enemy_anim.enemy_walkdown
	end

So I guess instead of there being one single "enemy_direction" I would like to add a direction to each enemy. So it would be like this:

Code: Select all

if enemies[1].dir == "down" then
enemy_state = enemy_anim.enemy_walkdown
end
But obviously I would like that to apply to ALL enemies, not just the single one. Else, I would have to rewrite that IF for every enemy.

Also after re-reading I would need to give each enemy it's own enemy_state as well. Though this would be the same as direction so It's not a problem.

I'm really tired at the moment, have been up about 30 hours now so excuse any mistakes I may have made. I've uploaded the love if you would like to check through my code. I know everything is all over the place right now, but I will of course optimize everything when I need to. Thanks again everyone!
http://dl.dropbox.com/u/7905642/zombies_00.love
@rynesaur
User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Weapons / Multiple Enemies.

Post by ninwa »

Sorry Ryne, I just caught this before going to bed myself, so I mostly skimmed your question, but if I understand you correctly: To access all of your enemies you need to do it iteratively.

Iteration is a term often used in omputer programming and it means to go through an array (or in our case, table) item by item.

Check back to how you're drawing every enemy.

Code: Select all

for i=1, #enemies do
    --- {your code here}
end
Since #enemies will give us how many enemies exist in your enemies table, by looping that many times and incrementing i by 1 each time, we can use enemies to refer to every enemy through the course of our loop.

Time for sleepies now!
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Weapons / Multiple Enemies.

Post by Ryne »

Alright, Thanks for the reply, I'll try this out and post my results. Thanks for the help!
@rynesaur
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Weapons / Multiple Enemies.

Post by nevon »

Ryne wrote:But obviously I would like that to apply to ALL enemies, not just the single one. Else, I would have to rewrite that IF for every enemy.
That's right. You want each instance of your enemy to have a separate state. This you should know how to do, since you've done it for loads of other values, like position and hp.

Once you've done that and cleaned up your functions a bit, you're going to have to use a loop. A loop is basically doing something several times. In our case, we're going to be doing something to each instance of your enemy. Like so:

Code: Select all

for i,v in ipairs(enemies) do
    checkDirection(v, player)
end
i (you can call it whatever you want) means index. In other words, it's the current iteration of your loop.
v (which you can also call whatever you want) means value. It's the object in your table that's currently "active". Another way to see it is that the i object in your table is v.
table = v

I think I'll leave the explaining to kikito in the future. I might just have confused you more.

EDIT: Ah. Ninja'd by ninwa. That's probably a good thing.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Weapons / Multiple Enemies.

Post by Ryne »

Thanks nevon. I'm way too tired to continue this, though I'm still really confused about something. I'll reply again tomorrow with a better, more understandable description of my problem.

Thanks guy, goodnight.
@rynesaur
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Weapons / Multiple Enemies.

Post by Ryne »

It seems I posted this without seeing nevon's post, since It helps in this case, though any input is still appreciated, thanks.

Alright so I've been at this for about an hour now and I can't figure it out (+ a major headache). Basically I have this direction function. Which I can use to check the direction between player, and enemies[1](or enemies[2] etc.) but I would like for this to apply to ALL enemies. The current code that I use for direction checking is this.

The function

Code: Select all

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
Conditional

Code: Select all

	if not CheckCollision(player, enemies[1]) then
	-- the vector from enemy to the player
	local vx = player.x - enemies[1].x
	local vy = player.y - enemies[1].y

	-- normalize it, i.e. divide it by it's length
	local length = math.sqrt(vx * vx + vy * vy)
	vx = vx / length
	vy = vy / length

	-- move the zombie towards the player
	enemies[1].x = enemies[1].x + vx * enemies[1].speed * dt
	enemies[1].y = enemies[1].y + vy * enemies[1].speed * dt
	end
I tried to figure out how to use th code you posted here

Code: Select all

for i=1, #enemies do
    --- {your code here}
end
But I couldn't think of a way to implement it.. Any help would be appreciated. Thanks guys.
@rynesaur
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 22 guests