From 0b8eb3e3f17328899188ecf2c7ce0829989ae5c8 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Thu, 5 Mar 2020 15:28:34 -0500 Subject: [PATCH] Player: start using Sprite data to calculate texture source / animations. --- Shared/Player.cs | 30 ++++++++++-------------------- Shared/Sprites.cs | 21 ++++++++++++--------- Shared/Textures.cs | 6 +++++- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/Shared/Player.cs b/Shared/Player.cs index ef61401..7f0f438 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -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); } } diff --git a/Shared/Sprites.cs b/Shared/Sprites.cs index 5676490..d6e15e1 100644 --- a/Shared/Sprites.cs +++ b/Shared/Sprites.cs @@ -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")); - } - } } diff --git a/Shared/Textures.cs b/Shared/Textures.cs index f5b9cb8..671c7db 100644 --- a/Shared/Textures.cs +++ b/Shared/Textures.cs @@ -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");