Add TextRenderable for CashTick. Make ticks consistent.

This commit is contained in:
Paul Chote
2013-06-16 19:10:38 +12:00
parent 52335a37bf
commit 1eb04a70a5
10 changed files with 81 additions and 36 deletions

View File

@@ -0,0 +1,57 @@
#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 TextRenderable : IRenderable
{
readonly SpriteFont font;
readonly WPos pos;
readonly int zOffset;
readonly Color color;
readonly string text;
public TextRenderable(SpriteFont font, WPos pos, int zOffset, Color color, string text)
{
this.font = font;
this.pos = pos;
this.zOffset = zOffset;
this.color = color;
this.text = text;
}
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 TextRenderable(font, pos, zOffset, color, text); }
public IRenderable WithPalette(PaletteReference newPalette) { return new TextRenderable(font, pos, zOffset, color, text); }
public IRenderable WithZOffset(int newOffset) { return new TextRenderable(font, pos, zOffset, color, text); }
public IRenderable WithPos(WPos pos) { return new TextRenderable(font, pos, zOffset, color, text); }
public void BeforeRender(WorldRenderer wr) {}
public void Render(WorldRenderer wr)
{
var screenPos = Game.viewport.Zoom*(wr.ScreenPxPosition(pos) - Game.viewport.Location) - 0.5f*font.Measure(text).ToFloat2();
font.DrawTextWithContrast(text, screenPos, color, Color.Black, 1);
}
public void RenderDebugGeometry(WorldRenderer wr)
{
var size = font.Measure(text).ToFloat2();
var offset = wr.ScreenPxPosition(pos) - 0.5f*size;
Game.Renderer.WorldLineRenderer.DrawRect(offset, offset + size, Color.Red);
}
}
}

View File

@@ -233,6 +233,7 @@
<Compile Include="Traits\BodyOrientation.cs" /> <Compile Include="Traits\BodyOrientation.cs" />
<Compile Include="Graphics\VoxelAnimation.cs" /> <Compile Include="Graphics\VoxelAnimation.cs" />
<Compile Include="Graphics\VoxelRenderable.cs" /> <Compile Include="Graphics\VoxelRenderable.cs" />
<Compile Include="Graphics\TextRenderable.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj"> <ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -27,15 +27,15 @@ namespace OpenRA.Mods.RA.Activities
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (IsCanceled || !target.IsValid) if (IsCanceled || !target.IsValid || !target.IsActor)
return NextActivity; return NextActivity;
var targetPlayer = target.Actor.Owner; var targetActor = target.Actor;
targetPlayer.PlayerActor.Trait<PlayerResources>().GiveCash(payload); targetActor.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(payload);
self.Destroy(); self.Destroy();
if (self.Owner.IsAlliedWith(self.World.RenderPlayer)) if (self.Owner.IsAlliedWith(self.World.RenderPlayer))
self.World.AddFrameEndTask(w => w.Add(new CashTick(payload, 30, 2, target.CenterLocation, targetPlayer.Color.RGB))); self.World.AddFrameEndTask(w => w.Add(new CashTick(targetActor.CenterPosition, targetActor.Owner.Color.RGB, payload)));
return this; return this;
} }

View File

@@ -32,7 +32,7 @@ namespace OpenRA.Mods.RA.Activities
ns.Sold(self); ns.Sold(self);
if (refund > 0 && self.Owner.IsAlliedWith(self.World.RenderPlayer)) if (refund > 0 && self.Owner.IsAlliedWith(self.World.RenderPlayer))
self.World.AddFrameEndTask(w => w.Add(new CashTick(refund, 30, 2, self.CenterLocation, self.Owner.Color.RGB))); self.World.AddFrameEndTask(w => w.Add(new CashTick(self.CenterPosition, self.Owner.Color.RGB, refund)));
self.Destroy(); self.Destroy();
return this; return this;

View File

@@ -23,10 +23,6 @@ namespace OpenRA.Mods.RA
public readonly int Amount = 15; public readonly int Amount = 15;
[Desc("Whether to show the cash tick indicators (+$15 rising from actor).")] [Desc("Whether to show the cash tick indicators (+$15 rising from actor).")]
public readonly bool ShowTicks = true; public readonly bool ShowTicks = true;
[Desc("How long the cash tick indicator should be shown for.")]
public readonly int TickLifetime = 30;
[Desc("Pixels/tick upward movement of the cash tick indicator.")]
public readonly int TickVelocity = 1;
[Desc("Amount of money awarded for capturing the actor.")] [Desc("Amount of money awarded for capturing the actor.")]
public readonly int CaptureAmount = 0; public readonly int CaptureAmount = 0;
@@ -64,7 +60,7 @@ namespace OpenRA.Mods.RA
void MaybeAddCashTick(Actor self, int amount) void MaybeAddCashTick(Actor self, int amount)
{ {
if (Info.ShowTicks) if (Info.ShowTicks)
self.World.AddFrameEndTask(w => w.Add(new CashTick(amount, Info.TickLifetime, Info.TickVelocity, self.CenterLocation, self.Owner.Color.RGB))); self.World.AddFrameEndTask(w => w.Add(new CashTick(self.CenterPosition, self.Owner.Color.RGB, amount)));
} }
} }
} }

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.RA
collector.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(amount); collector.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(amount);
if ((info as GiveCashCrateActionInfo).UseCashTick) if ((info as GiveCashCrateActionInfo).UseCashTick)
w.Add(new CashTick(amount, 20, 1, collector.CenterLocation, collector.Owner.Color.RGB)); w.Add(new CashTick(collector.CenterPosition, collector.Owner.Color.RGB, amount));
}); });
base.Activate(collector); base.Activate(collector);

