Add scripting support for TimeLimitManager

This commit is contained in:
Oliver Brakmann
2019-01-26 19:28:52 +01:00
committed by abcdefg30
parent 47d88983fb
commit 26d712728a
4 changed files with 101 additions and 5 deletions

View File

@@ -10,6 +10,8 @@
#endregion
using System;
using Eluant;
using OpenRA.Mods.Common.Traits;
using OpenRA.Scripting;
namespace OpenRA.Mods.Common.Scripting
@@ -17,8 +19,13 @@ namespace OpenRA.Mods.Common.Scripting
[ScriptGlobal("DateTime")]
public class DateGlobal : ScriptGlobal
{
readonly TimeLimitManager tlm;
public DateGlobal(ScriptContext context)
: base(context) { }
: base(context)
{
tlm = context.World.WorldActor.TraitOrDefault<TimeLimitManager>();
}
[Desc("True on the 31st of October.")]
public bool IsHalloween
@@ -43,5 +50,39 @@ namespace OpenRA.Mods.Common.Scripting
{
return Seconds(minutes * 60);
}
[Desc("Return or set the time limit (in ticks). When setting, the time limit will count from now. Setting the time limit to 0 will disable it.")]
public int TimeLimit
{
get
{
return tlm != null ? tlm.TimeLimit : 0;
}
set
{
if (tlm != null)
tlm.TimeLimit = value == 0 ? 0 : value + GameTime;
else
throw new LuaException("Cannot set TimeLimit, TimeLimitManager trait is missing.");
}
}
[Desc("The notification string used for custom time limit warnings. See the TimeLimitManager trait documentation for details.")]
public string TimeLimitNotification
{
get
{
return tlm != null ? tlm.Notification : null;
}
set
{
if (tlm != null)
tlm.Notification = value;
else
throw new LuaException("Cannot set TimeLimitNotification, TimeLimitManager trait is missing.");
}
}
}
}

View File

@@ -470,6 +470,12 @@ namespace OpenRA.Mods.Common.Scripting
GetScriptTriggers(a).RegisterCallback(Trigger.OnSold, func, Context);
}
[Desc("Call a function when the game timer expires. The callback function will be called as func().")]
public void OnTimerExpired(LuaFunction func)
{
GetScriptTriggers(Context.World.WorldActor).RegisterCallback(Trigger.OnTimerExpired, 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

@@ -24,7 +24,7 @@ namespace OpenRA.Mods.Common.Scripting
OnIdle, OnDamaged, OnKilled, OnProduction, OnOtherProduction, OnPlayerWon, OnPlayerLost,
OnObjectiveAdded, OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnInfiltrated,
OnAddedToWorld, OnRemovedFromWorld, OnDiscovered, OnPlayerDiscovered,
OnPassengerEntered, OnPassengerExited, OnSold
OnPassengerEntered, OnPassengerExited, OnSold, OnTimerExpired
}
[Desc("Allows map scripts to attach triggers to this actor via the Triggers global.")]
@@ -35,7 +35,7 @@ namespace OpenRA.Mods.Common.Scripting
public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, INotifyOtherProduction,
INotifyObjectivesUpdated, INotifyCapture, INotifyInfiltrated, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyDiscovered, INotifyActorDisposing,
INotifyPassengerEntered, INotifyPassengerExited, INotifySold, INotifyWinStateChanged
INotifyPassengerEntered, INotifyPassengerExited, INotifySold, INotifyWinStateChanged, INotifyTimeLimit
{
readonly World world;
readonly Actor self;
@@ -493,6 +493,25 @@ namespace OpenRA.Mods.Common.Scripting
}
}
void INotifyTimeLimit.NotifyTimerExpired(Actor self)
{
if (world.Disposing)
return;
foreach (var f in Triggerables(Trigger.OnTimerExpired))
{
try
{
f.Function.Call().Dispose();
}
catch (Exception ex)
{
f.Context.FatalError(ex.Message);
return;
}
}
}
public void Clear(Trigger trigger)
{
world.AddFrameEndTask(w =>