Merge pull request #10959 from Phrohdoh/cargo-scripting

Expose passenger enter/exit notifications to Lua
This commit is contained in:
abcdefg30
2016-03-31 00:53:35 +02:00
4 changed files with 71 additions and 9 deletions

View File

@@ -51,6 +51,20 @@ namespace OpenRA.Mods.Common.Scripting
Context.World.AddFrameEndTask(w => w.Add(new DelayedAction(delay, doCall))); Context.World.AddFrameEndTask(w => w.Add(new DelayedAction(delay, doCall)));
} }
[Desc("Call a function for each passenger when it enters a transport. " +
"The callback function will be called as func(Actor transport, Actor passenger).")]
public void OnPassengerEntered(Actor a, LuaFunction func)
{
GetScriptTriggers(a).RegisterCallback(Trigger.OnPassengerEntered, func, Context);
}
[Desc("Call a function for each passenger when it exits a transport. " +
"The callback function will be called as func(Actor transport, Actor passenger).")]
public void OnPassengerExited(Actor a, LuaFunction func)
{
GetScriptTriggers(a).RegisterCallback(Trigger.OnPassengerExited, func, Context);
}
[Desc("Call a function each tick that the actor is idle. " + [Desc("Call a function each tick that the actor is idle. " +
"The callback function will be called as func(Actor self).")] "The callback function will be called as func(Actor self).")]
public void OnIdle(Actor a, LuaFunction func) public void OnIdle(Actor a, LuaFunction func)

View File

@@ -12,6 +12,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Eluant; using Eluant;
using OpenRA.Mods.Common.Traits;
using OpenRA.Scripting; using OpenRA.Scripting;
using OpenRA.Traits; using OpenRA.Traits;
@@ -21,7 +22,8 @@ namespace OpenRA.Mods.Common.Scripting
{ {
OnIdle, OnDamaged, OnKilled, OnProduction, OnOtherProduction, OnPlayerWon, OnPlayerLost, OnIdle, OnDamaged, OnKilled, OnProduction, OnOtherProduction, OnPlayerWon, OnPlayerLost,
OnObjectiveAdded, OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnInfiltrated, OnObjectiveAdded, OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnInfiltrated,
OnAddedToWorld, OnRemovedFromWorld, OnDiscovered, OnPlayerDiscovered OnAddedToWorld, OnRemovedFromWorld, OnDiscovered, OnPlayerDiscovered,
OnPassengerEntered, OnPassengerExited
} }
[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.")]
@@ -31,7 +33,8 @@ namespace OpenRA.Mods.Common.Scripting
} }
public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, INotifyOtherProduction, public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, INotifyOtherProduction,
INotifyObjectivesUpdated, INotifyCapture, INotifyInfiltrated, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyDiscovered, INotifyActorDisposing INotifyObjectivesUpdated, INotifyCapture, INotifyInfiltrated, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyDiscovered, INotifyActorDisposing,
INotifyPassengerEntered, INotifyPassengerExited
{ {
readonly World world; readonly World world;
readonly Actor self; readonly Actor self;
@@ -421,6 +424,48 @@ namespace OpenRA.Mods.Common.Scripting
} }
} }
void INotifyPassengerEntered.OnPassengerEntered(Actor self, Actor passenger)
{
if (world.Disposing)
return;
foreach (var f in Triggerables(Trigger.OnPassengerEntered))
{
try
{
using (var trans = self.ToLuaValue(f.Context))
using (var pass = passenger.ToLuaValue(f.Context))
f.Function.Call(trans, pass).Dispose();
}
catch (Exception ex)
{
f.Context.FatalError(ex.Message);
return;
}
}
}
void INotifyPassengerExited.OnPassengerExited(Actor self, Actor passenger)
{
if (world.Disposing)
return;
foreach (var f in Triggerables(Trigger.OnPassengerExited))
{
try
{
using (var trans = self.ToLuaValue(f.Context))
using (var pass = passenger.ToLuaValue(f.Context))
f.Function.Call(trans, pass).Dispose();
}
catch (Exception ex)
{
f.Context.FatalError(ex.Message);
return;
}
}
}
public void Clear(Trigger trigger) public void Clear(Trigger trigger)
{ {
world.AddFrameEndTask(w => world.AddFrameEndTask(w =>

View File

@@ -93,7 +93,7 @@ namespace OpenRA.Mods.Common.Traits
getArmaments = () => armaments; getArmaments = () => armaments;
} }
public void PassengerEntered(Actor self, Actor passenger) void INotifyPassengerEntered.OnPassengerEntered(Actor self, Actor passenger)
{ {
paxFacing.Add(passenger, passenger.Trait<IFacing>()); paxFacing.Add(passenger, passenger.Trait<IFacing>());
paxPos.Add(passenger, passenger.Trait<IPositionable>()); paxPos.Add(passenger, passenger.Trait<IPositionable>());
@@ -103,7 +103,7 @@ namespace OpenRA.Mods.Common.Traits
.Where(a => Info.Armaments.Contains(a.Info.Name))); .Where(a => Info.Armaments.Contains(a.Info.Name)));
} }
public void PassengerExited(Actor self, Actor passenger) void INotifyPassengerExited.OnPassengerExited(Actor self, Actor passenger)
{ {
paxFacing.Remove(passenger); paxFacing.Remove(passenger);
paxPos.Remove(passenger); paxPos.Remove(passenger);

View File

@@ -246,7 +246,7 @@ namespace OpenRA.Mods.Common.Traits
SetPassengerFacing(a); SetPassengerFacing(a);
foreach (var npe in self.TraitsImplementing<INotifyPassengerExited>()) foreach (var npe in self.TraitsImplementing<INotifyPassengerExited>())
npe.PassengerExited(self, a); npe.OnPassengerExited(self, a);
var p = a.Trait<Passenger>(); var p = a.Trait<Passenger>();
p.Transport = null; p.Transport = null;
@@ -311,7 +311,7 @@ namespace OpenRA.Mods.Common.Traits
} }
foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>()) foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>())
npe.PassengerEntered(self, a); npe.OnPassengerEntered(self, a);
var p = a.Trait<Passenger>(); var p = a.Trait<Passenger>();
p.Transport = self; p.Transport = self;
@@ -406,7 +406,7 @@ namespace OpenRA.Mods.Common.Traits
c.Trait<Passenger>().Transport = self; c.Trait<Passenger>().Transport = self;
foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>()) foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>())
npe.PassengerEntered(self, c); npe.OnPassengerEntered(self, c);
} }
initialized = true; initialized = true;
@@ -421,8 +421,11 @@ namespace OpenRA.Mods.Common.Traits
} }
} }
public interface INotifyPassengerEntered { void PassengerEntered(Actor self, Actor passenger); } [RequireExplicitImplementation]
public interface INotifyPassengerExited { void PassengerExited(Actor self, Actor passenger); } public interface INotifyPassengerEntered { void OnPassengerEntered(Actor self, Actor passenger); }
[RequireExplicitImplementation]
public interface INotifyPassengerExited { void OnPassengerExited(Actor self, Actor passenger); }
public class RuntimeCargoInit : IActorInit<Actor[]>, ISuppressInitExport public class RuntimeCargoInit : IActorInit<Actor[]>, ISuppressInitExport
{ {