Merge pull request #9415 from atlimit8/SpawnActorOnDeath_to_Common
Move SpawnActorOnDeath to OpenRA.Mods.Common
This commit is contained in:
@@ -82,7 +82,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
bool IOccupySpaceInfo.SharesCell { get { return false; } }
|
||||
}
|
||||
|
||||
public class Aircraft : ITick, ISync, IFacing, IPositionable, IMove, IIssueOrder, IResolveOrder, IOrderVoice,
|
||||
public class Aircraft : ITick, ISync, IFacing, IPositionable, IMove, IIssueOrder, IResolveOrder, IOrderVoice, IDeathActorInitModifier,
|
||||
INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyActorDisposing
|
||||
{
|
||||
static readonly Pair<CPos, SubCell>[] NoCells = { };
|
||||
@@ -328,6 +328,11 @@ namespace OpenRA.Mods.Common.Traits
|
||||
yield return new Repair(a);
|
||||
}
|
||||
|
||||
public void ModifyDeathActorInit(Actor self, TypeDictionary init)
|
||||
{
|
||||
init.Add(new FacingInit(Facing));
|
||||
}
|
||||
|
||||
#region Implement IPositionable
|
||||
|
||||
public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return false; } // TODO: Handle landing
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
bool IOccupySpaceInfo.SharesCell { get { return false; } }
|
||||
}
|
||||
|
||||
public class Husk : IPositionable, IFacing, ISync, INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld, IDisable
|
||||
public class Husk : IPositionable, IFacing, ISync, INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld, IDisable, IDeathActorInitModifier
|
||||
{
|
||||
readonly HuskInfo info;
|
||||
readonly Actor self;
|
||||
@@ -127,6 +127,11 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public void ModifyDeathActorInit(Actor self, TypeDictionary init)
|
||||
{
|
||||
init.Add(new FacingInit(Facing));
|
||||
}
|
||||
}
|
||||
|
||||
public class HuskSpeedInit : IActorInit<int>
|
||||
|
||||
@@ -304,7 +304,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
bool IOccupySpaceInfo.SharesCell { get { return SharesCell; } }
|
||||
}
|
||||
|
||||
public class Mobile : IIssueOrder, IResolveOrder, IOrderVoice, IPositionable, IMove, IFacing, ISync, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyBlockingMove
|
||||
public class Mobile : IIssueOrder, IResolveOrder, IOrderVoice, IPositionable, IMove, IFacing, ISync, IDeathActorInitModifier,
|
||||
INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyBlockingMove
|
||||
{
|
||||
const int AverageTicksBeforePathing = 5;
|
||||
const int SpreadTicksBeforePathing = 5;
|
||||
@@ -793,5 +794,14 @@ namespace OpenRA.Mods.Common.Traits
|
||||
var facing = Util.GetFacing(toPos - fromPos, Facing);
|
||||
return Util.SequenceActivities(new Turn(self, facing), new Drag(self, fromPos, toPos, length));
|
||||
}
|
||||
|
||||
public void ModifyDeathActorInit(Actor self, TypeDictionary init)
|
||||
{
|
||||
init.Add(new FacingInit(facing));
|
||||
|
||||
// Allows the husk to drag to its final position
|
||||
if (CanEnterCell(self.Location, self, false))
|
||||
init.Add(new HuskSpeedInit(MovementSpeedForCell(self, self.Location)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
109
OpenRA.Mods.Common/Traits/SpawnActorOnDeath.cs
Normal file
109
OpenRA.Mods.Common/Traits/SpawnActorOnDeath.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common.Warheads;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public enum OwnerType { Victim, Killer, InternalName }
|
||||
|
||||
[Desc("Spawn another actor immediately upon death.")]
|
||||
public class SpawnActorOnDeathInfo : ITraitInfo
|
||||
{
|
||||
[ActorReference, FieldLoader.Require]
|
||||
[Desc("Actor to spawn on death.")]
|
||||
public readonly string Actor = null;
|
||||
|
||||
[Desc("Probability the actor spawns.")]
|
||||
public readonly int Probability = 100;
|
||||
|
||||
[Desc("Owner of the spawned actor. Allowed keywords:" +
|
||||
"'Victim', 'Killer' and 'InternalName'.")]
|
||||
public readonly OwnerType OwnerType = OwnerType.Victim;
|
||||
|
||||
[Desc("Map player to use when 'InternalName' is defined on 'OwnerType'.")]
|
||||
public readonly string InternalOwner = null;
|
||||
|
||||
[Desc("DeathType that triggers the actor spawn. " +
|
||||
"Leave empty to spawn an actor ignoring the DeathTypes.")]
|
||||
public readonly string DeathType = null;
|
||||
|
||||
[Desc("Skips the spawned actor's make animations if true.")]
|
||||
public readonly bool SkipMakeAnimations = true;
|
||||
|
||||
[Desc("Should an actor only be spawned when the 'Creeps' setting is true?")]
|
||||
public readonly bool RequiresLobbyCreeps = false;
|
||||
|
||||
public object Create(ActorInitializer init) { return new SpawnActorOnDeath(init, this); }
|
||||
}
|
||||
|
||||
public class SpawnActorOnDeath : INotifyKilled
|
||||
{
|
||||
readonly SpawnActorOnDeathInfo info;
|
||||
readonly string faction;
|
||||
|
||||
public SpawnActorOnDeath(ActorInitializer init, SpawnActorOnDeathInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
|
||||
faction = init.Contains<FactionInit>() ? init.Get<FactionInit, string>() : init.Self.Owner.Faction.InternalName;
|
||||
}
|
||||
|
||||
public void Killed(Actor self, AttackInfo e)
|
||||
{
|
||||
if (info.RequiresLobbyCreeps && !self.World.LobbyInfo.GlobalSettings.Creeps)
|
||||
return;
|
||||
|
||||
if (!self.IsInWorld)
|
||||
return;
|
||||
|
||||
if (self.World.SharedRandom.Next(100) > info.Probability)
|
||||
return;
|
||||
|
||||
var warhead = e.Warhead as DamageWarhead;
|
||||
if (info.DeathType != null && (warhead == null || !warhead.DamageTypes.Contains(info.DeathType)))
|
||||
return;
|
||||
|
||||
self.World.AddFrameEndTask(w =>
|
||||
{
|
||||
var td = new TypeDictionary
|
||||
{
|
||||
new ParentActorInit(self),
|
||||
new LocationInit(self.Location),
|
||||
new CenterPositionInit(self.CenterPosition),
|
||||
new FactionInit(faction)
|
||||
};
|
||||
|
||||
if (info.OwnerType == OwnerType.Victim)
|
||||
td.Add(new OwnerInit(self.Owner));
|
||||
else if (info.OwnerType == OwnerType.Killer)
|
||||
td.Add(new OwnerInit(e.Attacker.Owner));
|
||||
else
|
||||
td.Add(new OwnerInit(self.World.Players.First(p => p.InternalName == info.InternalOwner)));
|
||||
|
||||
if (info.SkipMakeAnimations)
|
||||
td.Add(new SkipMakeAnimsInit());
|
||||
|
||||
foreach (var modifier in self.TraitsImplementing<IDeathActorInitModifier>())
|
||||
modifier.ModifyDeathActorInit(self, td);
|
||||
|
||||
var huskActor = self.TraitsImplementing<IHuskModifier>()
|
||||
.Select(ihm => ihm.HuskActor(self))
|
||||
.FirstOrDefault(a => a != null);
|
||||
|
||||
w.CreateActor(huskActor ?? info.Actor, td);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
@@ -30,7 +31,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public virtual object Create(ActorInitializer init) { return new Turreted(init, this); }
|
||||
}
|
||||
|
||||
public class Turreted : ITick, ISync, INotifyCreated
|
||||
public class Turreted : ITick, ISync, INotifyCreated, IDeathActorInitModifier
|
||||
{
|
||||
readonly TurretedInfo info;
|
||||
AttackTurreted attack;
|
||||
@@ -135,6 +136,18 @@ namespace OpenRA.Mods.Common.Traits
|
||||
var facing = Util.QuantizeFacing(local.Yaw.Angle / 4, QuantizedFacings) * (256 / QuantizedFacings);
|
||||
return new WRot(WAngle.Zero, WAngle.Zero, WAngle.FromFacing(facing));
|
||||
}
|
||||
|
||||
public void ModifyDeathActorInit(Actor self, TypeDictionary init)
|
||||
{
|
||||
var facings = init.GetOrDefault<TurretFacingsInit>();
|
||||
if (facings == null)
|
||||
{
|
||||
facings = new TurretFacingsInit();
|
||||
init.Add(facings);
|
||||
}
|
||||
|
||||
facings.Value(self.World).Add(Name, facing.Facing);
|
||||
}
|
||||
}
|
||||
|
||||
public class TurretFacingInit : IActorInit<int>
|
||||
|
||||
Reference in New Issue
Block a user