There are lots of ways to do this, here's the method I use.
First create an enemy table that will store all your enemy entities ( I'm gonna do all of this in a seperate file, let's call it enemy.lua):
Code: Select all
--enemy.lua
local enemy = {}
local enemyClass = {}
enemyClass.__index = function(t,i) return enemyClass[i] end --I don't know if this is necessary but I always write it
The enemy table will hold all enemy instances and the enemyClass table will hold all the enemy related functions.
Now lets code a function to create enemies:
Code: Select all
--enemy.lua
local enemy = {}
local enemyClass = {}
enemyClass.__index = function(t,i) return enemyClass[i] end --I don't know if this is necessary but I always write it
local newEnemy = function(x,y)
local enmy = {} --temporary table to hold all the play atributes
enmy.x = x
enmy.y = y
enmy.width = 10
enmy.height = 10
enmy.xvel = 0
enmy.yvel = 0
enmy.friction = 1
enmy.speed = 500
setmetatable(enmy, enemyClass) --this will link all the enemyClass functions to the newly created enemy.
table.insert(enemy, enmy) --this will store the newly created enemy in the enemy table.
return enmy
end
return newEnemy
To create an enemy all you need to do is to run newEnemy, like so:
Code: Select all
--main.lua
newEnemy = require('enemy.lua')
function love.load()
badGuy = newEnemy(100,100)
end
There you have it but we still need to draw it, to do this simply add the drawing function to the enemyClass:
Code: Select all
--enemy.lua
local enemy = {}
local enemyClass = {}
enemyClass.__index = function(t,i) return enemyClass[i] end --I don't know if this is necessary but I always write it
function enemyClass:draw()
love.graphics.setColor(255,0,0)
love.graphics.rectangle("fill",self.x,self.y,self.width,self.height)
end
local newEnemy = function(x,y)
local enmy = {} --temporary table to hold all the play atributes
enmy.x = x
enmy.y = y
enmy.width = 10
enmy.height = 10
enmy.xvel = 0
enmy.yvel = 0
enmy.friction = 1
enmy.speed = 500
setmetatable(enmy, enemyClass) --this will link all the enemyClass functions to the newly created enemy.
table.insert(enemy, enmy) --this will store the newly created enemy in the enemy table.
return enmy
end
return newEnemy
Notice the ':' instead of '.' in the draw function between enemyClass and draw ?
That allows us to use self inside the function, it gives us more flexibility.
To actualy draw the enemy all you need to do is this:
Code: Select all
--main.lua
newEnemy = require('enemy.lua')
function love.load()
badGuy = newEnemy(100,100)
end
function love.draw()
badGuy:draw()
end
Notice the ':' again, that is necessay, or else self will not work.
Now add all your other functions to the enemyClass table, don't forget to use ':' instead of '.' between enemyClass and the name of your function, always use self inside the function too. self will store the enemy entity that is currently being called.
But imagine that you have a load of enemies and you want do draw them all with a single function. Here's how you do it:
Code: Select all
--enemy.lua
local enemyTBL = {} --this will store all the functions that operate on all the enemy entities
local enemy = {}
local enemyClass = {}
enemyClass.__index = function(t,i) return enemyClass[i] end --I don't know if this is necessary but I always write it
function enemyClass:draw()
love.graphics.setColor(255,0,0)
love.graphics.rectangle("fill",self.x,self.y,self.width,self.height)
end
enemyTBL.newEnemy = function(x,y) -- now the newEnemy function will be stored inside the enemyTBL table
local enmy = {} --temporary table to hold all the play atributes
enmy.x = x
enmy.y = y
enmy.width = 10
enmy.height = 10
enmy.xvel = 0
enmy.yvel = 0
enmy.friction = 1
enmy.speed = 500
setmetatable(enmy, enemyClass) --this will link all the enemyClass functions to the newly created enemy.
table.insert(enemy, enmy) --this will store the newly created enemy in the enemy table.
return enmy
end
-- enemyTBL functions, these will operate on all the enemy entites with a single call
function enemyTBL.draw()
for i,v in ipairs(enemy) do
v:draw()
end
end
--return enemyTBL instead of the old newEnemy
return enemyTBL
The enemyTBL will store all the functions that operate on all the enemies at once.
Notice that now I'm using '.' again instead of ':' on all enemyTBL functions, thats because enemyTBL is not a class, it's a simple table.
Now in main.lua:
Code: Select all
--main.lua
enemy= require('enemy.lua')
function love.load()
for i=1,100 do
enemy.newEnemy(100 + 11*(i-1),100) --this will create a row of enemies form the point (100,100) to (9990,100), creating a total of 100 enemies
end
end
function love.draw()
enemy.draw() --this function will draw all the 100 enemies, yey
end
That's all, if you don't understand something then feel free to ask
TL;DR:
Read everything, this will improve your programming skills