From ed7b54a21ee20ed19506f01139b9fc71ce99742d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 5 Apr 2015 18:15:45 +0200 Subject: [PATCH] add a Lua controllable flash palette trait --- OpenRA.Mods.Common/Effects/NukeLaunch.cs | 9 ++- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 3 +- .../Scripting/Global/EffectGlobal.cs | 36 +++++++++ .../PaletteEffects/FlashPaletteEffect.cs | 81 +++++++++++++++++++ .../PaletteEffects/NukePaletteEffect.cs | 57 ------------- .../Traits/SupportPowers/NukePower.cs | 5 +- .../UtilityCommands/UpgradeRules.cs | 7 ++ mods/cnc/rules/palettes.yaml | 2 +- mods/cnc/rules/world.yaml | 1 - mods/d2k/rules/palettes.yaml | 2 +- mods/ra/rules/palettes.yaml | 3 +- mods/ra/rules/structures.yaml | 1 + 12 files changed, 141 insertions(+), 66 deletions(-) create mode 100644 OpenRA.Mods.Common/Scripting/Global/EffectGlobal.cs create mode 100644 OpenRA.Mods.Common/Traits/PaletteEffects/FlashPaletteEffect.cs delete mode 100644 OpenRA.Mods.Common/Traits/PaletteEffects/NukePaletteEffect.cs diff --git a/OpenRA.Mods.Common/Effects/NukeLaunch.cs b/OpenRA.Mods.Common/Effects/NukeLaunch.cs index d1915ad396..3d7ca5abfc 100644 --- a/OpenRA.Mods.Common/Effects/NukeLaunch.cs +++ b/OpenRA.Mods.Common/Effects/NukeLaunch.cs @@ -23,6 +23,7 @@ namespace OpenRA.Mods.Common.Effects readonly Player firedBy; readonly Animation anim; readonly string weapon; + readonly string flashType; readonly WPos ascendSource; readonly WPos ascendTarget; @@ -34,12 +35,13 @@ namespace OpenRA.Mods.Common.Effects WPos pos; int ticks; - public NukeLaunch(Player firedBy, string weapon, WPos launchPos, WPos targetPos, WRange velocity, int delay, bool skipAscent) + public NukeLaunch(Player firedBy, string weapon, WPos launchPos, WPos targetPos, WRange velocity, int delay, bool skipAscent, string flashType) { this.firedBy = firedBy; this.weapon = weapon; this.delay = delay; this.turn = delay / 2; + this.flashType = flashType; var offset = new WVec(WRange.Zero, WRange.Zero, velocity * turn); ascendSource = launchPos; @@ -84,8 +86,9 @@ namespace OpenRA.Mods.Common.Effects weapon.Impact(Target.FromPos(pos), firedBy.PlayerActor, Enumerable.Empty()); world.WorldActor.Trait().AddEffect(20, pos, 5); - foreach (var a in world.ActorsWithTrait()) - a.Trait.Enable(); + foreach (var flash in world.WorldActor.TraitsImplementing()) + if (flash.Info.Type == flashType) + flash.Enable(-1); } public IEnumerable Render(WorldRenderer wr) diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 5fa108d39f..3e6f22c4c5 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -215,6 +215,7 @@ + @@ -357,8 +358,8 @@ + - diff --git a/OpenRA.Mods.Common/Scripting/Global/EffectGlobal.cs b/OpenRA.Mods.Common/Scripting/Global/EffectGlobal.cs new file mode 100644 index 0000000000..e4669aade6 --- /dev/null +++ b/OpenRA.Mods.Common/Scripting/Global/EffectGlobal.cs @@ -0,0 +1,36 @@ +#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 OpenRA.Mods.Common.Traits; +using OpenRA.Scripting; + +namespace OpenRA.Mods.Common.Scripting +{ + [ScriptGlobal("Effect")] + public class EffectGlobal : ScriptGlobal + { + readonly IEnumerable fpes; + + public EffectGlobal(ScriptContext context) + : base(context) + { + fpes = context.World.WorldActor.TraitsImplementing(); + } + + [Desc("Controls the `FlashPaletteEffect` trait.")] + public void Flash(string type = null, int ticks = -1) + { + foreach (var fpe in fpes) + if (fpe.Info.Type == type) + fpe.Enable(ticks); + } + } +} diff --git a/OpenRA.Mods.Common/Traits/PaletteEffects/FlashPaletteEffect.cs b/OpenRA.Mods.Common/Traits/PaletteEffects/FlashPaletteEffect.cs new file mode 100644 index 0000000000..0b832e74d1 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/PaletteEffects/FlashPaletteEffect.cs @@ -0,0 +1,81 @@ +#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 OpenRA.Graphics; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + using GUtil = OpenRA.Graphics.Util; + + [Desc("Used for bursted one-colored whole screen effects. Add this to the world actor.")] + public class FlashPaletteEffectInfo : ITraitInfo + { + public readonly string[] ExcludePalettes = { "cursor", "chrome", "colorpicker", "fog", "shroud" }; + + [Desc("Measured in ticks.")] + public readonly int Length = 20; + + public readonly Color Color = Color.White; + + [Desc("Set this when using multiple independent flash effects.")] + public readonly string Type = null; + + public object Create(ActorInitializer init) { return new FlashPaletteEffect(this); } + } + + public class FlashPaletteEffect : IPaletteModifier, ITick + { + public readonly FlashPaletteEffectInfo Info; + + public FlashPaletteEffect(FlashPaletteEffectInfo info) + { + Info = info; + } + + int remainingFrames; + + public void Enable(int ticks) + { + if (ticks == -1) + remainingFrames = Info.Length; + else + remainingFrames = ticks; + } + + public void Tick(Actor self) + { + if (remainingFrames > 0) + remainingFrames--; + } + + public void AdjustPalette(IReadOnlyDictionary palettes) + { + if (remainingFrames == 0) + return; + + var frac = (float)remainingFrames / Info.Length; + + foreach (var pal in palettes) + { + for (var x = 0; x < Palette.Size; x++) + { + var orig = pal.Value.GetColor(x); + var c = Info.Color; + var color = Color.FromArgb(orig.A, ((int)c.R).Clamp(0, 255), ((int)c.G).Clamp(0, 255), ((int)c.B).Clamp(0, 255)); + var final = GUtil.PremultipliedColorLerp(frac, orig, GUtil.PremultiplyAlpha(Color.FromArgb(orig.A, color))); + pal.Value.SetColor(x, final); + } + } + } + } +} diff --git a/OpenRA.Mods.Common/Traits/PaletteEffects/NukePaletteEffect.cs b/OpenRA.Mods.Common/Traits/PaletteEffects/NukePaletteEffect.cs deleted file mode 100644 index 05199a1f97..0000000000 --- a/OpenRA.Mods.Common/Traits/PaletteEffects/NukePaletteEffect.cs +++ /dev/null @@ -1,57 +0,0 @@ -#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 OpenRA.Graphics; -using OpenRA.Traits; - -namespace OpenRA.Mods.Common.Traits -{ - using GUtil = OpenRA.Graphics.Util; - - [Desc("Apply palette full screen rotations during atom bomb explosions. Add this to the world actor.")] - class NukePaletteEffectInfo : TraitInfo { } - - public class NukePaletteEffect : IPaletteModifier, ITick - { - const int NukeEffectLength = 20; - int remainingFrames; - - public void Enable() - { - remainingFrames = NukeEffectLength; - } - - public void Tick(Actor self) - { - if (remainingFrames > 0) - remainingFrames--; - } - - public void AdjustPalette(IReadOnlyDictionary palettes) - { - if (remainingFrames == 0) - return; - - var frac = (float)remainingFrames / NukeEffectLength; - - foreach (var pal in palettes) - { - for (var x = 0; x < Palette.Size; x++) - { - var orig = pal.Value.GetColor(x); - var final = GUtil.PremultipliedColorLerp(frac, orig, GUtil.PremultiplyAlpha(Color.FromArgb(orig.A, Color.White))); - pal.Value.SetColor(x, final); - } - } - } - } -} diff --git a/OpenRA.Mods.Common/Traits/SupportPowers/NukePower.cs b/OpenRA.Mods.Common/Traits/SupportPowers/NukePower.cs index 63cd0042ba..fee57d8000 100644 --- a/OpenRA.Mods.Common/Traits/SupportPowers/NukePower.cs +++ b/OpenRA.Mods.Common/Traits/SupportPowers/NukePower.cs @@ -45,6 +45,8 @@ namespace OpenRA.Mods.Common.Traits [Desc("Amount of time after detonation to remove the camera")] public readonly int CameraRemoveDelay = 25; + public readonly string FlashType = null; + public override object Create(ActorInitializer init) { return new NukePower(init.Self, this); } } @@ -76,7 +78,8 @@ namespace OpenRA.Mods.Common.Traits var missile = new NukeLaunch(self.Owner, info.MissileWeapon, self.CenterPosition + body.LocalToWorld(info.SpawnOffset), targetPosition, - info.FlightVelocity, info.FlightDelay, info.SkipAscent); + info.FlightVelocity, info.FlightDelay, info.SkipAscent, + info.FlashType); self.World.AddFrameEndTask(w => w.Add(missile)); diff --git a/OpenRA.Mods.Common/UtilityCommands/UpgradeRules.cs b/OpenRA.Mods.Common/UtilityCommands/UpgradeRules.cs index f2d2f23de9..21c1c678e7 100644 --- a/OpenRA.Mods.Common/UtilityCommands/UpgradeRules.cs +++ b/OpenRA.Mods.Common/UtilityCommands/UpgradeRules.cs @@ -1353,6 +1353,13 @@ namespace OpenRA.Mods.Common.UtilityCommands } } + // Generalized the flash palette trait + if (engineVersion < 20150627) + { + if (node.Key == "NukePaletteEffect") + node.Key = "FlashPaletteEffect"; + } + UpgradeActorRules(engineVersion, ref node.Value.Nodes, node, depth + 1); } } diff --git a/mods/cnc/rules/palettes.yaml b/mods/cnc/rules/palettes.yaml index e6d4c8a4c7..ad0081679a 100644 --- a/mods/cnc/rules/palettes.yaml +++ b/mods/cnc/rules/palettes.yaml @@ -70,7 +70,7 @@ MenuPaletteEffect: MenuEffect: Desaturated CloakPaletteEffect: - NukePaletteEffect: + FlashPaletteEffect: WaterPaletteRotation: ExcludePalettes: effect diff --git a/mods/cnc/rules/world.yaml b/mods/cnc/rules/world.yaml index 3694600a82..f27ea40b00 100644 --- a/mods/cnc/rules/world.yaml +++ b/mods/cnc/rules/world.yaml @@ -54,7 +54,6 @@ World: PlayerCommands: HelpCommand: ScreenShaker: - NukePaletteEffect: BuildingInfluence: BridgeLayer: Bridges: bridge1, bridge2, bridge3, bridge4 diff --git a/mods/d2k/rules/palettes.yaml b/mods/d2k/rules/palettes.yaml index b32bb54553..159dd94f7e 100644 --- a/mods/d2k/rules/palettes.yaml +++ b/mods/d2k/rules/palettes.yaml @@ -86,5 +86,5 @@ Alpha: 0.68 Premultiply: false PlayerHighlightPalette: - NukePaletteEffect: + FlashPaletteEffect: diff --git a/mods/ra/rules/palettes.yaml b/mods/ra/rules/palettes.yaml index fbdcc159e8..70eb65a5f2 100644 --- a/mods/ra/rules/palettes.yaml +++ b/mods/ra/rules/palettes.yaml @@ -70,5 +70,6 @@ LightPaletteRotator: ExcludePalettes: terrain, effect ChronoshiftPaletteEffect: - NukePaletteEffect: + FlashPaletteEffect@NUKE: + Type: Nuke diff --git a/mods/ra/rules/structures.yaml b/mods/ra/rules/structures.yaml index bd4f760f69..5436180498 100644 --- a/mods/ra/rules/structures.yaml +++ b/mods/ra/rules/structures.yaml @@ -38,6 +38,7 @@ MSLO: DisplayRadarPing: True BeaconPoster: atomicon CameraActor: camera + FlashType: Nuke CanPowerDown: RequiresPower: DisabledOverlay: