drawing 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.
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

drawing multiple enemies[SOLVED]

Post by baconhawka7x »

Yay! i'm getting pretty far in my game thanks to the forums. But i noticed if i try to spawn more than one zombie in a map that they are all really just duplicates of the original zombie. so if i hit one, they all react. I made a table for my zombie with all of the zombie's properties in it, it looks something like this...

Code: Select all

--Zombeh Properties!:D
	zombie = {}
	zombie.x = 300
	zombie.y = 500
	zombie.speed = 100
	zombie.health = 10
	zombie.pic = love.graphics.newImage("pics/zombie.png")
	zombie.reach = 55
	zombie.damage = 2
How should I work this out so that there can be plenty of zombies in one room?
Thanks in advanced:D
Last edited by baconhawka7x on Fri Dec 23, 2011 8:14 am, edited 1 time in total.
SPlice
Prole
Posts: 48
Joined: Thu Jul 28, 2011 8:53 am

Re: drawing multiple enemies.

Post by SPlice »

try adding them to a table and use ipairs like so

Code: Select all

  
zombies = {}
 zombie = {
   x = 300,
   y = 500,
   speed = 100,
  color = "255, 255, 255",
   health = 10,
   pic = love.graphics.newImage("pics/zombie.png"),
   reach = 55,
   damage = 2}
--now add the zombie
table.insert(zombies, zombie)

--now do what you need to
for i,v in ipairs(zombies) do
--put stuff in here to modify them like
 if v.health < 0 then
table.remove(zombies, i)
end

User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: drawing multiple enemies.

Post by Jasoco »

I recommend using an object library or method. Here's how I do it:

Code: Select all

zombieType = {}

function zombieType:create(v)
  local instance = {}
  setmetatable(instance, {__index = self})
  instance:reset(v)
  return instance
end
function zombieType:reset(v)
  v = v or {}
  self.x = v.x or 300
  self.y = v.y or 500
  self.speed = v.speed or 100
  self.color = v.color or { 255, 255, 255 }
  self.health = v.health or 10
  self.pic = v.img or zombImg
  self.reach = v.reach or 55
  self.damage = v.damage or 2
end
function zombieType:update(dt)
  --ANY UPDATING LIKE MOVEMENT AND ATTACKING AND ANIMATION GOES HERE
end
function zombieType:draw()
  --DRAWING CODE GOES HERE
end
Note how it says self.something = v.value or something that means you can create the Zombie using your own parameters, or have it use a default value. The default is the part after or. The v = v or {} part is an error catcher that makes a table if it doesn't exist so you don't end up with blue screens when making a generic Zombie.

To create the Zombie type you do something like:

Code: Select all

zombie[id] = zombieType:create({ x = ??, y = ??, etc... })
If you leave the {} empty, you end up with a default zombie. So you'll want to pass them values.

Then in your normal game update and draw functions you would do something like:

Code: Select all

for i, z in pairs(zombie) do
  z:update(dt)
end

for i, z in pairs(zombie) do
  z:draw()
end
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: drawing multiple enemies.

Post by baconhawka7x »

Ok I'm so lost. That all looks very foreign to me. I don't even know where to start with questions.
Like..

Code: Select all

for i, z in pairs(zombie) do
  z:update(dt)
end
wheres the z coming from? and the I? and what does "inpairs" mean, and why is zombie in parenthasis?D:
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: drawing multiple enemies.

Post by baconhawka7x »

SPlice wrote:try adding them to a table and use ipairs like so

Code: Select all

  
zombies = {}
 zombie = {
   x = 300,
   y = 500,
   speed = 100,
  color = "255, 255, 255",
   health = 10,
   pic = love.graphics.newImage("pics/zombie.png"),
   reach = 55,
   damage = 2}
--now add the zombie
table.insert(zombies, zombie)

--now do what you need to
for i,v in ipairs(zombies) do
--put stuff in here to modify them like
 if v.health < 0 then
table.remove(zombies, i)
end

So could I still draw more then one zombie though?
SPlice
Prole
Posts: 48
Joined: Thu Jul 28, 2011 8:53 am

Re: drawing multiple enemies.

Post by SPlice »

yeah sure you could make a function

Code: Select all

function draw_zombies()
for i,v in ipairs(zombies)
love.graphics.draw(v.image, v.x, v.y)
end
end

than you would just put draw_zombies() in your love.draw and it will go through and draw a zombie from the picture and at the x and y pos, I sent you a PM

see it puts a zombie in the zombies table and than when you do ipairs on it it goes through each different zombie in the table and you can use v.whatever to get at each bit of the tablet. you can run through and draw it or move each one over or what have you
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: drawing multiple enemies.

Post by baconhawka7x »

So Should I do the same with steve?(My Player?)
SPlice
Prole
Posts: 48
Joined: Thu Jul 28, 2011 8:53 am

Re: drawing multiple enemies.

Post by SPlice »

you could but its prob not needed, if you had MSN or Skype I could explain this a lot better and send you some code examples. The ipairs is useful for stuff that your going to have a lot of like zombies or bullets. For things like the player it prob wont be needed unless your game is multi player. you could make a player table and use it with ipairs such as

Code: Select all

 player = { life = 100, x = 0, y = 0} 
for i,v in ipairs(zombies) do
if v.x < player.x + 10 and v.x > player.x - 10 and  v.y < player.y + 10 and v.y > player.y - 10 then
player.life = player.life -10
end
end

so the player is damaged when the zombie is close to the player
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: drawing multiple enemies.

Post by bartbes »

Stop double posting.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: drawing multiple enemies.

Post by coffee »

baconhawka7x wrote:So Should I do the same with steve?(My Player?)
Like SPlice said you could deal with "steve"(your player) as multiple entities like your "zombehs" but problably is not needed. Depends of what what kind of your game is and the features of it. However if you pretend and want in the future have sidequest-companions, friendly summons, another second or more players perhaps you should consider right way use multiples "stevies" and use player[1] as default player in "solo" and when needed the engine is already prepare to deal with a player[2] and so on...
Post Reply

Who is online

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