View File

@@ -8,6 +8,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using OpenRA.Effects; using OpenRA.Effects;
@@ -18,41 +19,32 @@ namespace OpenRA.Mods.RA.Effects
{ {
class CashTick : IEffect class CashTick : IEffect
{ {
readonly string s;
int remaining;
readonly int velocity;
PPos pos;
readonly float2 offset;
readonly Color color;
readonly SpriteFont font; readonly SpriteFont font;
readonly string text;
Color color;
int remaining = 30;
WPos pos;
static string FormatCashAmount(int x) { return "{0}${1}".F(x < 0 ? "-" : "+", x); } public CashTick(WPos pos, Color color, int value)
public CashTick(int value, int lifetime, int velocity, PPos pos, Color color)
: this(FormatCashAmount(value), lifetime, velocity, pos, color) { }
public CashTick(string value, int lifetime, int velocity, PPos pos, Color color)
{ {
this.color = color; this.font = Game.Renderer.Fonts["TinyBold"];
this.velocity = velocity;
this.pos = pos; this.pos = pos;
s = value; this.color = color;
font = Game.Renderer.Fonts["TinyBold"]; this.text = "{0}${1}".F(value < 0 ? "-" : "+", Math.Abs(value));
offset = 0.5f*font.Measure(s).ToFloat2();
remaining = lifetime;
} }
static readonly WVec velocity = new WVec(0,0,86);
public void Tick(World world) public void Tick(World world)
{ {
if (--remaining <= 0) if (--remaining <= 0)
world.AddFrameEndTask(w => w.Remove(this)); world.AddFrameEndTask(w => w.Remove(this));
pos -= new PVecInt(0, velocity);
pos += velocity;
} }
public IEnumerable<IRenderable> Render(WorldRenderer wr) public IEnumerable<IRenderable> Render(WorldRenderer wr)
{ {
font.DrawTextWithContrast(s, Game.viewport.Zoom*(pos.ToFloat2() - Game.viewport.Location) - offset, color, Color.Black,1); yield return new TextRenderable(font, pos, 0, color, text);
yield break;
} }
} }
} }

View File

@@ -54,7 +54,7 @@ namespace OpenRA.Mods.RA
var bounty = cost * GetMultiplier(self) * info.Percentage / 10000; var bounty = cost * GetMultiplier(self) * info.Percentage / 10000;
if (bounty > 0 && e.Attacker.Owner.IsAlliedWith(self.World.RenderPlayer)) if (bounty > 0 && e.Attacker.Owner.IsAlliedWith(self.World.RenderPlayer))
e.Attacker.World.AddFrameEndTask(w => w.Add(new CashTick(bounty, 20, 1, self.CenterLocation, e.Attacker.Owner.Color.RGB))); e.Attacker.World.AddFrameEndTask(w => w.Add(new CashTick(self.CenterPosition, e.Attacker.Owner.Color.RGB, bounty)));
e.Attacker.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(bounty); e.Attacker.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(bounty);
} }

View File

@@ -44,8 +44,7 @@ namespace OpenRA.Mods.RA
Sound.PlayToPlayer(self.Owner, info.SoundToVictim); Sound.PlayToPlayer(self.Owner, info.SoundToVictim);
self.World.AddFrameEndTask(w => w.Add(new CashTick(toGive, 30, 2, self.CenterLocation, self.World.AddFrameEndTask(w => w.Add(new CashTick(self.CenterPosition, infiltrator.Owner.Color.RGB, toGive)));
infiltrator.Owner.Color.RGB)));
} }
} }
} }

View File

@@ -93,7 +93,7 @@ namespace OpenRA.Mods.RA
{ {
var temp = currentDisplayValue; var temp = currentDisplayValue;
if (self.Owner.IsAlliedWith(self.World.RenderPlayer)) if (self.Owner.IsAlliedWith(self.World.RenderPlayer))
self.World.AddFrameEndTask(w => w.Add(new CashTick(temp, Info.TickLifetime, Info.TickVelocity, self.CenterLocation, self.Owner.Color.RGB))); self.World.AddFrameEndTask(w => w.Add(new CashTick(self.CenterPosition, self.Owner.Color.RGB, temp)));
currentDisplayTick = Info.TickRate; currentDisplayTick = Info.TickRate;
currentDisplayValue = 0; currentDisplayValue = 0;
} }