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.
This commit is contained in:
Oliver Brakmann
2014-07-01 22:33:46 +02:00
parent 1c8a56d197
commit d23707b5f7
2 changed files with 60 additions and 2 deletions

View File

@@ -144,6 +144,27 @@ namespace OpenRA.Mods.RA.Scripting
GetScriptTriggers(player.PlayerActor).RegisterCallback(Trigger.OnObjectiveFailed, func, context); 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")] [Desc("Removes all triggers from this actor")]
public void ClearAll(Actor a) public void ClearAll(Actor a)
{ {

View File

@@ -18,12 +18,13 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Scripting 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.")] [Desc("Allows map scripts to attach triggers to this actor via the Triggers global.")]
public class ScriptTriggersInfo : TraitInfo<ScriptTriggers> { } public class ScriptTriggersInfo : TraitInfo<ScriptTriggers> { }
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<Actor> OnKilledInternal = _ => {}; public event Action<Actor> 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) public void Clear(Trigger trigger)
{ {
Triggers[trigger].Clear(); Triggers[trigger].Clear();