From 6c70ddeaba5cd9e910a53f918f1514b84021da8c Mon Sep 17 00:00:00 2001 From: SatchelF Date: Wed, 17 Apr 2024 22:38:41 -0600 Subject: [PATCH] basic setup for animated sprite. --- src/Client/Systems/AnimatedSprite.cs | 47 ++++++++ src/Client/Systems/AnimatedSpriteObject.cs | 32 +++++ .../Systems/RenderingAnimatedSprites.cs | 109 ++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 src/Client/Systems/AnimatedSprite.cs create mode 100644 src/Client/Systems/AnimatedSpriteObject.cs create mode 100644 src/Client/Systems/RenderingAnimatedSprites.cs diff --git a/src/Client/Systems/AnimatedSprite.cs b/src/Client/Systems/AnimatedSprite.cs new file mode 100644 index 0000000..870083b --- /dev/null +++ b/src/Client/Systems/AnimatedSprite.cs @@ -0,0 +1,47 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; + +namespace CS5410 +{ + public class AnimatedSprite + { + private Texture2D m_spriteSheet; + private int[] m_spriteTime; + + private TimeSpan m_animationTime; + private int m_subImageIndex; + private int m_subImageWidth; + + public AnimatedSprite(Texture2D spriteSheet, int[] spriteTime) + { + this.m_spriteSheet = spriteSheet; + this.m_spriteTime = spriteTime; + + m_subImageWidth = spriteSheet.Width / spriteTime.Length; + } + + public void update(GameTime gameTime) + { + m_animationTime += gameTime.ElapsedGameTime; + if (m_animationTime.TotalMilliseconds >= m_spriteTime[m_subImageIndex]) + { + m_animationTime -= TimeSpan.FromMilliseconds(m_spriteTime[m_subImageIndex]); + m_subImageIndex++; + m_subImageIndex = m_subImageIndex % m_spriteTime.Length; + } + } + + public void draw(SpriteBatch spriteBatch, Objects.AnimatedSprite model) + { + spriteBatch.Draw( + m_spriteSheet, + new Rectangle((int)model.Center.X - m_subImageWidth / 2, (int)model.Center.Y - m_spriteSheet.Height / 2, (int)model.Size.X, (int)model.Size.Y), // Destination rectangle + new Rectangle(m_subImageIndex * m_subImageWidth, 0, m_subImageWidth, m_spriteSheet.Height), // Source sub-texture + Color.White, + model.Rotation, // Angular rotation + new Vector2(m_subImageWidth / 2, m_spriteSheet.Height / 2), // Center point of rotation + SpriteEffects.None, 0); + } + } +} diff --git a/src/Client/Systems/AnimatedSpriteObject.cs b/src/Client/Systems/AnimatedSpriteObject.cs new file mode 100644 index 0000000..c2c2491 --- /dev/null +++ b/src/Client/Systems/AnimatedSpriteObject.cs @@ -0,0 +1,32 @@ +using Microsoft.Xna.Framework; + +namespace CS5410.Objects +{ + public class AnimatedSprite + { + private readonly Vector2 m_size; + protected Vector2 m_center; + protected float m_rotation = 0; + + public AnimatedSprite(Vector2 size, Vector2 center) + { + m_size = size; + m_center = center; + } + + public Vector2 Size + { + get { return m_size; } + } + + public Vector2 Center + { + get { return m_center; } + } + + public float Rotation + { + get { return m_rotation; } + } + } +} diff --git a/src/Client/Systems/RenderingAnimatedSprites.cs b/src/Client/Systems/RenderingAnimatedSprites.cs new file mode 100644 index 0000000..925f12b --- /dev/null +++ b/src/Client/Systems/RenderingAnimatedSprites.cs @@ -0,0 +1,109 @@ +using CS5410.Input; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using System; + +namespace CS5410 +{ + public class RenderingAnimatedSprites : Game + { + private GraphicsDeviceManager m_graphics; + private SpriteBatch m_spriteBatch; + private Objects.Bird m_littleBird; + private Objects.Bird m_bigBird; + private AnimatedSprite m_bigBirdRenderer; + private AnimatedSprite m_littleBirdRenderer; + + private KeyboardInput m_inputKeyboard = new KeyboardInput(); + + public RenderingAnimatedSprites() + { + m_graphics = new GraphicsDeviceManager(this); + Content.RootDirectory = "Content"; + IsMouseVisible = true; + } + + protected override void Initialize() + { + m_graphics.PreferredBackBufferWidth = 1920; + m_graphics.PreferredBackBufferHeight = 1080; + + m_graphics.ApplyChanges(); + + base.Initialize(); + } + + protected override void LoadContent() + { + m_spriteBatch = new SpriteBatch(GraphicsDevice); + + m_littleBird = new Objects.Bird( + new Vector2(75, 75), + new Vector2(150, 200), + 125 / 1000.0, // Pixels per second + (float)(Math.PI / 1000.0)); + + m_bigBird = new Objects.Bird( + new Vector2(100, 100), + new Vector2(150, 350), + 75 / 1000.0, // Pixels per second + (float)(Math.PI / 1000.0)); + + m_bigBirdRenderer = new AnimatedSprite( + this.Content.Load("Images/spritesheet-bird"), + new int[] { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 } + ); + + m_littleBirdRenderer = new AnimatedSprite( + this.Content.Load("Images/spritesheet-bird"), + new int[] { 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25 } + ); + + // + // Setup input handlers + m_inputKeyboard.registerCommand(Keys.W, false, new InputDeviceHelper.CommandDelegate((gameTime, value) => { m_littleBird.moveForward(gameTime); } )); + m_inputKeyboard.registerCommand(Keys.A, false, new InputDeviceHelper.CommandDelegate((gameTime, value) => { m_littleBird.rotateLeft(gameTime); })); + m_inputKeyboard.registerCommand(Keys.D, false, new InputDeviceHelper.CommandDelegate((gameTime, value) => { m_littleBird.rotateRight(gameTime); })); + + m_inputKeyboard.registerCommand(Keys.Up, false, new InputDeviceHelper.CommandDelegate((gameTime, value) => { m_bigBird.moveForward(gameTime); })); + m_inputKeyboard.registerCommand(Keys.Left, false, new InputDeviceHelper.CommandDelegate((gameTime, value) => { m_bigBird.rotateLeft(gameTime); })); + m_inputKeyboard.registerCommand(Keys.Right, false, new InputDeviceHelper.CommandDelegate((gameTime, value) => { m_bigBird.rotateRight(gameTime); })); + } + + protected override void Update(GameTime gameTime) + { + // Allows the game to exit + if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) + { + this.Exit(); + } + + if (Keyboard.GetState().IsKeyDown(Keys.Escape)) + { + this.Exit(); + } + + m_littleBirdRenderer.update(gameTime); + m_bigBirdRenderer.update(gameTime); + + m_inputKeyboard.Update(gameTime); + + base.Update(gameTime); + } + + protected override void Draw(GameTime gameTime) + { + GraphicsDevice.Clear(Color.Black); + + m_spriteBatch.Begin(); + + m_littleBirdRenderer.draw(m_spriteBatch, m_littleBird); + m_bigBirdRenderer.draw(m_spriteBatch, m_bigBird); + + m_spriteBatch.End(); + + base.Draw(gameTime); + } + } +}