From 488440197e5692b75ba32618a8516e800d19bcab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-R=C3=A9my=20Buchs?= Date: Sat, 3 Jun 2017 00:41:42 +0200 Subject: [PATCH] Add new trait ReloadArmamentsBar which renders a bar to show the current reload status --- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 + .../Traits/Render/ReloadArmamentsBar.cs | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 OpenRA.Mods.Common/Traits/Render/ReloadArmamentsBar.cs diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index eab2a6ecd5..cacb933d83 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -811,6 +811,7 @@ + diff --git a/OpenRA.Mods.Common/Traits/Render/ReloadArmamentsBar.cs b/OpenRA.Mods.Common/Traits/Render/ReloadArmamentsBar.cs new file mode 100644 index 0000000000..204f11e546 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/Render/ReloadArmamentsBar.cs @@ -0,0 +1,60 @@ +#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 System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits.Render +{ + [Desc("Visualizes the minimum remaining time for reloading the armaments.")] + class ReloadArmamentsBarInfo : ITraitInfo + { + [Desc("Armament names")] + public readonly string[] Armaments = { "primary", "secondary" }; + + public readonly Color Color = Color.Red; + + public object Create(ActorInitializer init) { return new ReloadArmamentsBar(init.Self, this); } + } + + class ReloadArmamentsBar : ISelectionBar, INotifyCreated + { + readonly ReloadArmamentsBarInfo info; + readonly Actor self; + IEnumerable armaments; + + public ReloadArmamentsBar(Actor self, ReloadArmamentsBarInfo info) + { + this.self = self; + this.info = info; + } + + void INotifyCreated.Created(Actor self) + { + // Name check can be cached but enabled check can't. + armaments = self.TraitsImplementing().Where(a => info.Armaments.Contains(a.Info.Name)).ToArray().Where(Exts.IsTraitEnabled); + } + + float ISelectionBar.GetValue() + { + if (!self.Owner.IsAlliedWith(self.World.RenderPlayer)) + return 0; + + return armaments.Min(a => a.FireDelay / (float)a.Weapon.ReloadDelay); + } + + Color ISelectionBar.GetColor() { return info.Color; } + bool ISelectionBar.DisplayWhenEmpty { get { return false; } } + } +}