Multiple instances of an enemy.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Lemony Lime
Prole
Posts: 22
Joined: Fri Dec 28, 2012 9:35 pm

Multiple instances of an enemy.

Post by Lemony Lime »

I'm remaking this game as my first real love2d project, and am stuck on how to make multiple(unlimited) copies of the fruit.
http://www.newgrounds.com/portal/view/581553

Classes in Lua... well, the lack of classes in Lua rather, has me completely lost. Are there any tutorials as to how you might do this? I've already got this class for the oranges, using the class file I found in Mari0: https://love2d.org/wiki/Simple_Educative_Class_System (The top one.) I've been trying to use mari0 as a reference for most of what I'm doing, but I just can't find anything relating to this in there, even though there are clearly multiple copies of the enemies. (Granted they all always move in the same pattern, direction, and speed as the other copies, so that probably makes things easier, whereas mine requires them all to move in completely random directions.)

The move and direction functions were just for testing.

Code: Select all

orange = class:new()

function orange:init()
	self.sprite = love.graphics.newImage("graphics/orange.png")
	self.speed = 100
	self.timer = 0
	self.dir = 0
	self.x = 200
	self.y = 200
	
end

function orange:update(dt)
	self:direction(dt)
	self:move(dt)
end

function orange:draw()
	love.graphics.draw(self.sprite, self.x, self.y, 0, 1, 1, ( self.sprite:getWidth() / 2 ), ( self.sprite:getHeight() / 2 ))
end

function orange:direction(dt)
	self.timer = self.timer + dt
	if ( self.timer >= 2	) then
		self.timer = 0
		self.dir = math.random(4) - 1
	end
end

function orange:move(dt)
	if ( self.dir == 0 ) then
		self.y = ( self.y - ( self.speed * dt ) )
		elseif ( self.dir == 1 ) then
			self.x = ( self.x + ( self.speed * dt ) )
			elseif ( self.dir == 2 ) then
				self.y = ( self.y + ( self.speed * dt ) )
				elseif ( self.dir == 3 ) then
					self.x = ( self.x - ( self.speed * dt ) ) 
	end
end
User avatar
Lemony Lime
Prole
Posts: 22
Joined: Fri Dec 28, 2012 9:35 pm

Re: Multiple instances of an enemy.

Post by Lemony Lime »

Looking back at that class link I posted, I notice this, which is an example of exactly what I just asked. More help or tutorials would still be appreciated though. It's far too late for me to actually try messing with this anymore. Thankfully, no school tomorrow, so I can actually do something important. :D lol (Though, I'm actually in college learning C++, not high school, so I probably shouldn't be saying that. haha)

Code: Select all

myclass = class:new()
myclass.value = 13
function myclass:setvalue(v)
    self.value = v
end
object = myclass:new()
object:setvalue(128)
--myclass.value is still 13 and object.value is now 128
anotherObject = object:new()
--anotherObject.value is 128
User avatar
master both
Party member
Posts: 262
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: Multiple instances of an enemy.

Post by master both »

The way i do it is to creat the objects inside a table and then doing the following:

Code: Select all

function love.load()
        fruits = {}
        fruits[1] = orange:new()
        fruits[2] = orange:new()
end

function love.update(dt)
        for i = 1, #fruits do
                   fruits[i]:update(dt)
        end
end

function love.draw()
        for i = 1, #fruits do
                   fruits[i]:draw(dt)
        end
end
EDIT : sorry, i didnt saw your other post :rofl:
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: Multiple instances of an enemy.

Post by scutheotaku »

I would normally go into a lengthy explanation, but unfortunately I'm a bit short on time at the moment...sorry!

Instead, I'll direct you to my basic framework: viewtopic.php?f=5&t=12238
Now, I'm not saying that you should use my framework (depending on your game, it may be way overkill). But I think that it might help give you an idea of how one might use a class system (in my case, the 30log library - an excellent one) to have multiple instances of a class :)

My framework essentially stores each instance in a table on creation. That table is then used to update and draw each instance of the class (well, there's a separate draw table in framework for z depth sorting).
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Multiple instances of an enemy.

Post by Kadoba »

There are lots of different approaches but the idea is the same:

- For each instance create a new table to hold independent values such as position and acceleration
- Make sure the instances have common class functions. This is usually accomplished by metatables and __index.
- Put them into a list and forward callbacks to each object in the list

I created a little demo to show how I approach it.
InstanceDemo.love
(861 Bytes) Downloaded 280 times
And just in case if that's too complicated, here is a much more basic version.
SimpleInstanceDemo.love
(464 Bytes) Downloaded 262 times
Post Reply

Who is online

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