From d23707b5f7c560f56d8a663df1c45c1519594fb8 Mon Sep 17 00:00:00 2001 From: Oliver Brakmann Date: Tue, 1 Jul 2014 22:33:46 +0200 Subject: [PATCH] Add OnCapture, OnAddedToWorld and OnRemovedFromWorld triggers to Lua API This brings the new Lua API up to feature-parity with the old API in the trigger department. --- .../Scripting/Global/TriggerGlobal.cs | 21 ++++++++++ OpenRA.Mods.RA/Scripting/ScriptTriggers.cs | 41 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/OpenRA.Mods.RA/Scripting/Global/TriggerGlobal.cs b/OpenRA.Mods.RA/Scripting/Global/TriggerGlobal.cs index a2dca58344..4fd96055d1 100644 --- a/OpenRA.Mods.RA/Scripting/Global/TriggerGlobal.cs +++ b/OpenRA.Mods.RA/Scripting/Global/TriggerGlobal.cs @@ -144,6 +144,27 @@ namespace OpenRA.Mods.RA.Scripting GetScriptTriggers(player.PlayerActor).RegisterCallback(Trigger.OnObjectiveFailed, func, context); } + [Desc("Call a function when this actor is added to the world. " + + "The callback function will be called as func(Actor self).")] + public void OnAddedToWorld(Actor a, LuaFunction func) + { + GetScriptTriggers(a).RegisterCallback(Trigger.OnAddedToWorld, func, context); + } + + [Desc("Call a function when this actor is removed from the world. " + + "The callback function will be called as func(Actor self).")] + public void OnRemovedFromWorld(Actor a, LuaFunction func) + { + GetScriptTriggers(a).RegisterCallback(Trigger.OnRemovedFromWorld, func, context); + } + + [Desc("Call a function when this actor is captured. The callback function " + + "will be called as func(Actor self, Actor captor, Player oldOwner, Player newOwner).")] + public void OnCapture(Actor a, LuaFunction func) + { + GetScriptTriggers(a).RegisterCallback(Trigger.OnCapture, func, context); + } + [Desc("Removes all triggers from this actor")] public void ClearAll(Actor a) { diff --git a/OpenRA.Mods.RA/Scripting/ScriptTriggers.cs b/OpenRA.Mods.RA/Scripting/ScriptTriggers.cs index 9c2a8e597b..7ff196ab4b 100644 --- a/OpenRA.Mods.RA/Scripting/ScriptTriggers.cs +++ b/OpenRA.Mods.RA/Scripting/ScriptTriggers.cs @@ -18,12 +18,13 @@ using OpenRA.Traits; namespace OpenRA.Mods.RA.Scripting { - public enum Trigger { OnIdle, OnDamaged, OnKilled, OnProduction, OnPlayerWon, OnPlayerLost, OnObjectiveAdded, OnObjectiveCompleted, OnObjectiveFailed }; + public enum Trigger { OnIdle, OnDamaged, OnKilled, OnProduction, OnPlayerWon, OnPlayerLost, OnObjectiveAdded, + OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnAddedToWorld, OnRemovedFromWorld }; [Desc("Allows map scripts to attach triggers to this actor via the Triggers global.")] public class ScriptTriggersInfo : TraitInfo { } - public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, INotifyObjectivesUpdated, IDisposable + public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, INotifyObjectivesUpdated, INotifyCapture, INotifyAddedToWorld, INotifyRemovedFromWorld, IDisposable { public event Action OnKilledInternal = _ => {}; @@ -146,6 +147,42 @@ namespace OpenRA.Mods.RA.Scripting } } + public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner) + { + foreach (var f in Triggers[Trigger.OnCapture]) + { + var a = self.ToLuaValue(f.Second); + var b = captor.ToLuaValue(f.Second); + var c = oldOwner.ToLuaValue(f.Second); + var d = newOwner.ToLuaValue(f.Second); + f.First.Call(a, b, c, d).Dispose(); + a.Dispose(); + b.Dispose(); + c.Dispose(); + d.Dispose(); + } + } + + public void AddedToWorld(Actor self) + { + foreach (var f in Triggers[Trigger.OnAddedToWorld]) + { + var a = self.ToLuaValue(f.Second); + f.First.Call(a).Dispose(); + a.Dispose(); + } + } + + public void RemovedFromWorld(Actor self) + { + foreach (var f in Triggers[Trigger.OnRemovedFromWorld]) + { + var a = self.ToLuaValue(f.Second); + f.First.Call(a).Dispose(); + a.Dispose(); + } + } + public void Clear(Trigger trigger) { Triggers[trigger].Clear();