working animated zombie

This commit is contained in:
Dylan Wilson
2015-10-06 22:14:47 +10:00
parent 63b48885d7
commit bc8ef34a74
7 changed files with 100 additions and 133 deletions
@@ -1,4 +1,6 @@
using System.IO;
using System;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Content.Pipeline;
@@ -9,22 +11,30 @@ namespace MonoGame.Extended.Content.Pipeline.Tiled
{
public override TmxMap Import(string filename, ContentImporterContext context)
{
if (filename == null) throw new ArgumentNullException("filename");
using (var reader = new StreamReader(filename))
{
var serializer = new XmlSerializer(typeof(TmxMap));
var map = (TmxMap)serializer.Deserialize(reader);
var xmlSerializer = new XmlSerializer(typeof(TmxTileset));
XmlSerializer xml = new XmlSerializer(typeof(TmxTileset));
for (int i = 0; i < map.Tilesets.Count; i++)
for (var i = 0; i < map.Tilesets.Count; i++)
{
var tileset = map.Tilesets[i];
if (!string.IsNullOrWhiteSpace(tileset.Source))
{
string dir = Path.GetDirectoryName(filename);
string tilesetLocation = tileset.Source.Replace('/', Path.DirectorySeparatorChar);
string filePath = Path.Combine(dir, tilesetLocation);
using (FileStream file = new FileStream(filePath, FileMode.Open))
map.Tilesets[i] = (TmxTileset)xml.Deserialize(file);
var directoryName = Path.GetDirectoryName(filename);
var tilesetLocation = tileset.Source.Replace('/', Path.DirectorySeparatorChar);
Debug.Assert(directoryName != null, "directoryName != null");
var filePath = Path.Combine(directoryName, tilesetLocation);
using (var file = new FileStream(filePath, FileMode.Open))
{
map.Tilesets[i] = (TmxTileset) xmlSerializer.Deserialize(file);
}
}
}
@@ -59,6 +59,7 @@
<Compile Include="Maps\Tiled\TiledMapOrientation.cs" />
<Compile Include="Shapes\Circle.cs" />
<Compile Include="Sprites\SpriteExtensions.cs" />
<Compile Include="Sprites\SpriteSheetAnimator.cs" />
<Compile Include="TextureAtlases\TextureAtlas.cs" />
<Compile Include="TextureAtlases\TextureAtlasReader.cs" />
<Compile Include="TextureAtlases\TextureRegion2DExtensions.cs" />
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using MonoGame.Extended.Sprites;
@@ -24,6 +25,7 @@ namespace Sandbox
private SpriteSheetAnimation _currentAnimation;
private float _nextFrameDelay;
private int _frameIndex;
private Action _onCompleteAction;
public bool IsPlaying { get; private set; }
public bool IsLooping { get; set; }
@@ -49,10 +51,14 @@ namespace Sandbox
_animations.Remove(name);
}
public void PlayAnimation(string name)
public void PlayAnimation(string name, Action onCompleteAction = null)
{
if(_currentAnimation != null && _currentAnimation.Name == name)
return;
_currentAnimation = _animations[name];
_frameIndex = 0;
_onCompleteAction = onCompleteAction;
}
public void Update(GameTime gameTime)
@@ -73,6 +79,11 @@ namespace Sandbox
_frameIndex = 0;
else
IsPlaying = false;
var onCompleteAction = _onCompleteAction;
if (onCompleteAction != null)
onCompleteAction();
}
var atlasIndex = _currentAnimation.FrameIndicies[_frameIndex];
@@ -81,5 +92,19 @@ namespace Sandbox
_nextFrameDelay -= deltaSeconds;
}
private class SpriteSheetAnimation
{
public SpriteSheetAnimation(string name, int framesPerSecond, int[] frameIndicies)
{
Name = name;
FramesPerSecond = framesPerSecond;
FrameIndicies = frameIndicies;
}
public string Name { get; private set; }
public int FramesPerSecond { get; private set; }
public int[] FrameIndicies { get; private set; }
}
}
}
-2
View File
@@ -39,8 +39,6 @@
<Compile Include="SandboxGame.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SpriteSheetAnimation.cs" />
<Compile Include="SpriteSheetAnimator.cs" />
<Compile Include="Zombie.cs" />
</ItemGroup>
<ItemGroup>
+14 -105
View File
@@ -25,10 +25,7 @@ namespace Sandbox
private BitmapFont _bitmapFont;
private TiledMap _tiledMap;
private FramesPerSecondCounter _fpsCounter;
private InputListenerManager _inputManager;
private SpriteAnimator _spriteAnimator;
private Vector2 _cameraDirection = Vector2.Zero;
private float _cameraRotation;
private Zombie _zombie;
public SandboxGame()
@@ -56,8 +53,6 @@ namespace Sandbox
Window.AllowUserResizing = true;
Window.ClientSizeChanged += (s, e) => _viewportAdapter.OnClientSizeChanged();
SetUpInput();
base.Initialize();
}
@@ -91,17 +86,26 @@ namespace Sandbox
protected override void Update(GameTime gameTime)
{
var deltaSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
var keyboardState = Keyboard.GetState();
if (Keyboard.GetState().IsKeyDown(Keys.R))
if (keyboardState.IsKeyDown(Keys.R))
_camera.ZoomIn(deltaSeconds);
if (Keyboard.GetState().IsKeyDown(Keys.F))
if (keyboardState.IsKeyDown(Keys.F))
_camera.ZoomOut(deltaSeconds);
_inputManager.Update(gameTime);
if (keyboardState.IsKeyDown(Keys.Left))
_zombie.Walk(-1.0f);
if (keyboardState.IsKeyDown(Keys.Right))
_zombie.Walk(1.0f);
if (keyboardState.IsKeyDown(Keys.Space))
_zombie.Attack();
if (keyboardState.IsKeyDown(Keys.Enter))
_zombie.Die();
_camera.Move(_cameraDirection * deltaSeconds);
_camera.Rotation += _cameraRotation * deltaSeconds;
_sprite.Position += new Vector2(-500, 0) * deltaSeconds;
_spriteAnimator.Update(gameTime);
@@ -133,100 +137,5 @@ namespace Sandbox
base.Draw(gameTime);
}
private void SetUpInput()
{
_inputManager = new InputListenerManager();
var up = new Vector2(0, -250);
var right = new Vector2(250, 0);
//camera movement
var keyboardListener = _inputManager.AddListener<KeyboardListener>();
var mouseListener = _inputManager.AddListener<MouseListener>();
keyboardListener.KeyPressed += (sender, args) =>
{
switch (args.Key)
{
case Keys.Escape:
Exit();
break;
case Keys.Q:
_cameraRotation += 1;
break;
case Keys.W:
_cameraRotation -= 1;
break;
case Keys.Up:
_cameraDirection += up;
break;
case Keys.Down:
_cameraDirection += -up;
break;
case Keys.Left:
_cameraDirection += -right;
break;
case Keys.Right:
_cameraDirection += right;
break;
}
};
keyboardListener.KeyReleased += (sender, args) =>
{
switch (args.Key)
{
case Keys.Escape:
Exit();
break;
case Keys.Q:
_cameraRotation -= 1;
break;
case Keys.W:
_cameraRotation += 1;
break;
case Keys.Up:
_cameraDirection -= up;
break;
case Keys.Down:
_cameraDirection -= -up;
break;
case Keys.Left:
_cameraDirection -= -right;
break;
case Keys.Right:
_cameraDirection -= right;
break;
}
};
// zoom
mouseListener.MouseWheelMoved += (sender, args) =>
{
_camera.ZoomIn(args.ScrollWheelDelta * 0.001f);
};
//// look at
//mouseListener.MouseUp += (sender, args) =>
//{
// if (args.Button == MouseButton.Left)
// {
// var p = _viewportAdapter.PointToScreen(args.Position);
// Trace.WriteLine(string.Format("{0} => {1}", args.Position, p));
// }
//};
mouseListener.MouseDown += (sender, args) => Trace.WriteLine("MouseDown");
mouseListener.MouseUp += (sender, args) => Trace.WriteLine("MouseUp");
mouseListener.MouseClicked += (sender, args) => Trace.WriteLine("MouseClicked");
mouseListener.MouseDoubleClicked += (sender, args) => Trace.WriteLine("MouseDoubleClicked");
mouseListener.MouseWheelMoved += (sender, args) => Trace.WriteLine(string.Format("MouseWheelMoved {0}", args.ScrollWheelValue));
mouseListener.MouseDragged += (sender, args) => Trace.WriteLine("MouseDragged");
}
}
}
-16
View File
@@ -1,16 +0,0 @@
namespace Sandbox
{
public class SpriteSheetAnimation
{
public SpriteSheetAnimation(string name, int framesPerSecond, int[] frameIndicies)
{
Name = name;
FramesPerSecond = framesPerSecond;
FrameIndicies = frameIndicies;
}
public string Name { get; private set; }
public int FramesPerSecond { get; private set; }
public int[] FrameIndicies { get; private set; }
}
}
+41 -1
View File
@@ -1,3 +1,4 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Sprites;
@@ -13,14 +14,30 @@ namespace Sandbox
{
OriginNormalized = new Vector2(0.5f, 1.0f)
};
_animator = new SpriteSheetAnimator(_sprite, textureAtlas);
_animator.AddAnimation("appear", framesPerSecond: 8, firstFrameIndex: 0, lastFrameIndex: 10);
_animator.AddAnimation("idle", framesPerSecond: 8, firstFrameIndex: 36, lastFrameIndex: 41);
_animator.PlayAnimation("idle");
_animator.AddAnimation("walk", framesPerSecond: 8, firstFrameIndex: 19, lastFrameIndex: 28);
_animator.AddAnimation("attack", framesPerSecond: 8, firstFrameIndex: 29, lastFrameIndex: 35);
_animator.AddAnimation("die", framesPerSecond: 8, firstFrameIndex: 11, lastFrameIndex: 18);
_animator.PlayAnimation("appear", ResumeIdle);
}
private float _direction = -1.0f;
private void ResumeIdle()
{
IsWalking = false;
_animator.PlayAnimation("idle");
}
private readonly Sprite _sprite;
private readonly SpriteSheetAnimator _animator;
public bool IsWalking { get; private set; }
public Vector2 Position
{
get { return _sprite.Position; }
@@ -30,11 +47,34 @@ namespace Sandbox
public void Update(GameTime gameTime)
{
_animator.Update(gameTime);
if(IsWalking)
Position += new Vector2(50f * _direction, 0) * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_sprite);
}
public void Walk(float direction)
{
_sprite.Effect = _direction > 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
_direction = direction;
IsWalking = true;
_animator.PlayAnimation("walk", ResumeIdle);
}
public void Attack()
{
IsWalking = false;
_animator.PlayAnimation("attack", ResumeIdle);
}
public void Die()
{
IsWalking = false;
_animator.PlayAnimation("die", () => _animator.PlayAnimation("appear", ResumeIdle));
}
}
}