Skip to content

Instantly share code, notes, and snippets.

@fdrobidoux
Created November 8, 2019 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdrobidoux/6b1b318c8ecf21cbb501515c5c212c08 to your computer and use it in GitHub Desktop.
Save fdrobidoux/6b1b318c8ecf21cbb501515c5c212c08 to your computer and use it in GitHub Desktop.
[SadConsole] Example of how to implement a custom Entity with animations defined in `GlyphDefinition`
using System;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using SadConsole;
using SadConsole.Entities;
using SadInput = SadConsole.Input;
using static SadConsole.FontMaster;
namespace TextAdventure.Core.Entities
{
public class EntityGuy : Entity
{
private AnimatedConsole animIdle;
private AnimatedConsole animWalkRight;
private AnimatedConsole animWalkLeft;
public EntityGuy(Font.FontSizes fontSize) : base(1, 1)
{
UseKeyboard = true;
IsFocused = true;
Font = Global.Fonts["EntityGuy"].GetFont(fontSize);
InitializeAnimations();
}
private void InitializeAnimations()
{
// Idle animation
animIdle = new AnimatedConsole("default", 1, 1, Font);
animIdle.CreateFrame().SetGlyph(0, 0, Font.Master.GetGlyphDefinition("Idle").Glyph);
Animations.Add("Idle", animIdle);
// Walk right animation
animWalkRight = new AnimatedConsole("WalkRight", 1, 1, Font)
{
AnimationDuration = 0.5f,
Repeat = true,
};
foreach (GlyphDefinition glyphDef in Font.Master.ListGlyphDefinitions("WalkRight"))
{
Cell singleCell = animWalkRight.CreateFrame().Cells[0];
singleCell.Glyph = glyphDef.Glyph;
singleCell.Mirror = glyphDef.Mirror;
}
Animations.Add("WalkRight", animWalkRight);
animWalkLeft = new AnimatedConsole("WalkLeft", 1, 1, Font)
{
AnimationDuration = 0.5f,
Repeat = true
};
foreach (GlyphDefinition glyphDef in Font.Master.ListGlyphDefinitions("WalkRight", SpriteEffects.FlipHorizontally))
{
Cell singleCell = animWalkLeft.CreateFrame().Cells[0];
singleCell.Glyph = glyphDef.Glyph;
singleCell.Mirror = glyphDef.Mirror;
}
Animations.Add("WalkLeft", animWalkLeft);
}
public override bool ProcessKeyboard(SadInput.Keyboard info)
{
// TODO use some StateMachine instead.
KeyboardState state = Keyboard.GetState();
if (info.IsKeyDown(Keys.Right))
(Animation = Animations["WalkRight"]).Start();
else if (info.IsKeyReleased(Keys.Right))
(Animation = Animations["Idle"]).Start();
if (info.IsKeyDown(Keys.Left))
(Animation = Animations["WalkLeft"]).Start();
else if (info.IsKeyReleased(Keys.Left))
(Animation = Animations["Idle"]).Start();
return base.ProcessKeyboard(info);
}
}
}
{
"Name": "EntityGuy",
"FilePath": "EntityGuy.png",
"Columns": 8,
"SolidGlyphIndex": 1,
"GlyphHeight": 32,
"GlyphWidth": 16,
"GlyphPadding": 0,
"IsSadExtended": true,
"GlyphDefinitions": {
"Idle": {
"Glyph": 2,
"Mirror": 0
},
"WalkRight": {
"Glyph": 3,
"Mirror": 0
},
"WalkRight1": {
"Glyph": 4,
"Mirror": 0
},
"WalkRight2": {
"Glyph": 5,
"Mirror": 0
},
"WalkRight3": {
"Glyph": 6,
"Mirror": 0
},
"WalkRight4": {
"Glyph": 7,
"Mirror": 0
},
"WalkRight5": {
"Glyph": 8,
"Mirror": 0
},
"WalkRight6": {
"Glyph": 9,
"Mirror": 0
}
}
}
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using static SadConsole.FontMaster;
namespace SadConsole
{
public static class FontMasterExtensions
{
public static IEnumerable<GlyphDefinition> ListGlyphDefinitions(this FontMaster _thisFM, string basename, SpriteEffects? mirrorMode = null)
{
int? frameIndex = null;
string currentName;
List<GlyphDefinition> outList = new List<GlyphDefinition>();
bool useMirror = mirrorMode.HasValue;
while (_thisFM.HasGlyphDefinition(currentName = basename + frameIndex))
{
GlyphDefinition glyph = _thisFM.GetGlyphDefinition(currentName);
yield return mirrorMode.HasValue
? new GlyphDefinition(glyph.Glyph, mirrorMode.Value)
: glyph;
frameIndex = (frameIndex == null ? 1 : ++frameIndex);
}
}
}
}
using System;
using SadConsole;
using SadConsole.Entities;
using TextAdventure.Core.Entities;
namespace TextAdventure.Core.Consoles.Tests
{
public class TestAnimatedEntitiesConsole : SadConsole.Console
{
Font characterFont;
Entity stickman;
public TestAnimatedEntitiesConsole() : base(30, 10)
{
UseKeyboard = true;
Font = Global.Fonts["EntityGuy"].GetFont(Font.FontSizes.One);
Children.Add(stickman = new EntityGuy(Font.SizeMultiple));
}
public override void Update(TimeSpan timeElapsed)
{
base.Update(timeElapsed);
}
}
}
@fdrobidoux
Copy link
Author

EntityGuy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment