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

@@ -178,7 +178,7 @@ namespace OpenRA.Mods.Common.Orders
if (!Game.GetModifierKeys().HasModifier(Modifiers.Shift)) if (!Game.GetModifierKeys().HasModifier(Modifiers.Shift))
foreach (var t in BuildingUtils.GetLineBuildCells(world, topLeft, building, buildingInfo)) foreach (var t in BuildingUtils.GetLineBuildCells(world, topLeft, building, buildingInfo))
cells.Add(t, buildingInfo.IsCloseEnoughToBase(world, world.LocalPlayer, building, t)); cells.Add(t.First, buildingInfo.IsCloseEnoughToBase(world, world.LocalPlayer, building, t.First));
else else
cells.Add(topLeft, buildingInfo.IsCloseEnoughToBase(world, world.LocalPlayer, building, topLeft)); cells.Add(topLeft, buildingInfo.IsCloseEnoughToBase(world, world.LocalPlayer, building, topLeft));
} }

View File

@@ -11,6 +11,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Primitives;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
@@ -48,18 +49,20 @@ namespace OpenRA.Mods.Common.Traits
world.IsCellBuildable(t, building, toIgnore)); world.IsCellBuildable(t, building, toIgnore));
} }
public static IEnumerable<CPos> GetLineBuildCells(World world, CPos location, string name, BuildingInfo bi) public static IEnumerable<Pair<CPos, Actor>> GetLineBuildCells(World world, CPos location, string name, BuildingInfo bi)
{ {
var lbi = world.Map.Rules.Actors[name].TraitInfo<LineBuildInfo>(); var lbi = world.Map.Rules.Actors[name].TraitInfo<LineBuildInfo>();
var topLeft = location; // 1x1 assumption! var topLeft = location; // 1x1 assumption!
if (world.IsCellBuildable(topLeft, bi)) if (world.IsCellBuildable(topLeft, bi))
yield return topLeft; yield return Pair.New<CPos, Actor>(topLeft, null);
// Start at place location, search outwards // Start at place location, search outwards
// TODO: First make it work, then make it nice // TODO: First make it work, then make it nice
var vecs = new[] { new CVec(1, 0), new CVec(0, 1), new CVec(-1, 0), new CVec(0, -1) }; var vecs = new[] { new CVec(1, 0), new CVec(0, 1), new CVec(-1, 0), new CVec(0, -1) };
int[] dirs = { 0, 0, 0, 0 }; int[] dirs = { 0, 0, 0, 0 };
Actor[] connectors = { null, null, null, null };
for (var d = 0; d < 4; d++) for (var d = 0; d < 4; d++)
{ {
for (var i = 1; i < lbi.Range; i++) for (var i = 1; i < lbi.Range; i++)
@@ -72,17 +75,17 @@ namespace OpenRA.Mods.Common.Traits
continue; // Cell is empty; continue search continue; // Cell is empty; continue search
// Cell contains an actor. Is it the type we want? // Cell contains an actor. Is it the type we want?
var hasConnector = world.ActorMap.GetActorsAt(cell) connectors[d] = world.ActorMap.GetActorsAt(cell)
.Any(a => a.Info.TraitInfos<LineBuildNodeInfo>() .FirstOrDefault(a => a.Info.TraitInfos<LineBuildNodeInfo>()
.Any(info => info.Types.Overlaps(lbi.NodeTypes) && info.Connections.Contains(vecs[d]))); .Any(info => info.Types.Overlaps(lbi.NodeTypes) && info.Connections.Contains(vecs[d])));
dirs[d] = hasConnector ? i : -1; dirs[d] = connectors[d] != null ? i : -1;
} }
// Place intermediate-line sections // Place intermediate-line sections
if (dirs[d] > 0) if (dirs[d] > 0)
for (var i = 1; i < dirs[d]; i++) for (var i = 1; i < dirs[d]; i++)
yield return topLeft + i * vecs[d]; yield return Pair.New(topLeft + i * vecs[d], connectors[d]);
} }
} }
} }

View File

@@ -10,19 +10,91 @@
#endregion #endregion
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.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).")] [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.")] [Desc("The maximum allowed length of the line.")]
public readonly int Range = 5; public readonly int Range = 5;
[Desc("LineBuildNode 'Types' to attach to.")] [Desc("LineBuildNode 'Types' to attach to.")]
public readonly HashSet<string> NodeTypes = new HashSet<string> { "wall" }; 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);
}
}
} }

View File

@@ -74,21 +74,34 @@ namespace OpenRA.Mods.Common.Traits
if (os == "LineBuild") if (os == "LineBuild")
{ {
var playSounds = true; // Build the parent actor first
var placed = w.CreateActor(order.TargetString, new TypeDictionary
{
new LocationInit(order.TargetLocation),
new OwnerInit(order.Player),
new FactionInit(faction),
});
foreach (var s in buildingInfo.BuildSounds)
Game.Sound.PlayToPlayer(SoundType.World, order.Player, s, placed.CenterPosition);
// Build the connection segments
var segmentType = unit.TraitInfo<LineBuildInfo>().SegmentType;
if (string.IsNullOrEmpty(segmentType))
segmentType = order.TargetString;
foreach (var t in BuildingUtils.GetLineBuildCells(w, order.TargetLocation, order.TargetString, buildingInfo)) foreach (var t in BuildingUtils.GetLineBuildCells(w, order.TargetLocation, order.TargetString, buildingInfo))
{ {
var building = w.CreateActor(order.TargetString, new TypeDictionary if (t.First == order.TargetLocation)
continue;
w.CreateActor(t.First == order.TargetLocation ? order.TargetString : segmentType, new TypeDictionary
{ {
new LocationInit(t), new LocationInit(t.First),
new OwnerInit(order.Player), new OwnerInit(order.Player),
new FactionInit(faction) new FactionInit(faction),
new LineBuildParentInit(new[] { t.Second, placed })
}); });
if (playSounds)
foreach (var s in buildingInfo.BuildSounds)
Game.Sound.PlayToPlayer(SoundType.World, order.Player, s, building.CenterPosition);
playSounds = false;
} }
} }
else if (os == "PlacePlug") else if (os == "PlacePlug")