Add ProductionSpawnLocationInit, used by ProductionFromMapEdge

This enables fixed per-factory actor spawn locations.
This commit is contained in:
Oliver Brakmann
2016-02-28 14:15:29 +01:00
parent a1e6f65f8f
commit f547a4b6e9

View File

@@ -10,6 +10,7 @@
#endregion #endregion
using System.Drawing; using System.Drawing;
using OpenRA;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Traits; using OpenRA.Traits;
@@ -17,17 +18,22 @@ using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits namespace OpenRA.Mods.D2k.Traits
{ {
[Desc("Produce a unit on the closest map edge cell and move into the world.")] [Desc("Produce a unit on the closest map edge cell and move into the world.")]
class ProductionFromMapEdgeInfo : ProductionInfo class ProductionFromMapEdgeInfo : ProductionInfo, UsesInit<ProductionSpawnLocationInit>
{ {
public override object Create(ActorInitializer init) { return new ProductionFromMapEdge(init, this); } public override object Create(ActorInitializer init) { return new ProductionFromMapEdge(init, this); }
} }
class ProductionFromMapEdge : Production, INotifyCreated class ProductionFromMapEdge : Production, INotifyCreated
{ {
readonly CPos? spawnLocation;
RallyPoint rp; RallyPoint rp;
public ProductionFromMapEdge(ActorInitializer init, ProductionInfo info) public ProductionFromMapEdge(ActorInitializer init, ProductionInfo info)
: base(init, info) { } : base(init, info)
{
if (init.Contains<ProductionSpawnLocationInit>())
spawnLocation = init.Get<ProductionSpawnLocationInit, CPos>();
}
void INotifyCreated.Created(Actor self) void INotifyCreated.Created(Actor self)
{ {
@@ -36,7 +42,8 @@ namespace OpenRA.Mods.D2k.Traits
public override bool Produce(Actor self, ActorInfo producee, string factionVariant) public override bool Produce(Actor self, ActorInfo producee, string factionVariant)
{ {
var location = self.World.Map.ChooseClosestEdgeCell(self.Location); var location = spawnLocation.HasValue ? spawnLocation.Value : self.World.Map.ChooseClosestEdgeCell(self.Location);
var pos = self.World.Map.CenterOfCell(location); var pos = self.World.Map.CenterOfCell(location);
// If aircraft, spawn at cruise altitude // If aircraft, spawn at cruise altitude
@@ -84,4 +91,12 @@ namespace OpenRA.Mods.D2k.Traits
return true; return true;
} }
} }
public class ProductionSpawnLocationInit : IActorInit<CPos>
{
[FieldFromYamlKey] readonly CPos value = CPos.Zero;
public ProductionSpawnLocationInit() { }
public ProductionSpawnLocationInit(CPos init) { value = init; }
public CPos Value(World world) { return value; }
}
} }