Files
OpenRA/mods/common/lua/team.lua
Oliver Brakmann 42532efd8f Replace Lua's for loops with Utils.Do/Team.Do
Besides fitting in better with the OpenRA coding style, this also gets
rid of some weird timing-related errors I have seen when creating teams
with a larger number (6+) of members. (The script would just print
'Error: function' in Team.AddActorEventHandlers. Adding a strategically
placed print statement fixed that. Replacing the original for loop with
Team.Do did as well)
2014-02-02 23:37:22 +01:00

68 lines
1.6 KiB
Lua

Team = { }
Team.New = function(actors)
local team = { }
team.Actors = actors
team.OnAllKilled = { }
team.OnAnyKilled = { }
team.OnAllRemovedFromWorld = { }
team.OnAnyRemovedFromWorld = { }
Team.AddActorEventHandlers(team)
return team
end
Team.AddActorEventHandlers = function(team)
Team.Do(team, function(actor)
Actor.OnKilled(actor, function()
Team.InvokeHandlers(team.OnAnyKilled)
if Team.AllAreDead(team) then Team.InvokeHandlers(team.OnAllKilled) end
end)
Actor.OnRemovedFromWorld(actor, function()
Team.InvokeHandlers(team.OnAnyRemovedFromWorld)
if not Team.AnyAreInWorld(team) then Team.InvokeHandlers(team.OnAllRemovedFromWorld) end
end)
end)
end
Team.InvokeHandlers = function(event)
Utils.Do(event, function(handler) handler() end)
end
Team.AllAreDead = function(team)
return Utils.All(team.Actors, Actor.IsDead)
end
Team.AnyAreDead = function(team)
return Utils.Any(team.Actors, Actor.IsDead)
end
Team.AllAreInWorld = function(team)
return Utils.All(team.Actors, Actor.IsInWorld)
end
Team.AnyAreInWorld = function(team)
return Utils.Any(team.Actors, Actor.IsInWorld)
end
Team.AddEventHandler = function(event, func)
table.insert(event, func)
end
Team.Contains = function(team, actor)
return Utils.Any(team.Actors, function(a) return a == actor end)
end
Team.Do = function(team, func)
Utils.Do(team.Actors, func)
end
Team.Patrol = function(team, waypoints, wait, loop)
Team.Do(team, function(a) Actor.Patrol(a, waypoints, wait, loop) end)
end
Team.PatrolUntil = function(team, waypoints, wait, func)
Team.Do(team, function(a) Actor.PatrolUntil(a, waypoints, wait, func) end)
end