diff --git a/Shared/NPC.cs b/Shared/NPC.cs index a86c2ec..f8c3109 100644 --- a/Shared/NPC.cs +++ b/Shared/NPC.cs @@ -47,10 +47,7 @@ namespace SemiColinGames { } public class NPC { - // TODO: load sprite sizes from metadata. - private const int spriteWidth = 96; - private const int spriteHeight = 82; - private const int groundPadding = 7; + private readonly Sprite sprite; private readonly Vector2 spriteCenter; private readonly Vector2 eyeOffset = new Vector2(4, -9); @@ -59,8 +56,10 @@ namespace SemiColinGames { private readonly Vector2 halfSize = new Vector2(12, 24); public NPC(Point position, int facing) { + sprite = Sprites.Executioner; Position = position; - spriteCenter = new Vector2(spriteWidth / 2, spriteHeight - halfSize.Y - groundPadding); + spriteCenter = new Vector2( + sprite.Width / 2, sprite.Height - halfSize.Y - sprite.GroundPadding); physicsBox = new AABB(position.ToVector2(), halfSize); Facing = facing; fsm = new FSM(new Dictionary> { @@ -104,8 +103,8 @@ namespace SemiColinGames { } public void Draw(SpriteBatch spriteBatch) { - Rectangle textureSource = Sprites.Executioner.GetTextureSource( - fsm.StateName, Clock.ModelTime.TotalSeconds); + Rectangle textureSource = + sprite.GetTextureSource(fsm.StateName, Clock.ModelTime.TotalSeconds); SpriteEffects effect = Facing == 1 ? SpriteEffects.None : SpriteEffects.FlipHorizontally; Color color = Color.White; diff --git a/Shared/Sprites.cs b/Shared/Sprites.cs index 15118ac..04b0f29 100644 --- a/Shared/Sprites.cs +++ b/Shared/Sprites.cs @@ -46,6 +46,12 @@ namespace SemiColinGames { } public class Sprite { + public readonly int Width; + public readonly int Height; + // Measures the empty pixels between the ground (where the sprite's feet rest in idle pose) + // and the bottom of the sprite's image. + public readonly int GroundPadding = 7; + public readonly TextureRef Texture; private readonly Dictionary animations; @@ -67,6 +73,10 @@ namespace SemiColinGames { double duration = child.SelectToken("duration").Value() / 1000; frames.Add(new Frame(source, duration)); } + // We assume that all frames are the same size (which right now is assured by the + // Aseprite-based spritesheet export process). + Width = frames[0].Source.Width; + Height = frames[0].Source.Height; JToken frameTags = json.SelectToken("meta.frameTags"); foreach (JToken child in frameTags.Children()) {