1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

allowed formatting codes to contain sprite animations

This commit is contained in:
Ellpeck 2020-01-04 00:27:45 +01:00
parent c9d1fc4b47
commit e20a51ef23
5 changed files with 27 additions and 13 deletions

View file

@ -4,6 +4,7 @@ using System.Linq;
using Coroutine; using Coroutine;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Animations;
using MLEM.Extensions; using MLEM.Extensions;
using MLEM.Font; using MLEM.Font;
using MLEM.Formatting; using MLEM.Formatting;
@ -96,7 +97,11 @@ namespace Demos {
// note that all added formatting codes need to be lowercase, while their casing doesn't matter when used // note that all added formatting codes need to be lowercase, while their casing doesn't matter when used
TextFormatting.FormattingCodes["grass"] = new FormattingCode(image.Texture); TextFormatting.FormattingCodes["grass"] = new FormattingCode(image.Texture);
TextFormatting.FormattingCodes["tree"] = new FormattingCode(tree); TextFormatting.FormattingCodes["tree"] = new FormattingCode(tree);
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Additionally, you can create custom formatting codes that contain [Grass] images! Note that these images have to be square, or [Tree] bad things happen.")); // formatting codes can also be sprite animations!
var atlas = new UniformTextureAtlas(LoadContent<Texture2D>("Textures/Anim"), 4, 4);
TextFormatting.FormattingCodes["walk"] = new FormattingCode(new SpriteAnimation(0.2F, atlas[0, 0], atlas[0, 1], atlas[0, 2], atlas[0, 3]));
root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Additionally, you can create custom formatting codes that contain [Grass] images or [Walk] sprite animations! Note that these images have to be square, or [Tree] bad things happen."));
var animatedPar = root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Defining text animations as formatting codes is also possible, including [Wobbly]wobbly text[Unanimated] as well as a [Typing]dialogue-esc typing effect by default. Of course, more animations can be added though.")); var animatedPar = root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Defining text animations as formatting codes is also possible, including [Wobbly]wobbly text[Unanimated] as well as a [Typing]dialogue-esc typing effect by default. Of course, more animations can be added though."));
root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Reset Typing Animation") { root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Reset Typing Animation") {

View file

@ -39,19 +39,22 @@ namespace MLEM.Animations {
this.TotalTime += frame.Seconds; this.TotalTime += frame.Seconds;
} }
public SpriteAnimation(float timePerFrame, params TextureRegion[] regions) public SpriteAnimation(double timePerFrame, params TextureRegion[] regions)
: this(Array.ConvertAll(regions, region => new AnimationFrame(region, timePerFrame))) { : this(Array.ConvertAll(regions, region => new AnimationFrame(region, timePerFrame))) {
} }
public SpriteAnimation(float timePerFrame, Texture2D texture, params Rectangle[] regions) public SpriteAnimation(double timePerFrame, Texture2D texture, params Rectangle[] regions)
: this(timePerFrame, Array.ConvertAll(regions, region => new TextureRegion(texture, region))) { : this(timePerFrame, Array.ConvertAll(regions, region => new TextureRegion(texture, region))) {
} }
public void Update(GameTime time) { public void Update(GameTime time) {
this.SetTime(this.TimeIntoAnimation + time.ElapsedGameTime.TotalSeconds);
}
public void SetTime(double totalTime) {
if (this.IsFinished || this.IsPaused) if (this.IsFinished || this.IsPaused)
return; return;
this.TimeIntoAnimation = totalTime;
this.TimeIntoAnimation += time.ElapsedGameTime.TotalSeconds;
if (this.TimeIntoAnimation >= this.TotalTime) { if (this.TimeIntoAnimation >= this.TotalTime) {
if (!this.IsLooping) { if (!this.IsLooping) {
this.IsFinished = true; this.IsFinished = true;
@ -75,9 +78,9 @@ namespace MLEM.Animations {
public class AnimationFrame { public class AnimationFrame {
public readonly TextureRegion Region; public readonly TextureRegion Region;
public readonly float Seconds; public readonly double Seconds;
public AnimationFrame(TextureRegion region, float seconds) { public AnimationFrame(TextureRegion region, double seconds) {
this.Region = region; this.Region = region;
this.Seconds = seconds; this.Seconds = seconds;
} }

View file

@ -41,7 +41,7 @@ namespace MLEM.Animations {
return this.animations.Find(anim => anim.Animation.Name == name)?.Animation; return this.animations.Find(anim => anim.Animation.Name == name)?.Animation;
} }
private void FindAnimationToPlay() { public void FindAnimationToPlay() {
ConditionedAnimation animToPlay = null; ConditionedAnimation animToPlay = null;
if (this.CurrAnimation != null && this.CurrAnimation.ShouldPlay()) if (this.CurrAnimation != null && this.CurrAnimation.ShouldPlay())
animToPlay = this.CurrAnimation; animToPlay = this.CurrAnimation;

View file

@ -1,4 +1,5 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Animations;
using MLEM.Font; using MLEM.Font;
using MLEM.Textures; using MLEM.Textures;
@ -8,7 +9,7 @@ namespace MLEM.Formatting {
public readonly Type CodeType; public readonly Type CodeType;
public readonly Color Color; public readonly Color Color;
public readonly TextStyle Style; public readonly TextStyle Style;
public readonly TextureRegion Icon; public readonly SpriteAnimation Icon;
public readonly TextAnimation.DrawCharacter Animation; public readonly TextAnimation.DrawCharacter Animation;
public FormattingCode(Color color) { public FormattingCode(Color color) {
@ -21,7 +22,11 @@ namespace MLEM.Formatting {
this.CodeType = Type.Style; this.CodeType = Type.Style;
} }
public FormattingCode(TextureRegion icon) { public FormattingCode(TextureRegion icon) :
this(new SpriteAnimation(0, icon)) {
}
public FormattingCode(SpriteAnimation icon) {
this.Icon = icon; this.Icon = icon;
this.CodeType = Type.Icon; this.CodeType = Type.Icon;
} }
@ -31,7 +36,7 @@ namespace MLEM.Formatting {
this.CodeType = Type.Animation; this.CodeType = Type.Animation;
} }
public string GetReplacementString(IGenericFont font) { public virtual string GetReplacementString(IGenericFont font) {
return this.CodeType == Type.Icon ? TextFormatting.GetOneEmString(font) : string.Empty; return this.CodeType == Type.Icon ? TextFormatting.GetOneEmString(font) : string.Empty;
} }

View file

@ -5,6 +5,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions; using MLEM.Extensions;
using MLEM.Font; using MLEM.Font;
using MLEM.Misc;
using MLEM.Textures; using MLEM.Textures;
namespace MLEM.Formatting { namespace MLEM.Formatting {
@ -107,8 +108,8 @@ namespace MLEM.Formatting {
currStyle = code.Style; currStyle = code.Style;
break; break;
case FormattingCode.Type.Icon: case FormattingCode.Type.Icon:
var iconSc = new Vector2(1F / code.Icon.Width, 1F / code.Icon.Height) * regularFont.LineHeight * scale; code.Icon.SetTime(timeIntoAnimation.TotalSeconds % code.Icon.TotalTime);
batch.Draw(code.Icon, pos + innerOffset, color, 0, Vector2.Zero, iconSc, SpriteEffects.None, depth); batch.Draw(code.Icon.CurrentRegion, new RectangleF(pos + innerOffset, new Vector2(regularFont.LineHeight * scale)), color, 0, Vector2.Zero, SpriteEffects.None, depth);
break; break;
case FormattingCode.Type.Animation: case FormattingCode.Type.Animation:
currAnim = code.Animation; currAnim = code.Animation;