Moves SupportPowers and related Widgets to Mods.Common

This commit is contained in:
reaperrr
2015-01-11 12:29:19 +01:00
parent f897a9ff01
commit be9d37f30e
20 changed files with 37 additions and 41 deletions

View File

@@ -289,6 +289,7 @@
<Compile Include="Traits\Render\RenderSimple.cs" />
<Compile Include="Traits\Render\RenderSprites.cs" />
<Compile Include="Traits\Render\RenderUnit.cs" />
<Compile Include="Traits\Render\SupportPowerChargeBar.cs" />
<Compile Include="Traits\Render\TimedUpgradeBar.cs" />
<Compile Include="Traits\Render\WithBarrel.cs" />
<Compile Include="Traits\Render\WithBuildingExplosion.cs" />
@@ -326,6 +327,12 @@
<Compile Include="Traits\Sound\AnnounceOnKill.cs" />
<Compile Include="Traits\Sound\DeathSounds.cs" />
<Compile Include="Traits\Sound\SoundOnDamageTransition.cs" />
<Compile Include="Traits\SupportPowers\AirstrikePower.cs" />
<Compile Include="Traits\SupportPowers\GrantUpgradePower.cs" />
<Compile Include="Traits\SupportPowers\NukePower.cs" />
<Compile Include="Traits\SupportPowers\SupportPower.cs" />
<Compile Include="Traits\SupportPowers\SupportPowerManager.cs" />
<Compile Include="Traits\SupportPowers\SpawnActorPower.cs" />
<Compile Include="Traits\TargetableUnit.cs" />
<Compile Include="Traits\ThrowsParticle.cs" />
<Compile Include="Traits\Tooltip.cs" />
@@ -401,13 +408,18 @@
<Compile Include="Widgets\Logic\Ingame\LoadIngamePlayerOrObserverUILogic.cs" />
<Compile Include="Widgets\Logic\ModBrowserLogic.cs" />
<Compile Include="Widgets\Logic\ProductionTooltipLogic.cs" />
<Compile Include="Widgets\Logic\SupportPowerTooltipLogic.cs" />
<Compile Include="Widgets\Logic\SupportPowerBinLogic.cs" />
<Compile Include="Widgets\MenuButtonWidget.cs" />
<Compile Include="Widgets\ObserverSupportPowerIconsWidget.cs" />
<Compile Include="Widgets\ProductionPaletteWidget.cs" />
<Compile Include="Widgets\ProductionTabsWidget.cs" />
<Compile Include="Widgets\ProductionTypeButtonWidget.cs" />
<Compile Include="Widgets\RadarWidget.cs" />
<Compile Include="Widgets\ResourceBarWidget.cs" />
<Compile Include="Widgets\StrategicProgressWidget.cs" />
<Compile Include="Widgets\SupportPowersWidget.cs" />
<Compile Include="Widgets\SupportPowerTimerWidget.cs" />
<Compile Include="SpriteLoaders\ShpTDLoader.cs" />
<Compile Include="SpriteLoaders\ShpTSLoader.cs" />
<Compile Include="SpriteLoaders\TmpRALoader.cs" />

View File

@@ -0,0 +1,51 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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.Drawing;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Display the time remaining until the super weapon attached to the actor is ready to the player and his allies.")]
class SupportPowerChargeBarInfo : ITraitInfo
{
public readonly Color Color = Color.Magenta;
public object Create(ActorInitializer init) { return new SupportPowerChargeBar(init.Self, this); }
}
class SupportPowerChargeBar : ISelectionBar
{
readonly Actor self;
readonly SupportPowerChargeBarInfo info;
public SupportPowerChargeBar(Actor self, SupportPowerChargeBarInfo info)
{
this.self = self;
this.info = info;
}
public float GetValue()
{
if (!self.Owner.IsAlliedWith(self.World.RenderPlayer))
return 0;
var spm = self.Owner.PlayerActor.Trait<SupportPowerManager>();
var power = spm.GetPowersForActor(self).FirstOrDefault(sp => !sp.Disabled);
if (power == null) return 0;
return 1 - (float)power.RemainingTime / power.TotalTime;
}
public Color GetColor() { return info.Color; }
}
}

View File

@@ -0,0 +1,182 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class AirstrikePowerInfo : SupportPowerInfo
{
[ActorReference]
public readonly string UnitType = "badr.bomber";
public readonly int SquadSize = 1;
public readonly WVec SquadOffset = new WVec(-1536, 1536, 0);
public readonly int QuantizedFacings = 32;
public readonly WRange Cordon = new WRange(5120);
[ActorReference]
[Desc("Actor to spawn when the aircraft start attacking")]
public readonly string CameraActor = null;
[Desc("Amount of time to keep the camera alive after the aircraft have finished attacking")]
public readonly int CameraRemoveDelay = 25;
[Desc("Weapon range offset to apply during the beacon clock calculation")]
public readonly WRange BeaconDistanceOffset = WRange.FromCells(6);
public override object Create(ActorInitializer init) { return new AirstrikePower(init.Self, this); }
}
public class AirstrikePower : SupportPower
{
public AirstrikePower(Actor self, AirstrikePowerInfo info)
: base(self, info) { }
public override void Activate(Actor self, Order order, SupportPowerManager manager)
{
base.Activate(self, order, manager);
SendAirstrike(self, self.World.Map.CenterOfCell(order.TargetLocation));
}
public void SendAirstrike(Actor self, WPos target, bool randomize = true, int attackFacing = 0)
{
var info = Info as AirstrikePowerInfo;
if (randomize)
attackFacing = Util.QuantizeFacing(self.World.SharedRandom.Next(256), info.QuantizedFacings) * (256 / info.QuantizedFacings);
var altitude = self.World.Map.Rules.Actors[info.UnitType].Traits.Get<PlaneInfo>().CruiseAltitude.Range;
var attackRotation = WRot.FromFacing(attackFacing);
var delta = new WVec(0, -1024, 0).Rotate(attackRotation);
target = target + new WVec(0, 0, altitude);
var startEdge = target - (self.World.Map.DistanceToEdge(target, -delta) + info.Cordon).Range * delta / 1024;
var finishEdge = target + (self.World.Map.DistanceToEdge(target, delta) + info.Cordon).Range * delta / 1024;
Actor camera = null;
Beacon beacon = null;
var aircraftInRange = new Dictionary<Actor, bool>();
Action<Actor> onEnterRange = a =>
{
// Spawn a camera and remove the beacon when the first plane enters the target area
if (info.CameraActor != null && !aircraftInRange.Any(kv => kv.Value))
{
self.World.AddFrameEndTask(w =>
{
camera = w.CreateActor(info.CameraActor, new TypeDictionary
{
new LocationInit(self.World.Map.CellContaining(target)),
new OwnerInit(self.Owner),
});
});
}
if (beacon != null)
{
self.World.AddFrameEndTask(w =>
{
w.Remove(beacon);
beacon = null;
});
}
aircraftInRange[a] = true;
};
Action<Actor> onExitRange = a =>
{
aircraftInRange[a] = false;
// Remove the camera when the final plane leaves the target area
if (!aircraftInRange.Any(kv => kv.Value))
{
if (camera != null)
{
camera.QueueActivity(new Wait(info.CameraRemoveDelay));
camera.QueueActivity(new RemoveSelf());
}
camera = null;
if (beacon != null)
{
self.World.AddFrameEndTask(w =>
{
w.Remove(beacon);
beacon = null;
});
}
}
};
self.World.AddFrameEndTask(w =>
{
var notification = self.Owner.IsAlliedWith(self.World.RenderPlayer) ? Info.LaunchSound : Info.IncomingSound;
Sound.Play(notification);
Actor distanceTestActor = null;
for (var i = -info.SquadSize / 2; i <= info.SquadSize / 2; i++)
{
// Even-sized squads skip the lead plane
if (i == 0 && (info.SquadSize & 1) == 0)
continue;
// Includes the 90 degree rotation between body and world coordinates
var so = info.SquadOffset;
var spawnOffset = new WVec(i * so.Y, -Math.Abs(i) * so.X, 0).Rotate(attackRotation);
var targetOffset = new WVec(i * so.Y, 0, 0).Rotate(attackRotation);
var a = w.CreateActor(info.UnitType, new TypeDictionary
{
new CenterPositionInit(startEdge + spawnOffset),
new OwnerInit(self.Owner),
new FacingInit(attackFacing),
});
var attack = a.Trait<AttackBomber>();
attack.SetTarget(w, target + targetOffset);
attack.OnEnteredAttackRange += onEnterRange;
attack.OnExitedAttackRange += onExitRange;
attack.OnRemovedFromWorld += onExitRange;
a.QueueActivity(new Fly(a, Target.FromPos(target + spawnOffset)));
a.QueueActivity(new Fly(a, Target.FromPos(finishEdge + spawnOffset)));
a.QueueActivity(new RemoveSelf());
aircraftInRange.Add(a, false);
distanceTestActor = a;
}
if (Info.DisplayBeacon)
{
var distance = (target - startEdge).HorizontalLength;
beacon = new Beacon(
self.Owner,
target,
Info.BeaconPalettePrefix,
Info.BeaconPoster,
Info.BeaconPosterPalette,
() => 1 - ((distanceTestActor.CenterPosition - target).HorizontalLength - info.BeaconDistanceOffset.Range) * 1f / distance);
w.Add(beacon);
}
});
}
}
}

View File

@@ -0,0 +1,148 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
class GrantUpgradePowerInfo : SupportPowerInfo
{
[Desc("The upgrades to apply.")]
public readonly string[] Upgrades = { };
[Desc("Duration of the upgrade (in ticks). Set to 0 for a permanent upgrade.")]
public readonly int Duration = 0;
[Desc("Cells")]
public readonly int Range = 1;
public readonly string GrantUpgradeSound = "ironcur9.aud";
public override object Create(ActorInitializer init) { return new GrantUpgradePower(init.Self, this); }
}
class GrantUpgradePower : SupportPower
{
GrantUpgradePowerInfo info;
public GrantUpgradePower(Actor self, GrantUpgradePowerInfo info)
: base(self, info)
{
this.info = info;
}
public override IOrderGenerator OrderGenerator(string order, SupportPowerManager manager)
{
Sound.PlayToPlayer(manager.Self.Owner, Info.SelectTargetSound);
return new SelectTarget(Self.World, order, manager, this);
}
public override void Activate(Actor self, Order order, SupportPowerManager manager)
{
base.Activate(self, order, manager);
self.Trait<RenderBuilding>().PlayCustomAnim(self, "active");
Sound.Play(info.GrantUpgradeSound, self.World.Map.CenterOfCell(order.TargetLocation));
foreach (var a in UnitsInRange(order.TargetLocation))
{
var um = a.TraitOrDefault<UpgradeManager>();
if (um == null)
continue;
foreach (var u in info.Upgrades)
{
if (!um.AcceptsUpgrade(a, u))
continue;
if (info.Duration > 0)
um.GrantTimedUpgrade(a, u, info.Duration);
else
um.GrantUpgrade(a, u, this);
}
}
}
public IEnumerable<Actor> UnitsInRange(CPos xy)
{
var range = info.Range;
var tiles = Self.World.Map.FindTilesInCircle(xy, range);
var units = new List<Actor>();
foreach (var t in tiles)
units.AddRange(Self.World.ActorMap.GetUnitsAt(t));
return units.Distinct().Where(a =>
{
if (!a.Owner.IsAlliedWith(Self.Owner))
return false;
var um = a.TraitOrDefault<UpgradeManager>();
return um != null && info.Upgrades.Any(u => um.AcceptsUpgrade(a, u));
});
}
class SelectTarget : IOrderGenerator
{
readonly GrantUpgradePower power;
readonly int range;
readonly Sprite tile;
readonly SupportPowerManager manager;
readonly string order;
public SelectTarget(World world, string order, SupportPowerManager manager, GrantUpgradePower power)
{
this.manager = manager;
this.order = order;
this.power = power;
this.range = power.info.Range;
tile = world.Map.SequenceProvider.GetSequence("overlay", "target-select").GetSprite(0);
}
public IEnumerable<Order> Order(World world, CPos xy, MouseInput mi)
{
world.CancelInputMode();
if (mi.Button == MouseButton.Left && power.UnitsInRange(xy).Any())
yield return new Order(order, manager.Self, false) { TargetLocation = xy, SuppressVisualFeedback = true };
}
public void Tick(World world)
{
// Cancel the OG if we can't use the power
if (!manager.Powers.ContainsKey(order))
world.CancelInputMode();
}
public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr, World world)
{
var xy = wr.Viewport.ViewToWorld(Viewport.LastMousePos);
foreach (var unit in power.UnitsInRange(xy))
yield return new SelectionBoxRenderable(unit, Color.Red);
}
public IEnumerable<IRenderable> Render(WorldRenderer wr, World world)
{
var xy = wr.Viewport.ViewToWorld(Viewport.LastMousePos);
var pal = wr.Palette("terrain");
foreach (var t in world.Map.FindTilesInCircle(xy, range))
yield return new SpriteRenderable(tile, wr.World.Map.CenterOfCell(t), WVec.Zero, -511, pal, 1f, true);
}
public string GetCursor(World world, CPos xy, MouseInput mi)
{
return power.UnitsInRange(xy).Any() ? "ability" : "move-blocked";
}
}
}
}

View File

@@ -0,0 +1,127 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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;
using OpenRA.Effects;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Effects;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
class NukePowerInfo : SupportPowerInfo, Requires<IBodyOrientationInfo>
{
[WeaponReference]
public readonly string MissileWeapon = "";
public readonly WVec SpawnOffset = WVec.Zero;
[Desc("Travel time - split equally between ascent and descent")]
public readonly int FlightDelay = 400;
[Desc("Visual ascent velocity in WRange / tick")]
public readonly WRange FlightVelocity = new WRange(512);
[Desc("Descend immediately on the target, with half the FlightDelay")]
public readonly bool SkipAscent = false;
[Desc("Amount of time before detonation to remove the beacon")]
public readonly int BeaconRemoveAdvance = 25;
[ActorReference]
[Desc("Actor to spawn before detonation")]
public readonly string CameraActor = null;
[Desc("Amount of time before detonation to spawn the camera")]
public readonly int CameraSpawnAdvance = 25;
[Desc("Amount of time after detonation to remove the camera")]
public readonly int CameraRemoveDelay = 25;
public override object Create(ActorInitializer init) { return new NukePower(init.Self, this); }
}
class NukePower : SupportPower
{
IBodyOrientation body;
public NukePower(Actor self, NukePowerInfo info)
: base(self, info)
{
body = self.Trait<IBodyOrientation>();
}
public override IOrderGenerator OrderGenerator(string order, SupportPowerManager manager)
{
Sound.PlayToPlayer(manager.Self.Owner, Info.SelectTargetSound);
return new SelectGenericPowerTarget(order, manager, "nuke", MouseButton.Left);
}
public override void Activate(Actor self, Order order, SupportPowerManager manager)
{
base.Activate(self, order, manager);
if (self.Owner.IsAlliedWith(self.World.RenderPlayer))
Sound.Play(Info.LaunchSound);
else
Sound.Play(Info.IncomingSound);
var npi = Info as NukePowerInfo;
var rb = self.Trait<RenderSimple>();
rb.PlayCustomAnim(self, "active");
var targetPosition = self.World.Map.CenterOfCell(order.TargetLocation);
var missile = new NukeLaunch(self.Owner, npi.MissileWeapon,
self.CenterPosition + body.LocalToWorld(npi.SpawnOffset),
targetPosition,
npi.FlightVelocity, npi.FlightDelay, npi.SkipAscent);
self.World.AddFrameEndTask(w => w.Add(missile));
if (npi.CameraActor != null)
{
var camera = self.World.CreateActor(false, npi.CameraActor, new TypeDictionary
{
new LocationInit(order.TargetLocation),
new OwnerInit(self.Owner),
});
camera.QueueActivity(new Wait(npi.CameraSpawnAdvance + npi.CameraRemoveDelay));
camera.QueueActivity(new RemoveSelf());
Action addCamera = () => self.World.AddFrameEndTask(w => w.Add(camera));
self.World.AddFrameEndTask(w => w.Add(new DelayedAction(npi.FlightDelay - npi.CameraSpawnAdvance, addCamera)));
}
if (Info.DisplayBeacon)
{
var beacon = new Beacon(
order.Player,
targetPosition,
Info.BeaconPalettePrefix,
Info.BeaconPoster,
Info.BeaconPosterPalette,
() => missile.FractionComplete);
Action removeBeacon = () => self.World.AddFrameEndTask(w =>
{
w.Remove(beacon);
beacon = null;
});
self.World.AddFrameEndTask(w =>
{
w.Add(beacon);
w.Add(new DelayedAction(npi.FlightDelay - npi.BeaconRemoveAdvance, removeBeacon));
});
}
}
}
}

