diff --git a/Collector/Main.cs b/Collector/Main.cs index 50601d5..5e3a4bf 100644 --- a/Collector/Main.cs +++ b/Collector/Main.cs @@ -42,7 +42,7 @@ namespace Collector _spriteBatch = new SpriteBatch(GraphicsDevice); _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); } diff --git a/Collector/src/Character/InputController.cs b/Collector/src/Character/InputController.cs index fd1000f..7cff0f4 100644 --- a/Collector/src/Character/InputController.cs +++ b/Collector/src/Character/InputController.cs @@ -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 _animations; - private string Input { get; set; } - private readonly SpriteBatch _spriteBatch; + private readonly Mouse _mouse; + private readonly OrthographicCamera _cam; + private readonly Dictionary _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("man"); - Input = "Down"; - - _animations = new Dictionary { - ["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); } } \ No newline at end of file