Added OnKilledOrCaptured and OnAllKilledOrCaptured

This commit is contained in:
abcdefg30
2014-11-21 17:01:42 +01:00
parent e29adf5f4f
commit 33eeee4680
3 changed files with 69 additions and 6 deletions

View File

@@ -220,6 +220,68 @@ namespace OpenRA.Mods.RA.Scripting
GetScriptTriggers(a).RegisterCallback(Trigger.OnCapture, func, context); GetScriptTriggers(a).RegisterCallback(Trigger.OnCapture, func, context);
} }
[Desc("Call a function when this actor is killed or captured. " +
"The callback function will be called as func().")]
public void OnKilledOrCaptured(Actor a, LuaFunction func)
{
var called = false;
var copy = (LuaFunction)func.CopyReference();
Action<Actor> OnKilledOrCaptured = m =>
{
try
{
if (called)
return;
copy.Call().Dispose();
copy.Dispose();
called = true;
}
catch (Exception e)
{
context.FatalError(e.Message);
}
};
GetScriptTriggers(a).OnCapturedInternal += OnKilledOrCaptured;
GetScriptTriggers(a).OnKilledInternal += OnKilledOrCaptured;
}
[Desc("Call a function when all of the actors in a group have been killed or captured. " +
"The callback function will be called as func().")]
public void OnAllKilledOrCaptured(Actor[] actors, LuaFunction func)
{
var group = actors.ToList();
var copy = (LuaFunction)func.CopyReference();
Action<Actor> OnMemberKilledOrCaptured = m =>
{
try
{
if (!group.Contains(m))
return;
group.Remove(m);
if (!group.Any())
{
copy.Call().Dispose();
copy.Dispose();
}
}
catch (Exception e)
{
context.FatalError(e.Message);
}
};
foreach (var a in group)
{
GetScriptTriggers(a).OnCapturedInternal += OnMemberKilledOrCaptured;
GetScriptTriggers(a).OnKilledInternal += OnMemberKilledOrCaptured;
}
}
[Desc("Call a function when a ground-based actor enters this cell footprint." + [Desc("Call a function when a ground-based actor enters this cell footprint." +
"Returns the trigger id for later removal using RemoveFootprintTrigger(int id)." + "Returns the trigger id for later removal using RemoveFootprintTrigger(int id)." +
"The callback function will be called as func(Actor a, int id).")] "The callback function will be called as func(Actor a, int id).")]

View File

@@ -32,6 +32,7 @@ namespace OpenRA.Mods.RA.Scripting
readonly World world; readonly World world;
public event Action<Actor> OnKilledInternal = _ => { }; public event Action<Actor> OnKilledInternal = _ => { };
public event Action<Actor> OnCapturedInternal = _ => { };
public event Action<Actor> OnRemovedInternal = _ => { }; public event Action<Actor> OnRemovedInternal = _ => { };
public event Action<Actor, Actor> OnProducedInternal = (a, b) => { }; public event Action<Actor, Actor> OnProducedInternal = (a, b) => { };
public event Action<Actor, Actor> OnOtherProducedInternal = (a, b) => { }; public event Action<Actor, Actor> OnOtherProducedInternal = (a, b) => { };
@@ -236,6 +237,9 @@ namespace OpenRA.Mods.RA.Scripting
return; return;
} }
} }
// Run any internally bound callbacks
OnCapturedInternal(self);
} }
public void Infiltrated(Actor self, Actor infiltrator) public void Infiltrated(Actor self, Actor infiltrator)

View File

@@ -63,7 +63,7 @@ SovietRallyPoints = { SovietRallyPoint1, SovietRallyPoint2, SovietRallyPoint3, S
SovietGateRallyPoints = { AlliesBaseGate2, AlliesBaseGate2, AlliesBaseGate1, AlliesBaseGate1, AlliesBaseGate1 } SovietGateRallyPoints = { AlliesBaseGate2, AlliesBaseGate2, AlliesBaseGate1, AlliesBaseGate1, AlliesBaseGate1 }
Airfields = { SovietAirfield1, SovietAirfield2, SovietAirfield3 } Airfields = { SovietAirfield1, SovietAirfield2, SovietAirfield3 }
SovietBuildings = { Barrack1, SubPen, RadarDome, AdvancedPowerPlant1, AdvancedPowerPlant2, AdvancedPowerPlant3, WarFactory, Refinery, Silo1, Silo2, FlameTower1, FlameTower2, FlameTower3, Sam3, Sam4 } SovietBuildings = { Barrack1, SubPen, RadarDome, AdvancedPowerPlant1, AdvancedPowerPlant2, AdvancedPowerPlant3, WarFactory, Refinery, Silo1, Silo2, FlameTower1, FlameTower2, FlameTower3, Sam1, Sam2, Sam3, Sam4, SovietAirfield1, SovietAirfield2, SovietAirfield3 }
IdleTrigger = function(units, dest) IdleTrigger = function(units, dest)
Utils.Do(units, function(unit) Utils.Do(units, function(unit)
@@ -321,7 +321,7 @@ InitMission = function()
end) end)
end) end)
Trigger.OnAllKilled(SovietBuildings, function() Trigger.OnAllKilledOrCaptured(SovietBuildings, function()
if DestroyObj then if DestroyObj then
if not soviets.HasNoRequiredUnits() then if not soviets.HasNoRequiredUnits() then
KillObj = allies.AddPrimaryObjective("Kill all remaining soviet forces.") KillObj = allies.AddPrimaryObjective("Kill all remaining soviet forces.")
@@ -339,10 +339,7 @@ end
SetupSoviets = function() SetupSoviets = function()
Barrack1.IsPrimaryBuilding = true Barrack1.IsPrimaryBuilding = true
Barrack1.RallyPoint = SovietInfantryRally1.Location Barrack1.RallyPoint = SovietInfantryRally1.Location
Trigger.OnKilled(Barrack1, function() Trigger.OnKilledOrCaptured(Barrack1, function()
SpawningInfantry = false
end)
Trigger.OnCapture(Barrack1, function()
SpawningInfantry = false SpawningInfantry = false
end) end)