Move Power out of Building and into its own trait

Conflicts:
	mods/ts/rules/structures.yaml
This commit is contained in:
ScottNZ
2014-08-02 01:38:14 +12:00
parent 9527d7c2f4
commit 692e3a9c88
23 changed files with 383 additions and 211 deletions

View File

@@ -23,8 +23,6 @@ namespace OpenRA.Mods.RA.Buildings
public class BuildingInfo : ITraitInfo, IOccupySpaceInfo, UsesInit<LocationInit>
{
[Desc("If negative, it will drain power, if positive, it will provide power.")]
public readonly int Power = 0;
[Desc("Where you are allowed to place the building (Water, Clear, ...)")]
public readonly string[] TerrainTypes = {};
[Desc("The range to the next building it can be constructed. Set it higher for walls.")]
@@ -101,7 +99,7 @@ namespace OpenRA.Mods.RA.Buildings
}
}
public class Building : INotifyDamage, IOccupySpace, INotifyCapture, INotifySold, INotifyTransform, ISync, ITechTreePrerequisite, INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld
public class Building : IOccupySpace, INotifySold, INotifyTransform, ISync, ITechTreePrerequisite, INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld
{
public readonly BuildingInfo Info;
public bool BuildComplete { get; private set; }
@@ -109,8 +107,6 @@ namespace OpenRA.Mods.RA.Buildings
readonly Actor self;
public readonly bool SkipMakeAnimation;
PowerManager PlayerPower;
/* shared activity lock: undeploy, sell, capture, etc */
[Sync] public bool Locked = true;
@@ -135,7 +131,6 @@ namespace OpenRA.Mods.RA.Buildings
this.self = init.self;
this.topLeft = init.Get<LocationInit, CPos>();
this.Info = info;
this.PlayerPower = init.self.Owner.PlayerActor.Trait<PowerManager>();
occupiedCells = FootprintUtils.UnpathableTiles( self.Info.Name, Info, TopLeft )
.Select(c => Pair.New(c, SubCell.FullCell)).ToArray();
@@ -144,30 +139,9 @@ namespace OpenRA.Mods.RA.Buildings
SkipMakeAnimation = init.Contains<SkipMakeAnimsInit>();
}
public int GetPowerUsage()
{
if (Info.Power <= 0)
return Info.Power;
var health = self.TraitOrDefault<Health>();
return health != null ? (Info.Power * health.HP / health.MaxHP) : Info.Power;
}
public void Damaged(Actor self, AttackInfo e)
{
// Power plants lose power with damage
if (Info.Power > 0)
PlayerPower.UpdateActor(self, GetPowerUsage());
}
Pair<CPos, SubCell>[] occupiedCells;
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return occupiedCells; }
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{
PlayerPower = newOwner.PlayerActor.Trait<PowerManager>();
}
public void Created(Actor self)
{
if (SkipMakeAnimation || !self.HasTrait<WithMakeAnimation>())

View File

@@ -1,55 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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 OpenRA.Mods.RA.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Buildings
{
[Desc("The player can disable the power individually on this actor.")]
public class CanPowerDownInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new CanPowerDown(init); }
}
public class CanPowerDown : IResolveOrder, IDisable, INotifyCapture, ISync
{
[Sync] bool disabled = false;
int normalPower = 0;
PowerManager PowerManager;
public CanPowerDown(ActorInitializer init)
{
PowerManager = init.self.Owner.PlayerActor.Trait<PowerManager>();
normalPower = init.self.Info.Traits.Get<BuildingInfo>().Power;
}
public bool Disabled { get { return disabled; } }
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "PowerDown")
{
disabled = !disabled;
Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Sounds", (disabled ? "EnablePower" : "DisablePower"), self.Owner.Country.Race);
PowerManager.UpdateActor(self, disabled ? 0 : normalPower);
if (disabled)
self.World.AddFrameEndTask(
w => w.Add(new PowerdownIndicator(self)));
}
}
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{
PowerManager = newOwner.PlayerActor.Trait<PowerManager>();
}
}
}

View File

@@ -21,11 +21,11 @@ namespace OpenRA.Mods.RA.Buildings
public class PowerManager : ITick, ISync
{
PowerManagerInfo Info;
Player Player;
DeveloperMode devMode;
readonly PowerManagerInfo info;
readonly Player player;
readonly DeveloperMode devMode;
Dictionary<Actor, int> PowerDrain = new Dictionary<Actor, int>();
readonly Dictionary<Actor, int> powerDrain = new Dictionary<Actor, int>();
[Sync] int totalProvided;
public int PowerProvided { get { return totalProvided; } }
@@ -36,8 +36,8 @@ namespace OpenRA.Mods.RA.Buildings
public PowerManager(ActorInitializer init, PowerManagerInfo info)
{
Info = info;
Player = init.self.Owner;
this.info = info;
player = init.self.Owner;
init.world.ActorAdded += ActorAdded;
init.world.ActorRemoved += ActorRemoved;
@@ -48,17 +48,22 @@ namespace OpenRA.Mods.RA.Buildings
void ActorAdded(Actor a)
{
if (a.Owner != Player || !a.HasTrait<Building>())
if (a.Owner != player)
return;
PowerDrain.Add(a, a.Trait<Building>().GetPowerUsage());
var power = a.TraitOrDefault<Power>();
if (power == null)
return;
powerDrain.Add(a, power.CurrentPower);
UpdateTotals();
}
void ActorRemoved(Actor a)
{
if (a.Owner != Player || !a.HasTrait<Building>())
if (a.Owner != player || !a.HasTrait<Power>())
return;
PowerDrain.Remove(a);
powerDrain.Remove(a);
UpdateTotals();
}
@@ -66,7 +71,7 @@ namespace OpenRA.Mods.RA.Buildings
{
totalProvided = 0;
totalDrained = 0;
foreach (var kv in PowerDrain)
foreach (var kv in powerDrain)
{
var p = kv.Value;
if (p > 0)
@@ -81,10 +86,10 @@ namespace OpenRA.Mods.RA.Buildings
public void UpdateActor(Actor a, int newPower)
{
if (a.Owner != Player || !a.HasTrait<Building>())
if (a.Owner != player || !a.HasTrait<Power>())
return;
PowerDrain[a] = newPower;
powerDrain[a] = newPower;
UpdateTotals();
}
@@ -109,7 +114,7 @@ namespace OpenRA.Mods.RA.Buildings
{
if (lowPower)
Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", "LowPower", self.Owner.Country.Race);
nextPowerAdviceTime = Info.AdviceInterval;
nextPowerAdviceTime = info.AdviceInterval;
}
}