From 82059dca6dd982377cc5bd3049d3b4120e1fa67e Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 16 Jun 2013 19:25:05 +1200 Subject: [PATCH] Add BeamRenderable for lasers. --- OpenRA.Game/Graphics/BeamRenderable.cs | 68 ++++++++++++++++++++++++++ OpenRA.Game/OpenRA.Game.csproj | 1 + OpenRA.Mods.RA/Effects/LaserZap.cs | 31 ++++-------- 3 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 OpenRA.Game/Graphics/BeamRenderable.cs diff --git a/OpenRA.Game/Graphics/BeamRenderable.cs b/OpenRA.Game/Graphics/BeamRenderable.cs new file mode 100644 index 0000000000..49bf882792 --- /dev/null +++ b/OpenRA.Game/Graphics/BeamRenderable.cs @@ -0,0 +1,68 @@ +#region Copyright & License Information +/* + * Copyright 2007-2013 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; + +namespace OpenRA.Graphics +{ + public struct BeamRenderable : IRenderable + { + readonly WPos pos; + readonly int zOffset; + readonly WVec length; + readonly Color color; + readonly float width; + + public BeamRenderable(WPos pos, int zOffset, WVec length, float width, Color color) + { + this.pos = pos; + this.zOffset = zOffset; + this.length = length; + this.color = color; + this.width = width; + } + + public WPos Pos { get { return pos; } } + public float Scale { get { return 1f; } } + public PaletteReference Palette { get { return null; } } + public int ZOffset { get { return zOffset; } } + + public IRenderable WithScale(float newScale) { return new BeamRenderable(pos, zOffset, length, width, color); } + public IRenderable WithPalette(PaletteReference newPalette) { return new BeamRenderable(pos, zOffset, length, width, color); } + public IRenderable WithZOffset(int newOffset) { return new BeamRenderable(pos, zOffset, length, width, color); } + public IRenderable WithPos(WPos pos) { return new BeamRenderable(pos, zOffset, length, width, color); } + + public void BeforeRender(WorldRenderer wr) {} + public void Render(WorldRenderer wr) + { + var wlr = Game.Renderer.WorldLineRenderer; + var src = wr.ScreenPosition(pos); + var dest = wr.ScreenPosition(pos + length); + + var lineWidth = wlr.LineWidth; + if (lineWidth != width) + { + wlr.Flush(); + wlr.LineWidth = width; + } + + wlr.DrawLine(src, dest, color, color); + + if (lineWidth != width) + { + wlr.Flush(); + wlr.LineWidth = lineWidth; + } + } + + public void RenderDebugGeometry(WorldRenderer wr) {} + } +} diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 36357505a1..d2f7dea76d 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -234,6 +234,7 @@ + diff --git a/OpenRA.Mods.RA/Effects/LaserZap.cs b/OpenRA.Mods.RA/Effects/LaserZap.cs index d98ff53a68..8514f80a97 100755 --- a/OpenRA.Mods.RA/Effects/LaserZap.cs +++ b/OpenRA.Mods.RA/Effects/LaserZap.cs @@ -78,34 +78,21 @@ namespace OpenRA.Mods.RA.Effects public IEnumerable Render(WorldRenderer wr) { + if (ticks < info.BeamDuration) + { + var src = new PPos(args.src.X, args.src.Y).ToWPos(args.srcAltitude); + var dest = new PPos(args.dest.X, args.dest.Y).ToWPos(args.destAltitude); + var rc = Color.FromArgb((info.BeamDuration - ticks)*255/info.BeamDuration, color); + + yield return new BeamRenderable(src, 0, dest - src, info.BeamWidth, rc); + } + if (hitanim != null) yield return new SpriteRenderable(hitanim.Image, args.dest.ToFloat2(), wr.Palette("effect"), (int)args.dest.Y); if (ticks >= info.BeamDuration) yield break; - - var rc = Color.FromArgb((info.BeamDuration - ticks)*255/info.BeamDuration, color); - - var src = new PPos(args.src.X, args.src.Y - args.srcAltitude); - var dest = new PPos(args.dest.X, args.dest.Y - args.destAltitude); - var wlr = Game.Renderer.WorldLineRenderer; - - // TODO: Push this into a BeamRenderable, with support for refraction/ripples on sonic weapons - var lineWidth = wlr.LineWidth; - if (lineWidth != info.BeamWidth) - { - wlr.Flush(); - wlr.LineWidth = info.BeamWidth; - } - - wlr.DrawLine(src.ToFloat2(), dest.ToFloat2(), rc, rc); - - if (lineWidth != info.BeamWidth) - { - wlr.Flush(); - wlr.LineWidth = lineWidth; - } } } }