[ create a new paste ] login | about

Link: http://codepad.org/XM4QwNYP    [ raw code | fork ]

Lua, pasted on Nov 13:
function love.load()
	spawnEnemy(20,20,100)
	spawnEnemy(100,100,100)
end

function love.update(dt)

end

function love.draw()
	drawEnemies()
end

enemies = {}
function spawnEnemy(x,y,hp)
	table.insert(enemies, {x=x, y=y, hp=hp})
end

function drawEnemies()
	for i=1, #enemies do
		--[[ Normally here you'd draw your enemy sprite,
		     but I'm going to use a rectangle for demo. ]]--
			 
		love.graphics.rectangle('fill', enemies[i].x,
		                        enemies[i].y, 50, 50)
								
		--[[ Note that we're never talking specifically
		     about a particular enemy, but that we're all
			 going to treat them the same. What makes this work
			 and makes them go to their own locations is
			 'enemies[i].x' and 'enemies[i].y'. Think of a table
			 as a list of objects. 
			 
			 Tables are defined like this:
			 
			 t = {41,21,33,40}
			 
			 Here we just have a list of numbers. In Lua, and most
             programming languages you can access a particular
			 item in this list by using '[i]' where 'i' is the
			 particular object in that list you want to access.
			 
			 So to print the number 21 we could do this, using
			 our list aboev:
			 
				print(t[2])
				
			As a small note, in most languages the numbering starts
			with zero, but in Lua it starts with 1. 
			
			So back to our enemies, instead of a list of numbers,
			let's have a list of enemies. In fact, each enemy is just
			it's own list. There's another property to tables you might not
			know about. You don't have to refer to each item in a table by
			a number. You can, instead, use 'keys.' These are strings that
			refer to a specific item in the table. 
			
			Here's another demonstration table:
			
				fruit_colors = {apple = "red",
								plum = "purple",
								lemon = "yellow"}
								
			Each object in the fruit_colors table can be 
			accessed using a key. Now we could do something like this:
			
				print("The color of an apple is " .. fruit_colors[apple])
			
			Again, once more, back to our enemies list.
			
				enemies = {}
			
			This is an empy list, but our spawnEnemies function is going
			to populate it using Lua's built-in table.insert() function.
			
			We're going to place enemies, which are their own tables with
			keys relating to their specific attributes, into our enemies table,
			which is one giant collective table.
			
				function spawnEnemy(x,y,hp)
					table.insert(enemies, {x=x, y=y, hp=h})
				end
				
			Let's say we now call spawnEnemies a few time and create
			a few baddies:
			
					spawnEnemy(20,30,100)
					spawnEnemy(100,40,100)
			
			After these two calls our enemies table will look like this internally.
			
				enemies = { [1] = { x = 20, y = 30, hp = 100 },
							[2] = { x = 100, y = 40, hp = 100 } }
						
			With this knowledge in mind, we can then draw every enemy to
			the screen by 'iterating' over the 'enemies' table. That is, using
			our knowledge of how many enemies have been spawned, and our knowledge
			of how to access a specific enemy, we can do this:
			
				for i=1, #enemies do
					love.graphics.rectangle('fill', enemies[i].x, enemies[i].y, 50, 50)
				end
				
			This code will create a 50x50 rectangle at every enemies location. In your
			code of course you might want to instead draw a sprite. If you want different
			enemies to be represented by different sprites, you might store a sprite key within
			the enemy table that lives within our enemies master table.
			
			I know this is all a little confusing, but I hope this helps even a little. ]]--
	end
end


Create a new paste based on this one


Comments: