Add OnBuildingPlaced lua trigger.
This commit is contained in:
committed by
Gustas Kažukauskas
parent
eb550bbbbb
commit
3177de7b2c
@@ -231,6 +231,16 @@ namespace OpenRA.Mods.Common.Scripting
|
|||||||
GetScriptTriggers(player.PlayerActor).RegisterCallback(Trigger.OnObjectiveFailed, func, Context);
|
GetScriptTriggers(player.PlayerActor).RegisterCallback(Trigger.OnObjectiveFailed, func, Context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Desc("Call a function when this player places a building. " +
|
||||||
|
"The callback function will be called as func(p: player, placed: actor).")]
|
||||||
|
public void OnBuildingPlaced(Player player, [ScriptEmmyTypeOverride("fun(p: player, placed: actor)")] LuaFunction func)
|
||||||
|
{
|
||||||
|
if (player == null)
|
||||||
|
throw new NullReferenceException(nameof(player));
|
||||||
|
|
||||||
|
GetScriptTriggers(player.PlayerActor).RegisterCallback(Trigger.OnBuildingPlaced, func, Context);
|
||||||
|
}
|
||||||
|
|
||||||
[Desc("Call a function when this actor is added to the world. " +
|
[Desc("Call a function when this actor is added to the world. " +
|
||||||
"The callback function will be called as func(self: actor).")]
|
"The callback function will be called as func(self: actor).")]
|
||||||
public void OnAddedToWorld(Actor actor, [ScriptEmmyTypeOverride("fun(self: actor)")] LuaFunction func)
|
public void OnAddedToWorld(Actor actor, [ScriptEmmyTypeOverride("fun(self: actor)")] LuaFunction func)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace OpenRA.Mods.Common.Scripting
|
|||||||
{
|
{
|
||||||
public enum Trigger
|
public enum Trigger
|
||||||
{
|
{
|
||||||
OnIdle, OnDamaged, OnKilled, OnProduction, OnOtherProduction, OnPlayerWon, OnPlayerLost,
|
OnIdle, OnDamaged, OnKilled, OnProduction, OnOtherProduction, OnBuildingPlaced, OnPlayerWon, OnPlayerLost,
|
||||||
OnObjectiveAdded, OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnInfiltrated,
|
OnObjectiveAdded, OnObjectiveCompleted, OnObjectiveFailed, OnCapture, OnInfiltrated,
|
||||||
OnAddedToWorld, OnRemovedFromWorld, OnDiscovered, OnPlayerDiscovered,
|
OnAddedToWorld, OnRemovedFromWorld, OnDiscovered, OnPlayerDiscovered,
|
||||||
OnPassengerEntered, OnPassengerExited, OnSold, OnTimerExpired
|
OnPassengerEntered, OnPassengerExited, OnSold, OnTimerExpired
|
||||||
@@ -33,7 +33,7 @@ namespace OpenRA.Mods.Common.Scripting
|
|||||||
public override object Create(ActorInitializer init) { return new ScriptTriggers(init.World, init.Self); }
|
public override object Create(ActorInitializer init) { return new ScriptTriggers(init.World, init.Self); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, INotifyOtherProduction,
|
public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, INotifyBuildingPlaced, INotifyOtherProduction,
|
||||||
INotifyObjectivesUpdated, INotifyCapture, INotifyInfiltrated, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyDiscovered, INotifyActorDisposing,
|
INotifyObjectivesUpdated, INotifyCapture, INotifyInfiltrated, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyDiscovered, INotifyActorDisposing,
|
||||||
INotifyPassengerEntered, INotifyPassengerExited, INotifySold, INotifyWinStateChanged, INotifyTimeLimit
|
INotifyPassengerEntered, INotifyPassengerExited, INotifySold, INotifyWinStateChanged, INotifyTimeLimit
|
||||||
{
|
{
|
||||||
@@ -280,6 +280,28 @@ namespace OpenRA.Mods.Common.Scripting
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void INotifyBuildingPlaced.BuildingPlaced(Actor self, Actor building)
|
||||||
|
{
|
||||||
|
if (world.Disposing)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Run Lua callbacks
|
||||||
|
foreach (var f in Triggerables(Trigger.OnBuildingPlaced))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var a = self.Owner.ToLuaValue(f.Context))
|
||||||
|
using (var b = building.ToLuaValue(f.Context))
|
||||||
|
f.Function.Call(a, b).Dispose();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
f.Context.FatalError(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void INotifyCapture.OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner, BitSet<CaptureType> captureTypes)
|
void INotifyCapture.OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner, BitSet<CaptureType> captureTypes)
|
||||||
{
|
{
|
||||||
if (world.Disposing)
|
if (world.Disposing)
|
||||||
|
|||||||
@@ -131,6 +131,13 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
foreach (var s in buildingInfo.BuildSounds)
|
foreach (var s in buildingInfo.BuildSounds)
|
||||||
Game.Sound.PlayToPlayer(SoundType.World, order.Player, s, placed.CenterPosition);
|
Game.Sound.PlayToPlayer(SoundType.World, order.Player, s, placed.CenterPosition);
|
||||||
|
|
||||||
|
if (producer.Actor != null)
|
||||||
|
foreach (var nbp in producer.Actor.TraitsImplementing<INotifyBuildingPlaced>())
|
||||||
|
nbp.BuildingPlaced(producer.Actor, placed);
|
||||||
|
|
||||||
|
foreach (var nbp in self.TraitsImplementing<INotifyBuildingPlaced>())
|
||||||
|
nbp.BuildingPlaced(self, placed);
|
||||||
|
|
||||||
// Build the connection segments
|
// Build the connection segments
|
||||||
var segmentType = actorInfo.TraitInfo<LineBuildInfo>().SegmentType;
|
var segmentType = actorInfo.TraitInfo<LineBuildInfo>().SegmentType;
|
||||||
if (string.IsNullOrEmpty(segmentType))
|
if (string.IsNullOrEmpty(segmentType))
|
||||||
@@ -141,8 +148,8 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
if (t.Cell == targetLocation)
|
if (t.Cell == targetLocation)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var segment = self.World.Map.Rules.Actors[segmentType];
|
var segmentInfo = self.World.Map.Rules.Actors[segmentType];
|
||||||
var replaceableSegments = segment.TraitInfos<ReplacementInfo>()
|
var replaceableSegments = segmentInfo.TraitInfos<ReplacementInfo>()
|
||||||
.SelectMany(r => r.ReplaceableTypes)
|
.SelectMany(r => r.ReplaceableTypes)
|
||||||
.ToHashSet();
|
.ToHashSet();
|
||||||
|
|
||||||
@@ -151,7 +158,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
if (a.TraitsImplementing<Replaceable>().Any(r => !r.IsTraitDisabled && r.Info.Types.Overlaps(replaceableSegments)))
|
if (a.TraitsImplementing<Replaceable>().Any(r => !r.IsTraitDisabled && r.Info.Types.Overlaps(replaceableSegments)))
|
||||||
self.World.Remove(a);
|
self.World.Remove(a);
|
||||||
|
|
||||||
w.CreateActor(segmentType,
|
var segment = w.CreateActor(segmentType,
|
||||||
[
|
[
|
||||||
new LocationInit(t.Cell),
|
new LocationInit(t.Cell),
|
||||||
new OwnerInit(order.Player),
|
new OwnerInit(order.Player),
|
||||||
@@ -160,6 +167,13 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
new LineBuildParentInit([t.Actor, placed]),
|
new LineBuildParentInit([t.Actor, placed]),
|
||||||
new PlaceBuildingInit()
|
new PlaceBuildingInit()
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
if (producer.Actor != null)
|
||||||
|
foreach (var nbp in producer.Actor.TraitsImplementing<INotifyBuildingPlaced>())
|
||||||
|
nbp.BuildingPlaced(producer.Actor, segment);
|
||||||
|
|
||||||
|
foreach (var nbp in self.TraitsImplementing<INotifyBuildingPlaced>())
|
||||||
|
nbp.BuildingPlaced(self, segment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (os == "PlacePlug")
|
else if (os == "PlacePlug")
|
||||||
@@ -201,11 +215,14 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
|
|
||||||
foreach (var s in buildingInfo.BuildSounds)
|
foreach (var s in buildingInfo.BuildSounds)
|
||||||
Game.Sound.PlayToPlayer(SoundType.World, order.Player, s, building.CenterPosition);
|
Game.Sound.PlayToPlayer(SoundType.World, order.Player, s, building.CenterPosition);
|
||||||
}
|
|
||||||
|
|
||||||
if (producer.Actor != null)
|
if (producer.Actor != null)
|
||||||
foreach (var nbp in producer.Actor.TraitsImplementing<INotifyBuildingPlaced>())
|
foreach (var nbp in producer.Actor.TraitsImplementing<INotifyBuildingPlaced>())
|
||||||
nbp.BuildingPlaced(producer.Actor);
|
nbp.BuildingPlaced(producer.Actor, building);
|
||||||
|
|
||||||
|
foreach (var nbp in self.TraitsImplementing<INotifyBuildingPlaced>())
|
||||||
|
nbp.BuildingPlaced(self, building);
|
||||||
|
}
|
||||||
|
|
||||||
queue.EndProduction(item);
|
queue.EndProduction(item);
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ namespace OpenRA.Mods.Common.Traits.Render
|
|||||||
wsb = self.TraitsImplementing<WithSpriteBody>().Single(w => w.Info.Name == info.Body);
|
wsb = self.TraitsImplementing<WithSpriteBody>().Single(w => w.Info.Name == info.Body);
|
||||||
}
|
}
|
||||||
|
|
||||||
void INotifyBuildingPlaced.BuildingPlaced(Actor self)
|
void INotifyBuildingPlaced.BuildingPlaced(Actor self, Actor building)
|
||||||
{
|
{
|
||||||
if (!IsTraitDisabled)
|
if (!IsTraitDisabled)
|
||||||
wsb.PlayCustomAnimation(self, Info.Sequence);
|
wsb.PlayCustomAnimation(self, Info.Sequence);
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ namespace OpenRA.Mods.Common.Traits.Render
|
|||||||
overlay.ReplaceAnim(RenderSprites.NormalizeSequence(overlay, e.DamageState, overlay.CurrentSequence.Name));
|
overlay.ReplaceAnim(RenderSprites.NormalizeSequence(overlay, e.DamageState, overlay.CurrentSequence.Name));
|
||||||
}
|
}
|
||||||
|
|
||||||
void INotifyBuildingPlaced.BuildingPlaced(Actor self)
|
void INotifyBuildingPlaced.BuildingPlaced(Actor self, Actor building)
|
||||||
{
|
{
|
||||||
visible = true;
|
visible = true;
|
||||||
overlay.PlayThen(overlay.CurrentSequence.Name, () => visible = false);
|
overlay.PlayThen(overlay.CurrentSequence.Name, () => visible = false);
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
public interface INotifyPowerLevelChanged { void PowerLevelChanged(Actor self); }
|
public interface INotifyPowerLevelChanged { void PowerLevelChanged(Actor self); }
|
||||||
public interface INotifySupportPower { void Charged(Actor self); void Activated(Actor self); }
|
public interface INotifySupportPower { void Charged(Actor self); void Activated(Actor self); }
|
||||||
|
|
||||||
public interface INotifyBuildingPlaced { void BuildingPlaced(Actor self); }
|
public interface INotifyBuildingPlaced { void BuildingPlaced(Actor self, Actor building); }
|
||||||
public interface INotifyBurstComplete { void FiredBurst(Actor self, in Target target, Armament a); }
|
public interface INotifyBurstComplete { void FiredBurst(Actor self, in Target target, Armament a); }
|
||||||
public interface INotifyChat { bool OnChat(string from, string message); }
|
public interface INotifyChat { bool OnChat(string from, string message); }
|
||||||
public interface INotifyProduction { void UnitProduced(Actor self, Actor other, CPos exit); }
|
public interface INotifyProduction { void UnitProduced(Actor self, Actor other, CPos exit); }
|
||||||
|
|||||||
Reference in New Issue
Block a user