Distinguish between line build nodes and segments.

This commit is contained in:
Paul Chote
2017-03-25 17:19:38 +00:00
parent 62603b324d
commit 304e3ef9f9
4 changed files with 107 additions and 19 deletions

View File

@@ -10,19 +10,91 @@
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class LineBuildParentInit : IActorInit<Actor[]>
{
[FieldFromYamlKey] public readonly string[] ParentNames = new string[0];
readonly Actor[] parents = null;
public LineBuildParentInit() { }
public LineBuildParentInit(Actor[] init) { parents = init; }
public Actor[] Value(World world)
{
if (parents != null)
return parents;
var sma = world.WorldActor.Trait<SpawnMapActors>();
return ParentNames.Select(n => sma.Actors[n]).ToArray();
}
}
public interface INotifyLineBuildSegmentsChanged
{
void SegmentAdded(Actor self, Actor segment);
void SegmentRemoved(Actor self, Actor segment);
}
[Desc("Place the second actor in line to build more of the same at once (used for walls).")]
public class LineBuildInfo : TraitInfo<LineBuild>
public class LineBuildInfo : ITraitInfo
{
[Desc("The maximum allowed length of the line.")]
public readonly int Range = 5;
[Desc("LineBuildNode 'Types' to attach to.")]
public readonly HashSet<string> NodeTypes = new HashSet<string> { "wall" };
[ActorReference(typeof(LineBuildInfo))]
[Desc("Actor type for line-built segments (defaults to same actor).")]
public readonly string SegmentType = null;
public object Create(ActorInitializer init) { return new LineBuild(init, this); }
}
public class LineBuild { }
public class LineBuild : INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyLineBuildSegmentsChanged
{
readonly Actor[] parentNodes = new Actor[0];
HashSet<Actor> segments;
public LineBuild(ActorInitializer init, LineBuildInfo info)
{
if (init.Contains<LineBuildParentInit>())
parentNodes = init.Get<LineBuildParentInit>().Value(init.World);
}
void INotifyLineBuildSegmentsChanged.SegmentAdded(Actor self, Actor segment)
{
if (segments == null)
segments = new HashSet<Actor>();
segments.Add(segment);
}
void INotifyLineBuildSegmentsChanged.SegmentRemoved(Actor self, Actor segment)
{
if (segments == null)
return;
segments.Remove(segment);
}
void INotifyAddedToWorld.AddedToWorld(Actor self)
{
foreach (var parent in parentNodes)
if (!parent.Disposed)
foreach (var n in parent.TraitsImplementing<INotifyLineBuildSegmentsChanged>())
n.SegmentAdded(parent, self);
}
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
{
foreach (var parent in parentNodes)
if (!parent.Disposed)
foreach (var n in parent.TraitsImplementing<INotifyLineBuildSegmentsChanged>())
n.SegmentRemoved(parent, self);
}
}
}