Skip to content

Instantly share code, notes, and snippets.

@torus
Created July 12, 2009 09:54
Show Gist options
  • Save torus/145589 to your computer and use it in GitHub Desktop.
Save torus/145589 to your computer and use it in GitHub Desktop.
-- load the game engine
dofile "engine.lua"
---
--- Entity with Coroutine
---
CoroutineEntity = create_entity_class ()
function CoroutineEntity:new (name, param1, param2)
-- inherits from EntityBase
local o = EntityBase.new (self, name)
o.seqcoro = coroutine.wrap (o.sequence)
-- additional parameters
o.param1 = param1
o.param2 = param2
return o
end
function CoroutineEntity:update ()
return self:seqcoro ()
end
function CoroutineEntity:sequence ()
print (self.name .. ": initialized")
-- do something...
print (self.name .. ": begin state 1")
for i = 1, self.param1 do
print (self.name .. ": state 1 - " .. i)
coroutine.yield ()
end
-- then, do another thing...
print (self.name .. ": begin state 2")
for i = 1, self.param2 do
print (self.name .. ": state 2 - " .. i)
coroutine.yield ()
end
print (self.name .. ": state 3")
coroutine.yield ()
print (self.name .. ": shutting down...")
return (FINISHED)
end
---
--- Generate entity instances and run
---
local entity1 = CoroutineEntity:new ("coroA <>", 2, 5)
local entity2 = CoroutineEntity:new ("coroB ==", 3, 1)
local entity3 = CoroutineEntity:new ("coroC ||", 1, 1)
local entlist = {entity1, entity2, entity3}
run (entlist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment