Added diagonal movement and figured out how to make spriteSheets work

This commit is contained in:
2020-04-23 13:06:39 -06:00
parent 0af73bf27d
commit 0c01a22761
2 changed files with 39 additions and 46 deletions

View File

@@ -1,7 +1,6 @@
using System.Collections.Generic;
using Collector;
using Collector.Character;
using Collector.Dimension;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
@@ -11,81 +10,76 @@ using Mouse = Collector.Character.Mouse;
public class InputController : IRestrictions
{
private Player _player;
private Mouse _mouse;
private OrthographicCamera _cam;
private Dictionary<string, Rectangle[]> _animations;
private string Input { get; set; }
private readonly SpriteBatch _spriteBatch;
private readonly Mouse _mouse;
private readonly OrthographicCamera _cam;
private readonly Dictionary<string, Rectangle[]> _animations;
private readonly Texture2D _texture;
private int frameNumber;
private int timeSinceLastFrame;
private readonly SpriteBatch _spriteBatch;
private string Input { get; set; }
private int _frameNumber;
private int _timeSinceLastFrame;
public InputController(Player player, Mouse mouse, OrthographicCamera cam, SpriteBatch spriteBatch,
ContentManager contentManager)
public InputController(Mouse mouse, OrthographicCamera cam, SpriteBatch spriteBatch, ContentManager contentManager)
{
_player = player;
Input = "Down";
_mouse = mouse;
_cam = cam;
_spriteBatch = spriteBatch;
frameNumber = 0;
_frameNumber = 0;
_texture = contentManager.Load<Texture2D>("man");
Input = "Down";
_animations = new Dictionary<string, Rectangle[]>
{
["Up"] = new Rectangle[4]
["Up"] = new[]
{
new Rectangle(32*0, 0, 32, 32),
new Rectangle(32*1, 0, 32, 32),
new Rectangle(32*2, 0, 32, 32),
new Rectangle(32*3, 0, 32, 32)
},
["Right"] = new Rectangle[4]
["Right"] = new[]
{
new Rectangle(32*0 + 32*4, 32, 32, 32),
new Rectangle(32*1 + 32*4, 32, 32, 32),
new Rectangle(32*2 + 32*4, 32, 32, 32),
new Rectangle(32*3 + 32*4, 32, 32, 32)
},
["UpRight"] = new Rectangle[4]
["UpRight"] = new[]
{
new Rectangle(32*0 + 32*4, 32*2, 32, 32),
new Rectangle(32*1 + 32*4, 32*2, 32, 32),
new Rectangle(32*2 + 32*4, 32*2, 32, 32),
new Rectangle(32*3 + 32*4, 32*2, 32, 32)
},
["DownRight"] = new Rectangle[4]
["DownRight"] = new[]
{
new Rectangle(32*0 + 32*4, 32*3, 32, 32),
new Rectangle(32*1 + 32*4, 32*3, 32, 32),
new Rectangle(32*2 + 32*4, 32*3, 32, 32),
new Rectangle(32*3 + 32*4, 32*3, 32, 32)
},
["Left"] = new Rectangle[4]
["Left"] = new[]
{
new Rectangle(32*0, 32, 32, 32),
new Rectangle(32*1, 32, 32, 32),
new Rectangle(32*2, 32, 32, 32),
new Rectangle(32*3, 32, 32, 32)
},
["UpLeft"] = new Rectangle[4]
["UpLeft"] = new[]
{
new Rectangle(32*0, 32*2, 32, 32),
new Rectangle(32*1, 32*2, 32, 32),
new Rectangle(32*2, 32*2, 32, 32),
new Rectangle(32*3, 32*2, 32, 32)
},
["DownLeft"] = new Rectangle[4]
["DownLeft"] = new[]
{
new Rectangle(32*0, 32, 32, 32),
new Rectangle(32*1, 32, 32, 32),
new Rectangle(32*2, 32, 32, 32),
new Rectangle(32*3, 32, 32, 32)
},
["Down"] = new Rectangle[4]
["Down"] = new[]
{
new Rectangle(32*0 + 32*4, 0, 32, 32),
new Rectangle(32*1 + 32*4, 0, 32, 32),
@@ -106,32 +100,30 @@ public class InputController : IRestrictions
if (keyboardState.IsKeyUp(Keys.W) && keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.S) && keyboardState.IsKeyUp(Keys.D))
{
Input = "Down";
frameNumber = 0;
Input = Input.Contains("Right") ? "DownRight" : "DownLeft";
_frameNumber = 0;
_mouse.Draw();
}
if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.D))
{
updateAnimationFrame(gameTime);
UpdateAnimationFrame(gameTime);
Input = "UpRight";
Player.Y += -movementSpeed;
Player.X += movementSpeed;
_cam.Move(new Vector2(movementSpeed, -movementSpeed));
}
else if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.A))
{
updateAnimationFrame(gameTime);
UpdateAnimationFrame(gameTime);
Input = "UpLeft";
Player.Y += -movementSpeed;
Player.X += -movementSpeed;
_cam.Move(new Vector2(-movementSpeed, -movementSpeed));
}
else if (keyboardState.IsKeyDown(Keys.S) && keyboardState.IsKeyDown(Keys.D))
{
updateAnimationFrame(gameTime);
UpdateAnimationFrame(gameTime);
Input = "DownRight";
Player.Y += movementSpeed;
Player.X += movementSpeed;
@@ -139,7 +131,7 @@ public class InputController : IRestrictions
}
else if (keyboardState.IsKeyDown(Keys.S) && keyboardState.IsKeyDown(Keys.A))
{
updateAnimationFrame(gameTime);
UpdateAnimationFrame(gameTime);
Input = "DownLeft";
Player.Y += movementSpeed;
Player.X += -movementSpeed;
@@ -147,28 +139,28 @@ public class InputController : IRestrictions
}
else if (keyboardState.IsKeyDown(Keys.W))
{
updateAnimationFrame(gameTime);
UpdateAnimationFrame(gameTime);
Input = "Up";
Player.Y += -movementSpeed;
_cam.Move(new Vector2(0, -movementSpeed));
}
else if (keyboardState.IsKeyDown(Keys.A))
{
updateAnimationFrame(gameTime);
UpdateAnimationFrame(gameTime);
Player.X += -movementSpeed;
_cam.Move(new Vector2(-movementSpeed, 0));
Input = "Left";
}
else if (keyboardState.IsKeyDown(Keys.S))
{
updateAnimationFrame(gameTime);
UpdateAnimationFrame(gameTime);
Player.Y += movementSpeed;
_cam.Move(new Vector2(0, movementSpeed));
Input = "Down";
}
else if (keyboardState.IsKeyDown(Keys.D))
{
updateAnimationFrame(gameTime);
UpdateAnimationFrame(gameTime);
Player.X += movementSpeed;
_cam.Move(new Vector2(movementSpeed, 0));
Input = "Right";
@@ -181,17 +173,18 @@ public class InputController : IRestrictions
{
_cam.ZoomOut(0.01f);
}
}
private void updateAnimationFrame(GameTime gameTime)
private void UpdateAnimationFrame(GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame <= 99) return;
frameNumber++;
timeSinceLastFrame = 0;
if (frameNumber > 3)
_timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (_timeSinceLastFrame <= 99) return;
_frameNumber++;
_timeSinceLastFrame = 0;
if (_frameNumber > 3)
{
frameNumber = 0;
_frameNumber = 0;
}
}
@@ -202,6 +195,6 @@ public class InputController : IRestrictions
public void Draw()
{
_spriteBatch.Draw(_texture, new Vector2(Player.X, Player.Y), _animations[Input][frameNumber], Color.White);
_spriteBatch.Draw(_texture, new Vector2(Player.X, Player.Y), _animations[Input][_frameNumber], Color.White);
}
}