Created
July 12, 2009 09:54
-
-
Save torus/145589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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