2019-08-21 20:25:32 +02:00
|
|
|
using System;
|
2019-08-14 19:07:23 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using MLEM.Animations;
|
|
|
|
using MLEM.Startup;
|
|
|
|
using MLEM.Textures;
|
|
|
|
|
|
|
|
namespace Demos {
|
2019-09-01 11:55:41 +02:00
|
|
|
public class AnimationDemo : Demo {
|
2019-08-14 19:07:23 +02:00
|
|
|
|
|
|
|
private SpriteAnimationGroup group;
|
|
|
|
private int facing;
|
|
|
|
|
2019-09-01 11:55:41 +02:00
|
|
|
public AnimationDemo(MlemGame game) : base(game) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void LoadContent() {
|
2019-08-14 19:07:23 +02:00
|
|
|
base.LoadContent();
|
|
|
|
|
|
|
|
var tex = LoadContent<Texture2D>("Textures/Anim");
|
2019-08-21 20:25:32 +02:00
|
|
|
|
2019-08-14 19:07:23 +02:00
|
|
|
// create the four animations by supplying the time per frame, the texture and the four regions used
|
2019-08-21 20:25:32 +02:00
|
|
|
var downAnim = new SpriteAnimation(0.2F, tex, new Rectangle(0, 0, 8, 8), new Rectangle(0, 8, 8, 8), new Rectangle(0, 16, 8, 8), new Rectangle(0, 24, 8, 8)) {Name = "Down"};
|
|
|
|
var upAnim = new SpriteAnimation(0.2F, tex, new Rectangle(8, 0, 8, 8), new Rectangle(8, 8, 8, 8), new Rectangle(8, 16, 8, 8), new Rectangle(8, 24, 8, 8)) {Name = "Up"};
|
|
|
|
var leftAnim = new SpriteAnimation(0.2F, tex, new Rectangle(16, 0, 8, 8), new Rectangle(16, 8, 8, 8), new Rectangle(16, 16, 8, 8), new Rectangle(16, 24, 8, 8)) {Name = "Left"};
|
|
|
|
var rightAnim = new SpriteAnimation(0.2F, tex, new Rectangle(24, 0, 8, 8), new Rectangle(24, 8, 8, 8), new Rectangle(24, 16, 8, 8), new Rectangle(24, 24, 8, 8)) {Name = "Right"};
|
2019-08-14 19:07:23 +02:00
|
|
|
|
|
|
|
// create a sprite animation group which manages a list of animations and figures out which one should
|
|
|
|
// be playing right now based on supplied conditions
|
|
|
|
// using a group isn't necessary, but highly recommended for things like character animations as it makes
|
|
|
|
// it very easy to have different animations play at different times
|
|
|
|
this.group = new SpriteAnimationGroup();
|
|
|
|
// for example, the down animation should only play when we're facing down (0 in this case)
|
|
|
|
this.group.Add(downAnim, () => this.facing == 0);
|
|
|
|
this.group.Add(upAnim, () => this.facing == 1);
|
|
|
|
this.group.Add(leftAnim, () => this.facing == 2);
|
|
|
|
this.group.Add(rightAnim, () => this.facing == 3);
|
2019-08-21 20:25:32 +02:00
|
|
|
|
|
|
|
// you can also add a priority to an animation in the group (10 in this case, which is higher than the default of 0)
|
|
|
|
// if two animations' playing conditions are both true, then the one with the higher priority will be picked to play
|
|
|
|
// in this instance, a standing "animation" is displayed when we're facing down and also holding the space key
|
2019-09-01 11:55:41 +02:00
|
|
|
this.group.Add(new SpriteAnimation(1F, tex, new Rectangle(0, 0, 8, 8)) {Name = "DownStanding"}, () => this.facing == 0 && this.InputHandler.IsKeyDown(Keys.Space), 10);
|
2019-08-21 20:25:32 +02:00
|
|
|
|
|
|
|
// you can also add a callback to see when the animation used changes
|
|
|
|
this.group.OnAnimationChanged += (anim, newAnim) => {
|
|
|
|
Console.WriteLine("Changing anim from " + (anim?.Name ?? "None") + " to " + (newAnim?.Name ?? "None"));
|
|
|
|
};
|
2019-08-14 19:07:23 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 11:55:41 +02:00
|
|
|
public override void Update(GameTime gameTime) {
|
2019-08-14 19:07:23 +02:00
|
|
|
base.Update(gameTime);
|
|
|
|
|
|
|
|
if (this.InputHandler.IsKeyDown(Keys.Down))
|
|
|
|
this.facing = 0;
|
|
|
|
else if (this.InputHandler.IsKeyDown(Keys.Up))
|
|
|
|
this.facing = 1;
|
|
|
|
else if (this.InputHandler.IsKeyDown(Keys.Left))
|
|
|
|
this.facing = 2;
|
|
|
|
else if (this.InputHandler.IsKeyDown(Keys.Right))
|
|
|
|
this.facing = 3;
|
|
|
|
|
|
|
|
// update the animation group
|
|
|
|
// if not using a group, just update the animation itself here
|
|
|
|
this.group.Update(gameTime);
|
|
|
|
}
|
|
|
|
|
2019-09-01 11:55:41 +02:00
|
|
|
public override void DoDraw(GameTime gameTime) {
|
2019-08-14 19:07:23 +02:00
|
|
|
this.GraphicsDevice.Clear(Color.Black);
|
|
|
|
|
|
|
|
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, transformMatrix: Matrix.CreateScale(10));
|
|
|
|
// draw the group's current region
|
2019-08-14 19:08:37 +02:00
|
|
|
// if not using a group, just draw the animation's CurrentRegion here
|
2019-08-14 19:07:23 +02:00
|
|
|
this.SpriteBatch.Draw(this.group.CurrentRegion, new Vector2(10, 10), Color.White);
|
|
|
|
this.SpriteBatch.End();
|
|
|
|
|
2019-08-15 14:59:15 +02:00
|
|
|
base.DoDraw(gameTime);
|
2019-08-14 19:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|