Fix Production.cs not properly using INotifyCreated

This commit is contained in:
abcdefg30
2020-10-24 12:19:42 +02:00
committed by Paul Chote
parent f5a963ac47
commit 774f2e0852

View File

@@ -27,19 +27,24 @@ namespace OpenRA.Mods.Common.Traits
public override object Create(ActorInitializer init) { return new Production(init, this); } public override object Create(ActorInitializer init) { return new Production(init, this); }
} }
public class Production : PausableConditionalTrait<ProductionInfo>, INotifyCreated public class Production : PausableConditionalTrait<ProductionInfo>
{ {
readonly Lazy<RallyPoint> rp; RallyPoint rp;
public string Faction { get; private set; } public string Faction { get; private set; }
public Production(ActorInitializer init, ProductionInfo info) public Production(ActorInitializer init, ProductionInfo info)
: base(info) : base(info)
{ {
rp = Exts.Lazy(() => init.Self.IsDead ? null : init.Self.TraitOrDefault<RallyPoint>());
Faction = init.GetValue<FactionInit, string>(init.Self.Owner.Faction.InternalName); Faction = init.GetValue<FactionInit, string>(init.Self.Owner.Faction.InternalName);
} }
protected override void Created(Actor self)
{
rp = self.TraitOrDefault<RallyPoint>();
base.Created(self);
}
public virtual void DoProduction(Actor self, ActorInfo producee, ExitInfo exitinfo, string productionType, TypeDictionary inits) public virtual void DoProduction(Actor self, ActorInfo producee, ExitInfo exitinfo, string productionType, TypeDictionary inits)
{ {
var exit = CPos.Zero; var exit = CPos.Zero;
@@ -71,7 +76,7 @@ namespace OpenRA.Mods.Common.Traits
else else
initialFacing = exitinfo.Facing.Value; initialFacing = exitinfo.Facing.Value;
exitLocations = rp.Value != null && rp.Value.Path.Count > 0 ? rp.Value.Path : new List<CPos> { exit }; exitLocations = rp != null && rp.Path.Count > 0 ? rp.Path : new List<CPos> { exit };
td.Add(new LocationInit(exit)); td.Add(new LocationInit(exit));
td.Add(new CenterPositionInit(spawn)); td.Add(new CenterPositionInit(spawn));
@@ -101,10 +106,10 @@ namespace OpenRA.Mods.Common.Traits
protected virtual Exit SelectExit(Actor self, ActorInfo producee, string productionType, Func<Exit, bool> p) protected virtual Exit SelectExit(Actor self, ActorInfo producee, string productionType, Func<Exit, bool> p)
{ {
if (!rp.IsValueCreated || rp.Value.Path.Count == 0) if (rp == null || rp.Path.Count == 0)
return self.RandomExitOrDefault(self.World, productionType, p); return self.RandomExitOrDefault(self.World, productionType, p);
return self.NearestExitOrDefault(self.World.Map.CenterOfCell(rp.Value.Path[0]), productionType, p); return self.NearestExitOrDefault(self.World.Map.CenterOfCell(rp.Path[0]), productionType, p);
} }
protected Exit SelectExit(Actor self, ActorInfo producee, string productionType) protected Exit SelectExit(Actor self, ActorInfo producee, string productionType)