Page 1 of 2

Enemy Spawning

Posted: Mon Nov 12, 2012 8:24 pm
by SuperMeijin
Hello

I have one enemy at the moment which I can kill.
I would like to have this enemy spawn multiple time every x seconds at different position.
I have no idea how to do this

Thanks

Re: Enemy Spawning

Posted: Mon Nov 12, 2012 8:41 pm
by Patalo
Hi,

One simple solution is to use the hump's timer library which has a function quite adapted :
http://vrld.github.com/hump/#hump.timer

For example, if you want to spawn enemies every 5 seconds, you would have to call (only once) this :

Code: Select all

Timer.addPeriodic(5, function()
    spawnTonsOfEnnemies()
end)
Where spawnTonsOfEnnemies() picks some random location and add the ennemies.

Have fun!

Re: Enemy Spawning

Posted: Tue Nov 13, 2012 8:11 pm
by SuperMeijin
Sounds good but any help on the spawnTonsOfEnnemies() :3

Re: Enemy Spawning

Posted: Tue Nov 13, 2012 8:29 pm
by mickeyjm
From what I can tell the one enemy you have is a premade table, instead you should create tables for enemies that go in an enemies table that stores all of them with that in mind spawnTonsOfEnemies() would be something like:

Code: Select all

function spawnTonsOfEnemies()
    for i=1,10 do --Replace 10 with number of enemies you want
        enemies[#enemies+1]={
        --Put data for enemy here (such as health, position, velocity etc)
        }
    end
end

Re: Enemy Spawning

Posted: Tue Nov 13, 2012 8:34 pm
by Larsii30
I could tell you how I do it . It should be readable but I'm not sure if this is efficient spawning code.
hopefully you know what self means.

First you have to create a global enemy table to save the functions and values.
Then load everything with a init / load function( or declare this anywhere else [ i.e in the love.load() ] ):

Code: Select all

enemy = {}

function enemy:init()
	self.table = {} -- stores the single enemys later
	self.timer = 0 -- use with spawntimer
	self.spawnTime = 3 -- time when the next enemy should spawn
end
Then I create a function to generate a new enemy. This create a table into the self.table( self = enemy / the name of the global table ) for the enemy.

Code: Select all

function enemy:new( x, y, xvel ) -- to use self you have to type a " : " to say that you want to use the self. 
	
	local temp = 	{
						x = x,
						y = y,
						w = 32,
						h = 64,
						xvel = xvel,
					}
	table.insert( self.table, temp )
end
Now you have to update the spawn and the enemy itself( here is just the spawner ).

Code: Select all

function enemy:update( dt )

	self.timer = self.timer + dt
	if self.timer > self.spawnTime then
		enemy:new( math.random( -200, width + 200 ) , height - 64 , 150 ) -- math.random for a different position everytime you spawn an enemy
		self.timer = 0
	end

        -- update anything else enemy related here
end

yeah, and now there is only one thing left to do. Draw all enemys.
the ipairs goes through the full self.table ( enemy.table = stores the single enemys ). v is the value of a single enemy, i is the index ( the position of the enemy in the self.table )

Code: Select all

function enemy:draw()
	
	for i, v in ipairs( self.table ) do
		lg.setColor( 0, 0, 255 )
		lg.rectangle( "line", v.x, v.y, v.w, v.h )
	end

end
Hopefully I could help.

Re: Enemy Spawning

Posted: Wed Nov 14, 2012 8:00 pm
by SuperMeijin
Getting this problem
Heres my table

Code: Select all

 
   zombie = {
   x = math.random(0,728),
   y = math.random(0,428)
   }
and

Code: Select all

function zombie:init()
   self.table = {}
   self.timer = 0
   self.spawnTime = 3
end
and this error
Atempt to index global zombie a nil value?

Re: Enemy Spawning

Posted: Wed Nov 14, 2012 8:18 pm
by Larsii30
May you should begin with the code from mickeyjm. It's much easier to understand.

The zombie table didn't get x, y values. They have to be in the self.table create like I wrote or you combine the code from
mickeyjm with mine. just remove function enemy:new and insert his code in the update.

Code: Select all


zombie = {}

function spawnZombies()
    for i=1,10 do --Replace 10 with number of enemies you want
        zombies[#zombies+1]={
        x = math.random(0,728),
        y = math.random(0,428),
        }
    end
end

function zombie:init()

   self.timer = 0
   self.spawnTime = 3

end

function zombie:update( dt )

   self.timer = self.timer + dt
   if self.timer > self.spawnTime then
     spawnZombies()  -- his code
      self.timer = 0
   end

        -- update anything else enemy related here
end


Re: Enemy Spawning

Posted: Wed Nov 21, 2012 5:45 am
by Alban
You have Deathmatch AI activated, so that's why they shoot eachother.

You should set your spawncount to a global variable that would fix your problem I think.
Can't see that much on these screens though.

Re: Enemy Spawning

Posted: Sat Nov 24, 2012 3:34 pm
by SuperMeijin
I know this thread is old but how do I draw a image on these enemies. For my old single enemy I was doing

Code: Select all

love.graphics.draw(zombieimg, zombie.x, zombie.y)

Re: Enemy Spawning

Posted: Sat Nov 24, 2012 4:44 pm
by GijsB
SuperMeijin wrote:I know this thread is old but how do I draw a image on these enemies. For my old single enemy I was doing

Code: Select all

love.graphics.draw(zombieimg, zombie.x, zombie.y)
Add an extra variable to your 'zombie' table, which corresponses to the correct picture of that zombie.
Something like this :

zombie = {
x = 120,
y = 310,
texture = <TEXTURE>
}

love.graphics.draw(zombie.texture, zombie.x, zombie.y)