Added diagonal movement and figured out how to make spriteSheets work
This commit is contained in:
@@ -9,35 +9,93 @@ using Microsoft.Xna.Framework.Input;
|
||||
using MonoGame.Extended;
|
||||
using Mouse = Collector.Character.Mouse;
|
||||
|
||||
public class InputController : IRestrictions {
|
||||
|
||||
public class InputController : IRestrictions
|
||||
{
|
||||
private Player _player;
|
||||
private Mouse _mouse;
|
||||
private OrthographicCamera _cam;
|
||||
private Dictionary<string, Rectangle> animations;
|
||||
public string Input { get; set; }
|
||||
private SpriteBatch _spriteBatch;
|
||||
private ContentManager _content;
|
||||
private Dictionary<string, Rectangle[]> _animations;
|
||||
private string Input { get; set; }
|
||||
private readonly SpriteBatch _spriteBatch;
|
||||
private readonly Texture2D _texture;
|
||||
private int frameNumber;
|
||||
private int timeSinceLastFrame;
|
||||
|
||||
public InputController(Player player, Mouse mouse, OrthographicCamera cam, SpriteBatch spriteBatch, ContentManager contentManager)
|
||||
|
||||
public InputController(Player player, Mouse mouse, OrthographicCamera cam, SpriteBatch spriteBatch,
|
||||
ContentManager contentManager)
|
||||
{
|
||||
_player = player;
|
||||
_mouse = mouse;
|
||||
_cam = cam;
|
||||
_spriteBatch = spriteBatch;
|
||||
_content = contentManager;
|
||||
frameNumber = 0;
|
||||
_texture = contentManager.Load<Texture2D>("man");
|
||||
Input = "Down";
|
||||
|
||||
animations = new Dictionary<string, Rectangle>
|
||||
|
||||
_animations = new Dictionary<string, Rectangle[]>
|
||||
{
|
||||
["Up"] = new Rectangle(0, 0, 32, 32),
|
||||
["Right"] = new Rectangle(0, 0, 32, 32),
|
||||
["Left"] = new Rectangle(0, 0, 32, 32),
|
||||
["Right"] = new Rectangle(0, 0, 32, 32)
|
||||
["Up"] = new Rectangle[4]
|
||||
{
|
||||
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]
|
||||
{
|
||||
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]
|
||||
{
|
||||
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]
|
||||
{
|
||||
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]
|
||||
{
|
||||
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]
|
||||
{
|
||||
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]
|
||||
{
|
||||
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]
|
||||
{
|
||||
new Rectangle(32*0 + 32*4, 0, 32, 32),
|
||||
new Rectangle(32*1 + 32*4, 0, 32, 32),
|
||||
new Rectangle(32*2 + 32*4, 0, 32, 32),
|
||||
new Rectangle(32*3 + 32*4, 0, 32, 32)
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public void PlayerInput(Main main, SpriteBatch spriteBatch)
|
||||
public void PlayerInput(Main main, Mouse mouse, GameTime gameTime)
|
||||
{
|
||||
var keyboardState = Keyboard.GetState();
|
||||
const int movementSpeed = IRestrictions.MovementSpeed;
|
||||
@@ -45,135 +103,105 @@ public class InputController : IRestrictions {
|
||||
{
|
||||
Quit(main);
|
||||
}
|
||||
if (keyboardState.IsKeyDown(Keys.W))
|
||||
|
||||
if (keyboardState.IsKeyUp(Keys.W) && keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.S) && keyboardState.IsKeyUp(Keys.D))
|
||||
{
|
||||
Input = "Up";
|
||||
Player.Y += -movementSpeed;
|
||||
_cam.Move(new Vector2(0,-movementSpeed));
|
||||
}
|
||||
if (keyboardState.IsKeyDown(Keys.A))
|
||||
{
|
||||
Player.X += -movementSpeed;
|
||||
_cam.Move(new Vector2(-movementSpeed,0));
|
||||
Input = "Left";
|
||||
}
|
||||
if (keyboardState.IsKeyDown(Keys.S))
|
||||
{
|
||||
Player.Y += movementSpeed;
|
||||
_cam.Move(new Vector2(0,movementSpeed));
|
||||
Input = "Down";
|
||||
frameNumber = 0;
|
||||
_mouse.Draw();
|
||||
}
|
||||
|
||||
if (keyboardState.IsKeyDown(Keys.D))
|
||||
if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.D))
|
||||
{
|
||||
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);
|
||||
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);
|
||||
Input = "DownRight";
|
||||
Player.Y += movementSpeed;
|
||||
Player.X += movementSpeed;
|
||||
_cam.Move(new Vector2(movementSpeed, movementSpeed));
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.S) && keyboardState.IsKeyDown(Keys.A))
|
||||
{
|
||||
updateAnimationFrame(gameTime);
|
||||
Input = "DownLeft";
|
||||
Player.Y += movementSpeed;
|
||||
Player.X += -movementSpeed;
|
||||
_cam.Move(new Vector2(-movementSpeed, movementSpeed));
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.W))
|
||||
{
|
||||
updateAnimationFrame(gameTime);
|
||||
Input = "Up";
|
||||
Player.Y += -movementSpeed;
|
||||
_cam.Move(new Vector2(0, -movementSpeed));
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.A))
|
||||
{
|
||||
updateAnimationFrame(gameTime);
|
||||
Player.X += -movementSpeed;
|
||||
_cam.Move(new Vector2(-movementSpeed, 0));
|
||||
Input = "Left";
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.S))
|
||||
{
|
||||
updateAnimationFrame(gameTime);
|
||||
Player.Y += movementSpeed;
|
||||
_cam.Move(new Vector2(0, movementSpeed));
|
||||
Input = "Down";
|
||||
}
|
||||
else if (keyboardState.IsKeyDown(Keys.D))
|
||||
{
|
||||
updateAnimationFrame(gameTime);
|
||||
Player.X += movementSpeed;
|
||||
_cam.Move(new Vector2(movementSpeed, 0));
|
||||
Input = "Right";
|
||||
}
|
||||
|
||||
if (keyboardState.IsKeyDown(Keys.Q))
|
||||
{
|
||||
_cam.ZoomIn(0.01f);
|
||||
}
|
||||
|
||||
if (keyboardState.IsKeyDown(Keys.E))
|
||||
{
|
||||
_cam.ZoomOut(0.01f);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateAnimationFrame(GameTime gameTime)
|
||||
{
|
||||
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
|
||||
if (timeSinceLastFrame <= 99) return;
|
||||
frameNumber++;
|
||||
timeSinceLastFrame = 0;
|
||||
if (frameNumber > 3)
|
||||
{
|
||||
frameNumber = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static void Quit(Game main)
|
||||
{
|
||||
main.Exit();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
public void handleInput() {
|
||||
i++;
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
|
||||
Main.cam.zoom += 5;
|
||||
}
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.E)) {
|
||||
Main.cam.zoom -= 5;
|
||||
}
|
||||
if ((Gdx.input.isButtonJustPressed(Input.Buttons.LEFT) || Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT) )&& i > KEY_DELAY) {
|
||||
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
|
||||
Main.cam.unproject(mousePos);
|
||||
int x = mouse.getSelectedX(mousePos) >> TILE_SHIFT;
|
||||
int y = mouse.getSelectedY(mousePos) >> TILE_SHIFT;
|
||||
if(Gdx.input.isButtonJustPressed(Input.Buttons.LEFT)) {
|
||||
i = 0;
|
||||
Chunks.placeBlock(x, y, "wood");
|
||||
}
|
||||
if (Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT)) {
|
||||
i = 0;
|
||||
Chunks.removeBlock(x,y);
|
||||
}
|
||||
}
|
||||
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
player.setAnimation(animations.get("UpLeft"));
|
||||
Player.addX(-MOVEMENT_SPEED);
|
||||
Player.addY(MOVEMENT_SPEED);
|
||||
Main.cam.translate(-MOVEMENT_SPEED, MOVEMENT_SPEED);
|
||||
}
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
player.setAnimation(animations.get("UpRight"));
|
||||
Player.addX(MOVEMENT_SPEED);
|
||||
Player.addY(MOVEMENT_SPEED);
|
||||
Main.cam.translate(MOVEMENT_SPEED, MOVEMENT_SPEED);
|
||||
}
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
player.setAnimation(animations.get("DownLeft"));
|
||||
Player.addX(-MOVEMENT_SPEED);
|
||||
Player.addY(-MOVEMENT_SPEED);
|
||||
Main.cam.translate(-MOVEMENT_SPEED, -MOVEMENT_SPEED);
|
||||
}
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
player.setAnimation(animations.get("DownRight"));
|
||||
Player.addX(MOVEMENT_SPEED);
|
||||
Player.addY(-MOVEMENT_SPEED);
|
||||
|
||||
Main.cam.translate(MOVEMENT_SPEED, -MOVEMENT_SPEED);
|
||||
}
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.A) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
player.setAnimation(animations.get("Left"));
|
||||
Player.addX(-MOVEMENT_SPEED);
|
||||
Main.cam.translate(-MOVEMENT_SPEED, 0);
|
||||
}
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.D) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
player.setAnimation(animations.get("Right"));
|
||||
Player.addX(MOVEMENT_SPEED);
|
||||
Main.cam.translate(MOVEMENT_SPEED, 0);
|
||||
}
|
||||
else if (Gdx.input.isKeyPressed(Input.Keys.S) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
player.setAnimation(animations.get("Down"));
|
||||
Player.addY(-MOVEMENT_SPEED);
|
||||
Main.cam.translate(0, -MOVEMENT_SPEED);
|
||||
}
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.W) && i > KEY_DELAY) {
|
||||
i = 0;
|
||||
player.setAnimation(animations.get("Up"));
|
||||
Player.addY(MOVEMENT_SPEED);
|
||||
Main.cam.translate(0, MOVEMENT_SPEED);
|
||||
}
|
||||
Main.cam.update();
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
public void Draw()
|
||||
{
|
||||
var texture = _content.Load<Texture2D>("man/man-0");
|
||||
_spriteBatch.Draw(texture,new Vector2(Player.X,Player.Y),Color.White);
|
||||
_spriteBatch.Draw(_texture, new Vector2(Player.X, Player.Y), _animations[Input][frameNumber], Color.White);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,12 @@ using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace Collector.Dimension
|
||||
{
|
||||
public class BlockMaterials {
|
||||
public static Dictionary<string,Block> Materials = new Dictionary<string, Block>();
|
||||
public static Dictionary<string,Texture2D> Textures = new Dictionary<string, Texture2D>();
|
||||
public static class BlockMaterials {
|
||||
public static readonly Dictionary<string,Block> Materials = new Dictionary<string, Block>();
|
||||
public static readonly Dictionary<string,Texture2D> Textures = new Dictionary<string, Texture2D>();
|
||||
|
||||
//Private so the singleton can't be instantiated
|
||||
internal BlockMaterials() {}
|
||||
static BlockMaterials() {}
|
||||
|
||||
public static void Initialize(ContentManager content){
|
||||
Materials.Add("grass",new Block("grass"));
|
||||
@@ -27,11 +27,6 @@ namespace Collector.Dimension
|
||||
{
|
||||
Textures.Add(name,content.Load<Texture2D>(name));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void Draw(string name,SpriteBatch _spriteBatch,int x, int y)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,10 @@ namespace Collector.Dimension
|
||||
private SpriteBatch _spriteBatch;
|
||||
private Main _main;
|
||||
private OrthographicCamera _orthographicCamera;
|
||||
private double _elapsedTime;
|
||||
|
||||
public WorldRenderer(Mouse mouse, InputController inputController, Player player, SpriteBatch spriteBatch, Main main, OrthographicCamera orthographicCamera)
|
||||
public WorldRenderer(Mouse mouse, InputController inputController, Player player, SpriteBatch spriteBatch,
|
||||
Main main, OrthographicCamera orthographicCamera)
|
||||
{
|
||||
_mouse = mouse;
|
||||
_inputController = inputController;
|
||||
@@ -24,6 +26,7 @@ namespace Collector.Dimension
|
||||
_spriteBatch = spriteBatch;
|
||||
_main = main;
|
||||
_orthographicCamera = orthographicCamera;
|
||||
_elapsedTime = 0;
|
||||
}
|
||||
|
||||
private static void DrawWorld(SpriteBatch batch, int layer)
|
||||
@@ -53,14 +56,13 @@ namespace Collector.Dimension
|
||||
}
|
||||
*/
|
||||
|
||||
public void Draw()
|
||||
public void Draw(GameTime gameTime)
|
||||
{
|
||||
//Higher means draws in a lower layer
|
||||
DrawWorld(_spriteBatch, 0);
|
||||
_inputController.Draw();
|
||||
_inputController.PlayerInput(_main,_mouse,gameTime);
|
||||
DrawWorld(_spriteBatch, 1);
|
||||
_mouse.Draw();
|
||||
_inputController.PlayerInput(_main,_spriteBatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user