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

@@ -42,7 +42,7 @@ namespace Collector
_spriteBatch = new SpriteBatch(GraphicsDevice); _spriteBatch = new SpriteBatch(GraphicsDevice);
_mouse = new Mouse(Content, _spriteBatch, _cam); _mouse = new Mouse(Content, _spriteBatch, _cam);
_inputController = new InputController(_player, _mouse, _cam, _spriteBatch, Content); _inputController = new InputController(_mouse, _cam, _spriteBatch, Content);
WorldRenderer = new WorldRenderer(_mouse, _inputController, _player, _spriteBatch, this, _cam); WorldRenderer = new WorldRenderer(_mouse, _inputController, _player, _spriteBatch, this, _cam);
} }

View File

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