101 lines
2.9 KiB
C#
101 lines
2.9 KiB
C#
#region Copyright & License Information
|
|
/*
|
|
* Copyright 2007-2017 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, either version 3 of
|
|
* the License, or (at your option) any later version. For more
|
|
* information, see COPYING.
|
|
*/
|
|
#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 : 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 : 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);
|
|
}
|
|
}
|
|
}
|