Add EnergyWall trait.

This commit is contained in:
Paul Chote
2017-03-26 18:27:11 +00:00
parent a83c0f96dd
commit 64896eb73d
2 changed files with 113 additions and 0 deletions

View File

@@ -109,6 +109,7 @@
<Compile Include="Traits\Chronoshiftable.cs" /> <Compile Include="Traits\Chronoshiftable.cs" />
<Compile Include="Traits\Cloneable.cs" /> <Compile Include="Traits\Cloneable.cs" />
<Compile Include="Traits\Disguise.cs" /> <Compile Include="Traits\Disguise.cs" />
<Compile Include="Traits\EnergyWall.cs" />
<Compile Include="Traits\FrozenUnderFogUpdatedByGps.cs" /> <Compile Include="Traits\FrozenUnderFogUpdatedByGps.cs" />
<Compile Include="Traits\HarvesterHuskModifier.cs" /> <Compile Include="Traits\HarvesterHuskModifier.cs" />
<Compile Include="Traits\Infiltration\InfiltrateForCash.cs" /> <Compile Include="Traits\Infiltration\InfiltrateForCash.cs" />

View File

@@ -0,0 +1,112 @@
#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. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Traits;
using OpenRA.Support;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Traits
{
[Desc("Will open and be passable for actors that appear friendly when there are no enemies in range.")]
public class EnergyWallInfo : BuildingInfo, IObservesVariablesInfo, IRulesetLoaded
{
[FieldLoader.Require]
[WeaponReference]
[Desc("The weapon to attack units on top of the wall with when activated.")]
public readonly string Weapon = null;
[ConsumedConditionReference]
[Desc("Boolean expression defining the condition to activate this trait.")]
public readonly BooleanExpression ActiveCondition = null;
public override object Create(ActorInitializer init) { return new EnergyWall(init, this); }
public WeaponInfo WeaponInfo { get; private set; }
public void RulesetLoaded(Ruleset rules, ActorInfo ai)
{
WeaponInfo weaponInfo;
var weaponToLower = Weapon.ToLowerInvariant();
if (!rules.Weapons.TryGetValue(weaponToLower, out weaponInfo))
throw new YamlException("Weapons Ruleset does not contain an entry '{0}'".F(weaponToLower));
WeaponInfo = weaponInfo;
}
}
public class EnergyWall : Building, IObservesVariables, ITick, ITemporaryBlocker
{
readonly EnergyWallInfo info;
IEnumerable<CPos> blockedPositions;
// Initial state is active to match Building adding the influence to the ActorMap
// This will be updated by ConditionsChanged at actor creation.
bool active = true;
public EnergyWall(ActorInitializer init, EnergyWallInfo info)
: base(init, info)
{
this.info = info;
}
public virtual IEnumerable<VariableObserver> GetVariableObservers()
{
if (info.ActiveCondition != null)
yield return new VariableObserver(ActiveConditionChanged, info.ActiveCondition.Variables);
}
void ActiveConditionChanged(Actor self, IReadOnlyDictionary<string, int> conditions)
{
if (info.ActiveCondition == null)
return;
var wasActive = active;
active = info.ActiveCondition.Evaluate(conditions);
if (!wasActive && active)
self.World.ActorMap.AddInfluence(self, this);
else if (wasActive && !active)
self.World.ActorMap.RemoveInfluence(self, this);
}
void ITick.Tick(Actor self)
{
if (!active)
return;
foreach (var loc in blockedPositions)
{
var blockers = self.World.ActorMap.GetActorsAt(loc).Where(a => !a.IsDead && a != self);
foreach (var blocker in blockers)
info.WeaponInfo.Impact(Target.FromActor(blocker), self, Enumerable.Empty<int>());
}
}
bool ITemporaryBlocker.IsBlocking(Actor self, CPos cell)
{
return active && blockedPositions.Contains(cell);
}
bool ITemporaryBlocker.CanRemoveBlockage(Actor self, Actor blocking)
{
return !active;
}
public override void AddedToWorld(Actor self)
{
base.AddedToWorld(self);
blockedPositions = FootprintUtils.Tiles(self);
}
}
}