Enemy Spawning

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.
SuperMeijin
Prole
Posts: 15
Joined: Wed Oct 03, 2012 3:37 pm

Enemy Spawning

Post 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
User avatar
Patalo
Prole
Posts: 41
Joined: Sun Apr 29, 2012 1:15 pm
Contact:

Re: Enemy Spawning

Post 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!
Current project : 3D fractal explorer DEFract ( viewtopic.php?t=9193)
Github : https://github.com/plule/ | Twitter : http://twitter.com/plule_
SuperMeijin
Prole
Posts: 15
Joined: Wed Oct 03, 2012 3:37 pm

Re: Enemy Spawning

Post by SuperMeijin »

Sounds good but any help on the spawnTonsOfEnnemies() :3
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: Enemy Spawning

Post 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
Your screen is very zoomed in...
User avatar
Larsii30
Party member
Posts: 267
Joined: Sun Sep 11, 2011 9:36 am
Location: Germany

Re: Enemy Spawning

Post 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.
SuperMeijin
Prole
Posts: 15
Joined: Wed Oct 03, 2012 3:37 pm

Re: Enemy Spawning

Post 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?
User avatar
Larsii30
Party member
Posts: 267
Joined: Sun Sep 11, 2011 9:36 am
Location: Germany

Re: Enemy Spawning

Post 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

Alban
Prole
Posts: 1
Joined: Wed Nov 21, 2012 5:42 am
Contact:

Re: Enemy Spawning

Post 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.
SuperMeijin
Prole
Posts: 15
Joined: Wed Oct 03, 2012 3:37 pm

Re: Enemy Spawning

Post 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)
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Enemy Spawning

Post 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)
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests