Introduce OnDiscovered and OnPlayerDiscovered Lua triggers

This commit is contained in:
penev92
2015-02-22 22:36:05 +02:00
parent 79c53e3981
commit 16dee554c4
2 changed files with 51 additions and 2 deletions

View File

@@ -406,6 +406,20 @@ namespace OpenRA.Mods.Common.Scripting
GetScriptTriggers(a).RegisterCallback(Trigger.OnInfiltrated, func, Context);
}
[Desc("Call a function when this actor is discovered by an enemy or a player with a Neutral stance. " +
"The callback function will be called as func(Actor discovered, Player discoverer)")]
public void OnDiscovered(Actor a, LuaFunction func)
{
GetScriptTriggers(a).RegisterCallback(Trigger.OnDiscovered, func, Context);
}
[Desc("Call a function when this player is discovered by an enemy or neutral player. " +
"The callback function will be called as func(Player discovered, Player discoverer, Actor discoveredActor)")]
public void OnPlayerDiscovered(Player discovered, LuaFunction func)
{
GetScriptTriggers(discovered.PlayerActor).RegisterCallback(Trigger.OnPlayerDiscovered, func, Context);
}
[Desc("Removes all triggers from this actor." +
"Note that the removal will only take effect at the end of a tick, " +
"so you must not add new triggers at the same time that you are calling this function.")]

View File

@@ -21,7 +21,8 @@ namespace OpenRA.Mods.Common.Scripting
public enum Trigger
{
OnIdle, OnDamaged, OnKilled, OnProduction, OnOtherProduction, OnPlayerWon, OnPlayerLost,
OnObjectiveAdded, OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnInfiltrated, OnAddedToWorld, OnRemovedFromWorld
OnObjectiveAdded, OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnInfiltrated,
OnAddedToWorld, OnRemovedFromWorld, OnDiscovered, OnPlayerDiscovered
}
[Desc("Allows map scripts to attach triggers to this actor via the Triggers global.")]
@@ -31,7 +32,7 @@ namespace OpenRA.Mods.Common.Scripting
}
public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, INotifyOtherProduction,
INotifyObjectivesUpdated, INotifyCapture, INotifyInfiltrated, INotifyAddedToWorld, INotifyRemovedFromWorld, IDisposable
INotifyObjectivesUpdated, INotifyCapture, INotifyInfiltrated, INotifyAddedToWorld, INotifyRemovedFromWorld, IDisposable, INotifyDiscovered
{
readonly World world;
@@ -324,6 +325,40 @@ namespace OpenRA.Mods.Common.Scripting
OnOtherProducedInternal(producee, produced);
}
public void OnDiscovered(Actor self, Player discoverer, bool playNotification)
{
foreach (var f in Triggers[Trigger.OnDiscovered])
{
try
{
using (var a = self.ToLuaValue(f.Second))
using (var b = discoverer.ToLuaValue(f.Second))
f.First.Call(a, b).Dispose();
}
catch (Exception ex)
{
f.Second.FatalError(ex.Message);
return;
}
}
foreach (var f in Triggers[Trigger.OnPlayerDiscovered])
{
try
{
using (var a = self.Owner.ToLuaValue(f.Second))
using (var b = discoverer.ToLuaValue(f.Second))
using (var c = self.ToLuaValue(f.Second))
f.First.Call(a, b, c).Dispose();
}
catch (Exception ex)
{
f.Second.FatalError(ex.Message);
return;
}
}
}
public void Clear(Trigger trigger)
{
world.AddFrameEndTask(w =>