I have a class that spawns enemies and depending of the enemy type they have different behavior. I want to store this behavior in class functions, like enemy:walking(), enemy:attacking() etc. Now the kicker is that I don't want all enemies to run all functions with every update, I also want to be able to dynamically add function to an enemy with player actions. The way I'd like to do it is to store all names of the functions in the table and then depending of the enemy type, select specific functions to run. Right now I have unelegant if, elseif statements that run through all enemy instances
Code: Select all
function enemy:update()
if self.typ == 1 then
self:Throwable(self.typ)
self:Bounce()
self:Gravity()
elseif self.typ == 2 then
self:BounceAndRicochet()
self:Gravity()
elseif self.typ == 3 then
self:Chase()
self:Gravity()
elseif self.typ == 4 then
self:NoGravity()
end
end
Code: Select all
functionTable = {
{self:Throwable(self.typ)}, {self:Bounce()},{self:Gravity()},
{self:BounceAndRicochet(),self:Gravity()},
{self:Chase(), self:Gravity()},
{self:NoGravity()}
}
function enemy:update()
for i = 1, #functionTable do
functionTable[self.typ][i]
end
end