-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented damage indicator as a floating text above the card being …
…damaged.
- Loading branch information
Showing
7 changed files
with
221 additions
and
4 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
trunk/TouhouSpring/THSXnaWindows/THSXnaWindows/Animation/ReverseLinearTrack.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
trunk/TouhouSpring/THSXnaWindows/THSXnaWindows/Graphics/FloatingText.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
trunk/TouhouSpring/THSXnaWindows/THSXnaWindows/UI/CardControlAddins/DamageIndicator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
trunk/TouhouSpring/THSXnaWindows/THSXnaWindowsContent/Curves/DamageIndicatorFloat.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters