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

View File

@ -4,6 +4,18 @@ using Newtonsoft.Json.Linq;
using System.Collections.Generic;
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 {
public readonly int Start;
public readonly int End;
@ -77,13 +89,4 @@ namespace SemiColinGames {
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 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 Ninja = new TextureRef("sprites/ccg/ninja_female");
// UI sprites.
public static TextureRef Heart = new TextureRef("sprites/semicolin/heart");
// 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"),
};
// Background tiles.
public static TextureRef Cemetery = new TextureRef("tiles/anokolisa/cemetery");
public static TextureRef Crypt = new TextureRef("tiles/anokolisa/crypt");
public static TextureRef Dungeon = new TextureRef("tiles/anokolisa/dungeon");