-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
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
|
||
private Objects.Bird m_bigBird; | ||
Check failure on line 14 in src/Client/Systems/RenderingAnimatedSprites.cs
|
||
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
|
||
|
||
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); | ||
} | ||
} | ||
} |