View File

@@ -0,0 +1,67 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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.Effects;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Effects;
using OpenRA.Primitives;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Spawns an actor that stays for a limited amount of time.")]
public class SpawnActorPowerInfo : SupportPowerInfo
{
[Desc("Actor to spawn.")]
public readonly string Actor = null;
[Desc("Amount of time to keep the actor alive in ticks.")]
public readonly int LifeTime = 250;
public readonly string DeploySound = null;
public readonly string EffectSequence = null;
public readonly string EffectPalette = null;
public override object Create(ActorInitializer init) { return new SpawnActorPower(init.Self, this); }
}
public class SpawnActorPower : SupportPower
{
public SpawnActorPower(Actor self, SpawnActorPowerInfo info) : base(self, info) { }
public override void Activate(Actor self, Order order, SupportPowerManager manager)
{
base.Activate(self, order, manager);
var info = Info as SpawnActorPowerInfo;
if (info.Actor != null)
{
self.World.AddFrameEndTask(w =>
{
var location = self.World.Map.CenterOfCell(order.TargetLocation);
Sound.Play(info.DeploySound, location);
if (!string.IsNullOrEmpty(info.EffectSequence) && !string.IsNullOrEmpty(info.EffectPalette))
w.Add(new SpriteEffect(location, w, info.EffectSequence, info.EffectPalette));
var actor = w.CreateActor(info.Actor, new TypeDictionary
{
new LocationInit(order.TargetLocation),
new OwnerInit(self.Owner),
});
actor.QueueActivity(new Wait(info.LifeTime));
actor.QueueActivity(new RemoveSelf());
});
}
}
}
}

View File

@@ -0,0 +1,89 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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.Traits;
namespace OpenRA.Mods.Common.Traits
{
public abstract class SupportPowerInfo : ITraitInfo
{
public readonly int ChargeTime = 0;
public readonly string Icon = null;
public readonly string Description = "";
public readonly string LongDesc = "";
public readonly bool AllowMultiple = false;
public readonly bool OneShot = false;
public readonly string[] Prerequisites = { };
public readonly string BeginChargeSound = null;
public readonly string EndChargeSound = null;
public readonly string SelectTargetSound = null;
public readonly string InsufficientPowerSound = null;
public readonly string LaunchSound = null;
public readonly string IncomingSound = null;
public readonly bool DisplayTimer = false;
[Desc("Beacons are only supported on the Airstrike and Nuke powers")]
public readonly bool DisplayBeacon = false;
public readonly string BeaconPalettePrefix = "player";
public readonly string BeaconPoster = null;
public readonly string BeaconPosterPalette = "chrome";
public readonly bool DisplayRadarPing = false;
public readonly int RadarPingDuration = 5 * 25;
public readonly string OrderName;
public abstract object Create(ActorInitializer init);
public SupportPowerInfo() { OrderName = GetType().Name + "Order"; }
}
public class SupportPower
{
public readonly Actor Self;
public readonly SupportPowerInfo Info;
protected RadarPing ping;
public SupportPower(Actor self, SupportPowerInfo info)
{
Info = info;
Self = self;
}
public virtual void Charging(Actor self, string key)
{
Sound.PlayToPlayer(self.Owner, Info.BeginChargeSound);
}
public virtual void Charged(Actor self, string key)
{
Sound.PlayToPlayer(self.Owner, Info.EndChargeSound);
}
public virtual void Activate(Actor self, Order order, SupportPowerManager manager)
{
if (Info.DisplayRadarPing && manager.RadarPings != null)
{
ping = manager.RadarPings.Value.Add(
() => order.Player.IsAlliedWith(self.World.RenderPlayer),
self.World.Map.CenterOfCell(order.TargetLocation),
order.Player.Color.RGB,
Info.RadarPingDuration);
}
}
public virtual IOrderGenerator OrderGenerator(string order, SupportPowerManager manager)
{
Sound.PlayToPlayer(manager.Self.Owner, Info.SelectTargetSound);
return new SelectGenericPowerTarget(order, manager, "ability", MouseButton.Left);
}
}
}

