Sprite: keep track of Width and Height

This commit is contained in:
Colin McMillen 2020-03-11 14:42:10 -04:00
parent b4e644c2d4
commit e97ff479d0
2 changed files with 16 additions and 7 deletions

View File

@ -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<NPC>(new Dictionary<string, IState<NPC>> {
@ -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;

View File

@ -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<string, SpriteAnimation> animations;
@ -67,6 +73,10 @@ namespace SemiColinGames {
double duration = child.SelectToken("duration").Value<double>() / 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()) {