Skip to content

Commit

Permalink
Implemented damage indicator as a floating text above the card being …
Browse files Browse the repository at this point in the history
…damaged.
  • Loading branch information
zeroyao committed Dec 31, 2012
1 parent 99de063 commit 436a4b0
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TouhouSpring.Animation
{
class ReverseLinearTrack : Track
{
private float m_duraion;

public override float Duration
{
get { return m_duraion; }
}

public ReverseLinearTrack(float duration)
{
if (duration == 0.0f)
{
throw new ArgumentOutOfRangeException("Duration can't be zero.");
}

m_duraion = duration;
}

public override float Evaluate(float time)
{
return 1.0f - time / Duration;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace TouhouSpring.Graphics
{
[Services.RenderDependency(typeof(Services.UIManager))]
class FloatingText : Services.GameService
{
private class Entry
{
public TextRenderer.IFormattedText m_text;
public TextRenderer.DrawOptions m_drawOptions;
public Vector2 m_2dPosStart;
public Vector2 m_2dPosEnd;
public Animation.Track m_positionTrack;
public Animation.Track m_alphaTrack;
}

private List<Entry> m_entries = new List<Entry>();
private Matrix m_transform;

public void Register(TextRenderer.IFormattedText text, TextRenderer.DrawOptions drawOptions, Vector2 screenVelocity, Vector2 screenPos, float duration)
{
Register(text, drawOptions, screenVelocity, screenPos, duration, false);
}

public void Register(TextRenderer.IFormattedText text, TextRenderer.DrawOptions drawOptions, Vector2 screenVelocity, Vector2 screenPos, float duration, bool fadeOut)
{
var posEnd = screenPos + duration * screenVelocity;
var positionTrack = new Animation.LinearTrack(duration);
var alphaTrack = fadeOut ? new Animation.ReverseLinearTrack(duration) : null;
Register(text, drawOptions, screenPos, posEnd, positionTrack, alphaTrack);
}

public void Register(TextRenderer.IFormattedText text, TextRenderer.DrawOptions drawOptions,
Vector2 screenPosStart, Vector2 screenPosEnd,
Animation.Track positionTrack, Animation.Track alphaTrack)
{
if (text == null)
{
throw new ArgumentNullException("text");
}
else if (positionTrack == null)
{
throw new ArgumentNullException("positionTrack");
}

positionTrack.Play();
if (alphaTrack != null)
{
alphaTrack.Play();
}

m_entries.Add(new Entry
{
m_text = text,
m_drawOptions = drawOptions,
m_2dPosStart = screenPosStart,
m_2dPosEnd = screenPosEnd,
m_positionTrack = positionTrack,
m_alphaTrack = alphaTrack
});
}

public override void Startup()
{
var device = GameApp.Instance.GraphicsDevice;
m_transform = Matrix.CreateScale(2.0f / device.Viewport.Width, -2.0f / device.Viewport.Height, 1.0f)
* Matrix.CreateTranslation(-1.0f, 1.0f, 0.0f);
}

public override void Update(float deltaTime)
{
for (int i = 0; i < m_entries.Count; ++i)
{
var entry = m_entries[i];
entry.m_positionTrack.Elapse(deltaTime);
if (entry.m_alphaTrack != null)
{
entry.m_alphaTrack.Elapse(deltaTime);
}

if (!entry.m_positionTrack.IsPlaying)
{
m_entries.RemoveAt(i);
--i;
}
}
}

public override void Render()
{
var textRenderer = GameApp.Service<TextRenderer>();
m_entries.ForEach(entry => RenderEntry(textRenderer, entry));
}

private void RenderEntry(TextRenderer renderer, Entry entry)
{
var pos = Vector2.Lerp(entry.m_2dPosStart, entry.m_2dPosEnd, entry.m_positionTrack.CurrentValue);
var transform = Matrix.CreateTranslation(pos.X, pos.Y, 0.0f) * m_transform;
entry.m_drawOptions.ColorScaling.W = entry.m_alphaTrack != null ? entry.m_alphaTrack.CurrentValue : 1.0f;
renderer.DrawText(entry.m_text, transform, entry.m_drawOptions);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ public void RegisterCard(BaseCard card)
ccStyle.Initialize();

var cardControl = ccStyle.TypedTarget;
//cardControl.Addins.Add(new UI.CardControlAddins.Flip(cardControl));
cardControl.Addins.Add(new UI.CardControlAddins.Glow(cardControl));
cardControl.Addins.Add(new UI.CardControlAddins.Highlight(cardControl));
cardControl.Addins.Add(new UI.CardControlAddins.DamageIndicator(cardControl));
cardControl.Addins.Add(new UI.CardControlAddins.LocationAnimation(cardControl, Card_ResolveLocationTransform));
//cardControl.Addins.Add(new UI.CardControlAddins.SelectedAnimation(cardControl));
//cardControl.Addins.Add(new UI.CardControlAddins.SpellButtons(cardControl));
cardControl.Addins.Add(new UI.CardControlAddins.ToneAnimation(cardControl));
m_cardControls.Add(cardControl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@
<Reference Include="System.Net" />
</ItemGroup>
<ItemGroup>
<Compile Include="Animation\ReverseLinearTrack.cs" />
<Compile Include="Animation\CurveTrack.cs" />
<Compile Include="Animation\LinearTrack.cs" />
<Compile Include="Animation\Track.cs" />
<Compile Include="Animation\TrackUpdater.cs" />
<Compile Include="Camera.cs" />
<Compile Include="ConversationManager.cs" />
<Compile Include="Graphics\FloatingText.cs" />
<Compile Include="Graphics\ParticleRenderer.cs" />
<Compile Include="Graphics\ParticleRenderer.ResourceLoader.cs" />
<Compile Include="Graphics\RenderManager.cs" />
Expand Down Expand Up @@ -147,6 +149,7 @@
<Compile Include="UI\CardControl.Binding.cs" />
<Compile Include="UI\CardControl.cs" />
<Compile Include="UI\CardControl.Render.cs" />
<Compile Include="UI\CardControlAddins\DamageIndicator.cs" />
<Compile Include="UI\CardControlAddins\Flip.cs" />
<Compile Include="UI\CardControlAddins\Glow.cs" />
<Compile Include="UI\CardControlAddins\Highlight.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace TouhouSpring.UI.CardControlAddins
{
class DamageIndicator : CardControl.Addin
{
[Services.LifetimeDependency(typeof(Services.ResourceManager))]
private class Resources : Services.GameService
{
public Graphics.TextRenderer.FormatOptions FormatOptions
{
get; private set;
}

public Graphics.TextRenderer.DrawOptions DrawOptions
{
get; private set;
}

public Curve MoveCurve
{
get; private set;
}

public override void Startup()
{
var font = new Graphics.TextRenderer.FontDescriptor("Georgia", 20, System.Drawing.FontStyle.Bold);
FormatOptions = new Graphics.TextRenderer.FormatOptions(font) { Alignment = Graphics.TextRenderer.Alignment.CenterMiddle };
var drawOptions = Graphics.TextRenderer.DrawOptions.Default;
drawOptions.ColorScaling = Vector4.One;
DrawOptions = drawOptions;
MoveCurve = GameApp.Service<Services.ResourceManager>().Acquire<Curve>("Curves/DamageIndicatorFloat");
}
}

private int m_lastLife = -1;
private Vector2 m_offset = new Vector2(0, -80);

public DamageIndicator(CardControl control) : base(control)
{
}

public override void Update(float deltaTime)
{
var warrior = Card.Behaviors.Get<Behaviors.Warrior>();
if (warrior != null && m_lastLife > warrior.Life)
{
// get the center position of the card in screen space
var pt = new Vector3(Control.Region.Width / 2, Control.Region.Height / 2, 0);
pt = (Control.Style.ChildIds["Body"].Target as ITransformNode).TransformToGlobal.TransformCoord(pt);
var halfVpWidth = GameApp.Instance.GraphicsDevice.Viewport.Width * 0.5f;
var halfVpHeight = GameApp.Instance.GraphicsDevice.Viewport.Height * 0.5f;
var screenPt = new Vector2(pt.X * halfVpWidth + halfVpWidth, halfVpHeight - pt.Y * halfVpHeight);

var resources = GameApp.Service<Resources>();
var text = GameApp.Service<Graphics.TextRenderer>().FormatText("[color:Red]-" + (m_lastLife - warrior.Life).ToString() + "[/color]", resources.FormatOptions);
GameApp.Service<Graphics.FloatingText>().Register(text, resources.DrawOptions,
screenPt, screenPt + m_offset,
new Animation.CurveTrack(resources.MoveCurve), new Animation.ReverseLinearTrack(resources.MoveCurve.Keys.LastOrDefault().Position));
}

m_lastLife = warrior != null ? warrior.Life : -1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><XnaContent xmlns:Framework="Microsoft.Xna.Framework"><Asset Type="Framework:Curve"><PreLoop>Constant</PreLoop><PostLoop>Constant</PostLoop><Keys>0 0 0 -0.007821287 Smooth 1 1 -0.00251948833 0 Smooth</Keys></Asset></XnaContent>
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,13 @@
<Processor>PassThroughProcessor</Processor>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Curves\DamageIndicatorFloat.xml">
<Name>DamageIndicatorFloat</Name>
<Importer>XmlImporter</Importer>
<Processor>PassThroughProcessor</Processor>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down

0 comments on commit 436a4b0

Please sign in to comment.