View File

@@ -0,0 +1,266 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Attach this to the player actor.")]
public class SupportPowerManagerInfo : ITraitInfo, Requires<DeveloperModeInfo>, Requires<TechTreeInfo>
{
public object Create(ActorInitializer init) { return new SupportPowerManager(init); }
}
public class SupportPowerManager : ITick, IResolveOrder, ITechTreeElement
{
public readonly Actor Self;
public readonly Dictionary<string, SupportPowerInstance> Powers = new Dictionary<string, SupportPowerInstance>();
public readonly DeveloperMode DevMode;
public readonly TechTree TechTree;
public readonly Lazy<RadarPings> RadarPings;
public SupportPowerManager(ActorInitializer init)
{
Self = init.Self;
DevMode = Self.Trait<DeveloperMode>();
TechTree = Self.Trait<TechTree>();
RadarPings = Exts.Lazy(() => init.World.WorldActor.TraitOrDefault<RadarPings>());
init.World.ActorAdded += ActorAdded;
init.World.ActorRemoved += ActorRemoved;
}
static string MakeKey(SupportPower sp)
{
return sp.Info.AllowMultiple ? sp.Info.OrderName + "_" + sp.Self.ActorID : sp.Info.OrderName;
}
void ActorAdded(Actor a)
{
if (a.Owner != Self.Owner)
return;
foreach (var t in a.TraitsImplementing<SupportPower>())
{
var key = MakeKey(t);
if (!Powers.ContainsKey(key))
{
Powers.Add(key, new SupportPowerInstance(key, this)
{
Instances = new List<SupportPower>(),
RemainingTime = t.Info.ChargeTime * 25,
TotalTime = t.Info.ChargeTime * 25,
});
if (t.Info.Prerequisites.Any())
{
TechTree.Add(key, t.Info.Prerequisites, 0, this);
TechTree.Update();
}
}
Powers[key].Instances.Add(t);
}
}
void ActorRemoved(Actor a)
{
if (a.Owner != Self.Owner || !a.HasTrait<SupportPower>())
return;
foreach (var t in a.TraitsImplementing<SupportPower>())
{
var key = MakeKey(t);
Powers[key].Instances.Remove(t);
if (Powers[key].Instances.Count == 0 && !Powers[key].Disabled)
{
Powers.Remove(key);
TechTree.Remove(key);
TechTree.Update();
}
}
}
public void Tick(Actor self)
{
foreach (var power in Powers.Values)
power.Tick();
}
public void ResolveOrder(Actor self, Order order)
{
// order.OrderString is the key of the support power
if (Powers.ContainsKey(order.OrderString))
Powers[order.OrderString].Activate(order);
}
// Deprecated. Remove after SupportPowerBinWidget is removed.
public void Target(string key)
{
if (Powers.ContainsKey(key))
Powers[key].Target();
}
static readonly SupportPowerInstance[] NoInstances = { };
public IEnumerable<SupportPowerInstance> GetPowersForActor(Actor a)
{
if (a.Owner != Self.Owner || !a.HasTrait<SupportPower>())
return NoInstances;
return a.TraitsImplementing<SupportPower>()
.Select(t => Powers[MakeKey(t)]);
}
public void PrerequisitesAvailable(string key)
{
SupportPowerInstance sp;
if (!Powers.TryGetValue(key, out sp))
return;
sp.Disabled = false;
}
public void PrerequisitesUnavailable(string key)
{
SupportPowerInstance sp;
if (!Powers.TryGetValue(key, out sp))
return;
sp.Disabled = true;
sp.RemainingTime = sp.TotalTime;
}
public void PrerequisitesItemHidden(string key) { }
public void PrerequisitesItemVisible(string key) { }
}
public class SupportPowerInstance
{
readonly SupportPowerManager manager;
readonly string key;
public List<SupportPower> Instances;
public int RemainingTime;
public int TotalTime;
public bool Active { get; private set; }
public bool Disabled { get; set; }
public SupportPowerInfo Info { get { return Instances.Select(i => i.Info).FirstOrDefault(); } }
public bool Ready { get { return Active && RemainingTime == 0; } }
public SupportPowerInstance(string key, SupportPowerManager manager)
{
this.manager = manager;
this.key = key;
}
static bool InstanceDisabled(SupportPower sp)
{
return sp.Self.TraitsImplementing<IDisable>().Any(d => d.Disabled);
}
bool notifiedCharging;
bool notifiedReady;
public void Tick()
{
Active = !Disabled && Instances.Any(i => !i.Self.IsDisabled());
if (!Active)
return;
if (Active)
{
var power = Instances.First();
if (manager.DevMode.FastCharge && RemainingTime > 25)
RemainingTime = 25;
if (RemainingTime > 0)
--RemainingTime;
if (!notifiedCharging)
{
power.Charging(power.Self, key);
notifiedCharging = true;
}
if (RemainingTime == 0
&& !notifiedReady)
{
power.Charged(power.Self, key);
notifiedReady = true;
}
}
}
public void Target()
{
if (!Ready)
return;
manager.Self.World.OrderGenerator = Instances.First().OrderGenerator(key, manager);
}
public void Activate(Order order)
{
if (!Ready)
return;
var power = Instances.First(i => !InstanceDisabled(i));
// Note: order.Subject is the *player* actor
power.Activate(power.Self, order, manager);
RemainingTime = TotalTime;
notifiedCharging = notifiedReady = false;
if (Info.OneShot)
Disabled = true;
}
}
public class SelectGenericPowerTarget : IOrderGenerator
{
readonly SupportPowerManager manager;
readonly string order;
readonly string cursor;
readonly MouseButton expectedButton;
public SelectGenericPowerTarget(string order, SupportPowerManager manager, string cursor, MouseButton button)
{
this.manager = manager;
this.order = order;
this.cursor = cursor;
expectedButton = button;
}
public IEnumerable<Order> Order(World world, CPos xy, MouseInput mi)
{
world.CancelInputMode();
if (mi.Button == expectedButton && world.Map.Contains(xy))
yield return new Order(order, manager.Self, false) { TargetLocation = xy, SuppressVisualFeedback = true };
}
public virtual void Tick(World world)
{
// Cancel the OG if we can't use the power
if (!manager.Powers.ContainsKey(order))
world.CancelInputMode();
}
public IEnumerable<IRenderable> Render(WorldRenderer wr, World world) { yield break; }
public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr, World world) { yield break; }
public string GetCursor(World world, CPos xy, MouseInput mi) { return world.Map.Contains(xy) ? cursor : "generic-blocked"; }
}
}

