Add GrantConditionOnLineBuildDirection trait.

This commit is contained in:
Paul Chote
2017-03-27 18:20:58 +01:00
parent f8af51643d
commit a83c0f96dd
4 changed files with 62 additions and 0 deletions

View File

@@ -800,6 +800,7 @@
<Compile Include="Traits\Render\WithChargeAnimation.cs" /> <Compile Include="Traits\Render\WithChargeAnimation.cs" />
<Compile Include="Traits\Render\WithChargeOverlay.cs" /> <Compile Include="Traits\Render\WithChargeOverlay.cs" />
<Compile Include="WebServices.cs" /> <Compile Include="WebServices.cs" />
<Compile Include="Traits\Conditions\GrantConditionOnLineBuildDirection.cs" />
<Compile Include="Traits\Conditions\LineBuildSegmentExternalCondition.cs" /> <Compile Include="Traits\Conditions\LineBuildSegmentExternalCondition.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View File

@@ -15,6 +15,15 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
public enum LineBuildDirection { Unset, X, Y }
public class LineBuildDirectionInit : IActorInit<LineBuildDirection>
{
[FieldFromYamlKey] readonly LineBuildDirection value = LineBuildDirection.Unset;
public LineBuildDirectionInit() { }
public LineBuildDirectionInit(LineBuildDirection init) { value = init; }
public LineBuildDirection Value(World world) { return value; }
}
public class LineBuildParentInit : IActorInit<Actor[]> public class LineBuildParentInit : IActorInit<Actor[]>
{ {
[FieldFromYamlKey] public readonly string[] ParentNames = new string[0]; [FieldFromYamlKey] public readonly string[] ParentNames = new string[0];

View File

@@ -0,0 +1,51 @@
#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 OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class GrantConditionOnLineBuildDirectionInfo : ITraitInfo, Requires<LineBuildInfo>
{
[FieldLoader.Require]
[GrantedConditionReference]
[Desc("Condition to grant.")]
public readonly string Condition = null;
[FieldLoader.Require]
[Desc("Line build direction to trigger the condition.")]
public readonly LineBuildDirection Direction = LineBuildDirection.X;
public object Create(ActorInitializer init) { return new GrantConditionOnLineBuildDirection(init, this); }
}
public class GrantConditionOnLineBuildDirection : INotifyCreated
{
readonly GrantConditionOnLineBuildDirectionInfo info;
readonly LineBuildDirection direction;
public GrantConditionOnLineBuildDirection(ActorInitializer init, GrantConditionOnLineBuildDirectionInfo info)
{
this.info = info;
direction = init.Get<LineBuildDirectionInit>().Value(init.World);
}
void INotifyCreated.Created(Actor self)
{
if (direction != info.Direction)
return;
var conditionManager = self.TraitOrDefault<ConditionManager>();
if (conditionManager != null && !string.IsNullOrEmpty(info.Condition))
conditionManager.GrantCondition(self, info.Condition);
}
}
}

View File

@@ -103,6 +103,7 @@ namespace OpenRA.Mods.Common.Traits
new LocationInit(t.First), new LocationInit(t.First),
new OwnerInit(order.Player), new OwnerInit(order.Player),
new FactionInit(faction), new FactionInit(faction),
new LineBuildDirectionInit(t.First.X == order.TargetLocation.X ? LineBuildDirection.Y : LineBuildDirection.X),
new LineBuildParentInit(new[] { t.Second, placed }) new LineBuildParentInit(new[] { t.Second, placed })
}); });
} }