Skip to content

Commit

Permalink
basic setup for animated sprite.
Browse files Browse the repository at this point in the history
  • Loading branch information
SatchelF committed Apr 18, 2024
1 parent ec948da commit 6c70dde
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Client/Systems/AnimatedSprite.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
32 changes: 32 additions & 0 deletions src/Client/Systems/AnimatedSpriteObject.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
}
109 changes: 109 additions & 0 deletions src/Client/Systems/RenderingAnimatedSprites.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using CS5410.Input;

Check failure on line 1 in src/Client/Systems/RenderingAnimatedSprites.cs

View workflow job for this annotation

GitHub Actions / buildProject

The type or namespace name 'Input' does not exist in the namespace 'CS5410' (are you missing an assembly reference?)

Check failure on line 1 in src/Client/Systems/RenderingAnimatedSprites.cs

View workflow job for this annotation

GitHub Actions / buildProject

The type or namespace name 'Input' does not exist in the namespace 'CS5410' (are you missing an assembly reference?)
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;

Check failure on line 13 in src/Client/Systems/RenderingAnimatedSprites.cs

View workflow job for this annotation

GitHub Actions / buildProject

The type or namespace name 'Bird' does not exist in the namespace 'CS5410.Objects' (are you missing an assembly reference?)

Check failure on line 13 in src/Client/Systems/RenderingAnimatedSprites.cs

View workflow job for this annotation

GitHub Actions / buildProject

The type or namespace name 'Bird' does not exist in the namespace 'CS5410.Objects' (are you missing an assembly reference?)
private Objects.Bird m_bigBird;

Check failure on line 14 in src/Client/Systems/RenderingAnimatedSprites.cs

View workflow job for this annotation

GitHub Actions / buildProject

The type or namespace name 'Bird' does not exist in the namespace 'CS5410.Objects' (are you missing an assembly reference?)

Check failure on line 14 in src/Client/Systems/RenderingAnimatedSprites.cs

View workflow job for this annotation

GitHub Actions / buildProject

The type or namespace name 'Bird' does not exist in the namespace 'CS5410.Objects' (are you missing an assembly reference?)
private AnimatedSprite m_bigBirdRenderer;
private AnimatedSprite m_littleBirdRenderer;

private KeyboardInput m_inputKeyboard = new KeyboardInput();

Check failure on line 18 in src/Client/Systems/RenderingAnimatedSprites.cs

View workflow job for this annotation

GitHub Actions / buildProject

The type or namespace name 'KeyboardInput' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 18 in src/Client/Systems/RenderingAnimatedSprites.cs

View workflow job for this annotation

GitHub Actions / buildProject

The type or namespace name 'KeyboardInput' could not be found (are you missing a using directive or an assembly reference?)

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<Texture2D>("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<Texture2D>("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);
}
}
}

0 comments on commit 6c70dde

Please sign in to comment.