Player: start using Sprite data to calculate texture source / animations.

This commit is contained in:
Colin McMillen 2020-03-05 15:28:34 -05:00
parent 77f1d73097
commit 0b8eb3e3f1
3 changed files with 27 additions and 30 deletions

View File

@ -231,36 +231,26 @@ namespace SemiColinGames {
return result; return result;
} }
private int SpriteIndex(Pose pose) { private Rectangle GetTextureSource(Pose pose) {
int frameNum = (int) Clock.ModelTime.TotalMilliseconds / 125 % 4; int time = (int) Clock.ModelTime.TotalMilliseconds;
switch (pose) { switch (pose) {
case Pose.Walking: case Pose.Walking:
return 35 + frameNum;
case Pose.Jumping: case Pose.Jumping:
return 35 + frameNum; return Sprites.Ninja.GetTextureSource("run", time);
case Pose.SwordSwing: case Pose.SwordSwing:
if (swordSwingTime > 0.2) { // TODO: make a proper animation class & FSM-driven animations.
return 0 + swordSwingNum * 3; return Sprites.Ninja.GetTextureSource(
} else if (swordSwingTime > 0.1) { "attack_sword", (int) (1000 * (0.3 - swordSwingTime)));
return 1 + swordSwingNum * 3;
} else {
return 2 + swordSwingNum * 3;
}
case Pose.Crouching: case Pose.Crouching:
case Pose.Stretching: case Pose.Stretching:
case Pose.Standing: case Pose.Standing:
default: { default:
if (frameNum == 3) { return Sprites.Ninja.GetTextureSource("idle", time);
frameNum = 1;
}
return 29 + frameNum;
}
} }
} }
public void Draw(SpriteBatch spriteBatch) { public void Draw(SpriteBatch spriteBatch) {
int index = SpriteIndex(pose); Rectangle textureSource = GetTextureSource(pose);
Rectangle textureSource = new Rectangle(index * spriteWidth, 0, spriteWidth, spriteHeight);
Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset); Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
SpriteEffects effect = Facing == 1 ? SpriteEffects effect = Facing == 1 ?
SpriteEffects.None : SpriteEffects.FlipHorizontally; SpriteEffects.None : SpriteEffects.FlipHorizontally;
@ -268,7 +258,7 @@ namespace SemiColinGames {
if (invincibilityTime > 0 && invincibilityTime % 0.2f > 0.1f) { if (invincibilityTime > 0 && invincibilityTime % 0.2f > 0.1f) {
color = new Color(0.5f, 0.5f, 0.5f, 0.5f); color = new Color(0.5f, 0.5f, 0.5f, 0.5f);
} }
spriteBatch.Draw(Textures.Player.Get, position.ToVector2(), textureSource, color, 0f, spriteBatch.Draw(Textures.Ninja.Get, position.ToVector2(), textureSource, color, 0f,
spriteCenter, Vector2.One, effect, 0f); spriteCenter, Vector2.One, effect, 0f);
} }
} }

View File

@ -4,6 +4,18 @@ using Newtonsoft.Json.Linq;
using System.Collections.Generic; using System.Collections.Generic;
namespace SemiColinGames { namespace SemiColinGames {
static class Sprites {
public static Sprite Executioner;
public static Sprite Ninja;
public static void Load(ContentManager content) {
Executioner = new Sprite(
Textures.Executioner, content.LoadString("sprites/ccg/executioner_female.json"));
Ninja = new Sprite(
Textures.Ninja, content.LoadString("sprites/ccg/ninja_female.json"));
}
}
struct SpriteAnimation { struct SpriteAnimation {
public readonly int Start; public readonly int Start;
public readonly int End; public readonly int End;
@ -77,13 +89,4 @@ namespace SemiColinGames {
return frames[animation.End].Source; return frames[animation.End].Source;
} }
} }
static class Sprites {
public static Sprite Executioner;
public static void Load(ContentManager content) {
Executioner = new Sprite(
Textures.Executioner, content.LoadString("sprites/ccg/executioner_female.json"));
}
}
} }

View File

@ -33,8 +33,11 @@ namespace SemiColinGames {
public static SpriteFont DebugFont; public static SpriteFont DebugFont;
public static SpriteFont BannerFont; public static SpriteFont BannerFont;
public static TextureRef Player = new TextureRef("sprites/ccg/ninja_female"); // Character spritesheets.
public static TextureRef Executioner = new TextureRef("sprites/ccg/executioner_female"); public static TextureRef Executioner = new TextureRef("sprites/ccg/executioner_female");
public static TextureRef Ninja = new TextureRef("sprites/ccg/ninja_female");
// UI sprites.
public static TextureRef Heart = new TextureRef("sprites/semicolin/heart"); public static TextureRef Heart = new TextureRef("sprites/semicolin/heart");
// Backgrounds are indexed by draw order; the first element should be drawn furthest back. // Backgrounds are indexed by draw order; the first element should be drawn furthest back.
@ -45,6 +48,7 @@ namespace SemiColinGames {
new TextureRef("backgrounds/szadiart/pf4/background4_day"), new TextureRef("backgrounds/szadiart/pf4/background4_day"),
}; };
// Background tiles.
public static TextureRef Cemetery = new TextureRef("tiles/anokolisa/cemetery"); public static TextureRef Cemetery = new TextureRef("tiles/anokolisa/cemetery");
public static TextureRef Crypt = new TextureRef("tiles/anokolisa/crypt"); public static TextureRef Crypt = new TextureRef("tiles/anokolisa/crypt");
public static TextureRef Dungeon = new TextureRef("tiles/anokolisa/dungeon"); public static TextureRef Dungeon = new TextureRef("tiles/anokolisa/dungeon");