View File

@@ -0,0 +1,72 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public class SupportPowerBinLogic
{
[ObjectCreator.UseCtor]
public SupportPowerBinLogic(Widget widget, World world)
{
var palette = widget.Get<SupportPowersWidget>("SUPPORT_PALETTE");
var background = widget.GetOrNull("PALETTE_BACKGROUND");
var foreground = widget.GetOrNull("PALETTE_FOREGROUND");
if (background != null || foreground != null)
{
Widget backgroundTemplate = null;
Widget foregroundTemplate = null;
if (background != null)
backgroundTemplate = background.Get("ICON_TEMPLATE");
if (foreground != null)
foregroundTemplate = foreground.Get("ICON_TEMPLATE");
Action<int, int> updateBackground = (_, icons) =>
{
var rowHeight = palette.IconSize.Y + palette.IconMargin;
if (background != null)
{
background.RemoveChildren();
for (var i = 0; i < icons; i++)
{
var row = backgroundTemplate.Clone();
row.Bounds.Y += i * rowHeight;
background.AddChild(row);
}
}
if (foreground != null)
{
foreground.RemoveChildren();
for (var i = 0; i < icons; i++)
{
var row = foregroundTemplate.Clone();
row.Bounds.Y += i * rowHeight;
foreground.AddChild(row);
}
}
};
palette.OnIconCountChanged += updateBackground;
// Set the initial palette state
updateBackground(0, 0);
}
}
}
}

View File

@@ -0,0 +1,67 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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;
using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public class SupportPowerTooltipLogic
{
[ObjectCreator.UseCtor]
public SupportPowerTooltipLogic(Widget widget, TooltipContainerWidget tooltipContainer, SupportPowersWidget palette)
{
widget.IsVisible = () => palette.TooltipPower != null;
var nameLabel = widget.Get<LabelWidget>("NAME");
var timeLabel = widget.Get<LabelWidget>("TIME");
var descLabel = widget.Get<LabelWidget>("DESC");
var nameFont = Game.Renderer.Fonts[nameLabel.Font];
var timeFont = Game.Renderer.Fonts[timeLabel.Font];
var descFont = Game.Renderer.Fonts[descLabel.Font];
var name = "";
var time = "";
var desc = "";
var baseHeight = widget.Bounds.Height;
var timeOffset = timeLabel.Bounds.X;
SupportPowerInstance lastPower = null;
tooltipContainer.BeforeRender = () =>
{
var sp = palette.TooltipPower;
if (sp == null)
return;
if (sp.Info == null)
return; // no instances actually exist (race with destroy)
time = "{0} / {1}".F(WidgetUtils.FormatTime(sp.RemainingTime),
WidgetUtils.FormatTime(sp.Info.ChargeTime * 25));
if (sp == lastPower)
return;
name = sp.Info.Description;
desc = sp.Info.LongDesc.Replace("\\n", "\n");
var timeWidth = timeFont.Measure(time).X;
var topWidth = nameFont.Measure(name).X + timeWidth + timeOffset;
var descSize = descFont.Measure(desc);
widget.Bounds.Width = 2 * nameLabel.Bounds.X + Math.Max(topWidth, descSize.X);
widget.Bounds.Height = baseHeight + descSize.Y;
timeLabel.Bounds.X = widget.Bounds.Width - nameLabel.Bounds.X - timeWidth;
lastPower = sp;
};
nameLabel.GetText = () => name;
timeLabel.GetText = () => time;
descLabel.GetText = () => desc;
}
}
}

View File

@@ -0,0 +1,108 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets
{
public class ObserverSupportPowerIconsWidget : Widget
{
public Func<Player> GetPlayer;
Animation icon;
World world;
WorldRenderer worldRenderer;
Dictionary<string, Animation> clocks;
public int IconWidth = 32;
public int IconHeight = 24;
public int IconSpacing = 8;
[ObjectCreator.UseCtor]
public ObserverSupportPowerIconsWidget(World world, WorldRenderer worldRenderer)
{
this.world = world;
this.worldRenderer = worldRenderer;
clocks = new Dictionary<string, Animation>();
icon = new Animation(world, "icon");
}
protected ObserverSupportPowerIconsWidget(ObserverSupportPowerIconsWidget other)
: base(other)
{
GetPlayer = other.GetPlayer;
icon = other.icon;
world = other.world;
worldRenderer = other.worldRenderer;
clocks = other.clocks;
}
public override void Draw()
{
var player = GetPlayer();
if (player == null)
{
return;
}
var powers = player.PlayerActor.Trait<SupportPowerManager>().Powers
.Select((a, i) => new { a, i });
foreach (var power in powers)
{
if (!clocks.ContainsKey(power.a.Key))
{
clocks.Add(power.a.Key, new Animation(world, "clock"));
}
}
var iconSize = new float2(IconWidth, IconHeight);
foreach (var power in powers)
{
var item = power.a.Value;
if (item == null || item.Info == null || item.Info.Icon == null)
continue;
icon.Play(item.Info.Icon);
var location = new float2(RenderBounds.Location) + new float2(power.i * (IconWidth + IconSpacing), 0);
WidgetUtils.DrawSHPCentered(icon.Image, location + 0.5f * iconSize, worldRenderer, 0.5f);
var clock = clocks[power.a.Key];
clock.PlayFetchIndex("idle",
() => item.TotalTime == 0 ? 0 : ((item.TotalTime - item.RemainingTime)
* (clock.CurrentSequence.Length - 1) / item.TotalTime));
clock.Tick();
WidgetUtils.DrawSHPCentered(clock.Image, location + 0.5f * iconSize, worldRenderer, 0.5f);
var tiny = Game.Renderer.Fonts["Tiny"];
var text = GetOverlayForItem(item);
tiny.DrawTextWithContrast(text,
location + new float2(16, 16) - new float2(tiny.Measure(text).X / 2, 0),
Color.White, Color.Black, 1);
}
}
static string GetOverlayForItem(SupportPowerInstance item)
{
if (item.Disabled) return "ON HOLD";
if (item.Ready) return "READY";
return WidgetUtils.FormatTime(item.RemainingTime);
}
public override Widget Clone()
{
return new ObserverSupportPowerIconsWidget(this);
}
}
}

View File

@@ -0,0 +1,65 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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.Drawing;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets
{
public class SupportPowerTimerWidget : Widget
{
public readonly string Font = "Bold";
public readonly string Format = "{0}: {1}";
public readonly TimerOrder Order = TimerOrder.Descending;
readonly IEnumerable<SupportPowerInstance> powers;
Pair<string, Color>[] texts;
[ObjectCreator.UseCtor]
public SupportPowerTimerWidget(World world)
{
powers = world.ActorsWithTrait<SupportPowerManager>()
.Where(p => !p.Actor.IsDead && !p.Actor.Owner.NonCombatant)
.SelectMany(s => s.Trait.Powers.Values)
.Where(p => p.Instances.Any() && p.Info.DisplayTimer && !p.Disabled);
}
public override void Tick()
{
texts = powers.Select(p =>
{
var time = WidgetUtils.FormatTime(p.RemainingTime, false);
var text = Format.F(p.Info.Description, time);
var color = !p.Ready || Game.LocalTick % 50 < 25 ? p.Instances[0].Self.Owner.Color.RGB : Color.White;
return Pair.New(text, color);
}).ToArray();
}
public override void Draw()
{
if (!IsVisible() || texts == null)
return;
var y = 0;
foreach (var t in texts)
{
var font = Game.Renderer.Fonts[Font];
font.DrawTextWithContrast(t.First, new float2(Bounds.Location) + new float2(0, y), t.Second, Color.Black, 1);
y += (font.Measure(t.First).Y + 5) * (int)Order;
}
}
public enum TimerOrder { Ascending = -1, Descending = 1 }
}
}

View File

@@ -0,0 +1,194 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets
{
public class SupportPowersWidget : Widget
{
[Translate] public readonly string ReadyText = "";
[Translate] public readonly string HoldText = "";
public readonly int2 IconSize = new int2(64, 48);
public readonly int IconMargin = 10;
public readonly int2 IconSpriteOffset = int2.Zero;
public readonly string TooltipContainer;
public readonly string TooltipTemplate = "SUPPORT_POWER_TOOLTIP";
public int IconCount { get; private set; }
public event Action<int, int> OnIconCountChanged = (a, b) => { };
readonly WorldRenderer worldRenderer;
readonly SupportPowerManager spm;
Animation icon;
Animation clock;
Dictionary<Rectangle, SupportPowerIcon> icons = new Dictionary<Rectangle, SupportPowerIcon>();
public SupportPowerInstance TooltipPower { get; private set; }
Lazy<TooltipContainerWidget> tooltipContainer;
Rectangle eventBounds;
public override Rectangle EventBounds { get { return eventBounds; } }
SpriteFont overlayFont;
float2 holdOffset, readyOffset, timeOffset;
[ObjectCreator.UseCtor]
public SupportPowersWidget(World world, WorldRenderer worldRenderer)
{
this.worldRenderer = worldRenderer;
spm = world.LocalPlayer.PlayerActor.Trait<SupportPowerManager>();
tooltipContainer = Exts.Lazy(() =>
Ui.Root.Get<TooltipContainerWidget>(TooltipContainer));
icon = new Animation(world, "icon");
clock = new Animation(world, "clock");
}
public class SupportPowerIcon
{
public SupportPowerInstance Power;
public float2 Pos;
public Sprite Sprite;
}
public void RefreshIcons()
{
icons = new Dictionary<Rectangle, SupportPowerIcon>();
var powers = spm.Powers.Values.Where(p => !p.Disabled);
var oldIconCount = IconCount;
IconCount = 0;
var rb = RenderBounds;
foreach (var p in powers)
{
var rect = new Rectangle(rb.X, rb.Y + IconCount * (IconSize.Y + IconMargin), IconSize.X, IconSize.Y);
icon.Play(p.Info.Icon);
var power = new SupportPowerIcon()
{
Power = p,
Pos = new float2(rect.Location),
Sprite = icon.Image
};
icons.Add(rect, power);
IconCount++;
}
eventBounds = icons.Any() ? icons.Keys.Aggregate(Rectangle.Union) : Rectangle.Empty;
if (oldIconCount != IconCount)
OnIconCountChanged(oldIconCount, IconCount);
}
public override void Draw()
{
var iconOffset = 0.5f * IconSize.ToFloat2() + IconSpriteOffset;
overlayFont = Game.Renderer.Fonts["TinyBold"];
holdOffset = iconOffset - overlayFont.Measure(HoldText) / 2;
readyOffset = iconOffset - overlayFont.Measure(ReadyText) / 2;
timeOffset = iconOffset - overlayFont.Measure(WidgetUtils.FormatTime(0)) / 2;
// Icons
foreach (var p in icons.Values)
{
WidgetUtils.DrawSHPCentered(p.Sprite, p.Pos + iconOffset, worldRenderer);
// Charge progress
var sp = p.Power;
clock.PlayFetchIndex("idle",
() => sp.TotalTime == 0 ? clock.CurrentSequence.Length - 1 : (sp.TotalTime - sp.RemainingTime)
* (clock.CurrentSequence.Length - 1) / sp.TotalTime);
clock.Tick();
WidgetUtils.DrawSHPCentered(clock.Image, p.Pos + iconOffset, worldRenderer);
}
// Overlay
foreach (var p in icons.Values)
{
if (p.Power.Ready)
overlayFont.DrawTextWithContrast(ReadyText,
p.Pos + readyOffset,
Color.White, Color.Black, 1);
else if (!p.Power.Active)
overlayFont.DrawTextWithContrast(HoldText,
p.Pos + holdOffset,
Color.White, Color.Black, 1);
else
overlayFont.DrawTextWithContrast(WidgetUtils.FormatTime(p.Power.RemainingTime),
p.Pos + timeOffset,
Color.White, Color.Black, 1);
}
}
public override void Tick()
{
// TODO: Only do this when the powers have changed
RefreshIcons();
}
public override void MouseEntered()
{
if (TooltipContainer == null)
return;
tooltipContainer.Value.SetTooltip(TooltipTemplate,
new WidgetArgs() { { "palette", this } });
}
public override void MouseExited()
{
if (TooltipContainer == null)
return;
tooltipContainer.Value.RemoveTooltip();
}
public override bool HandleMouseInput(MouseInput mi)
{
if (mi.Event == MouseInputEvent.Move)
{
var icon = icons.Where(i => i.Key.Contains(mi.Location))
.Select(i => i.Value).FirstOrDefault();
TooltipPower = icon != null ? icon.Power : null;
return false;
}
if (mi.Event != MouseInputEvent.Down)
return false;
var clicked = icons.Where(i => i.Key.Contains(mi.Location))
.Select(i => i.Value).FirstOrDefault();
if (clicked != null)
{
if (!clicked.Power.Active)
Sound.PlayToPlayer(spm.Self.Owner, clicked.Power.Info.InsufficientPowerSound);
clicked.Power.Target();
}
return true;
}
}
}