Fixed the discrepancy between the screen coords and the game coords and also fixed mouse problem when zoomed out too far.

This commit is contained in:
2020-06-23 04:18:00 -06:00
parent c464d19857
commit 86cc05f0d7
42 changed files with 316 additions and 3472 deletions

View File

@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using Collector;
using Collector.Character;
using Collector.Dimension;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
@@ -9,201 +6,198 @@ using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended;
public class InputController : IRestrictions
namespace Collector.Character
{
private readonly PlayerMouse _playerMouse;
private readonly OrthographicCamera _cam;
private readonly Dictionary<string, Rectangle[]> _animations;
private readonly Texture2D _texture;
private readonly SpriteBatch _spriteBatch;
private string Input { get; set; }
private int _frameNumber;
private int _timeSinceLastFrame;
public InputController(PlayerMouse playerMouse, OrthographicCamera cam, SpriteBatch spriteBatch, ContentManager contentManager)
public class InputController : IRestrictions
{
Input = "Down";
_playerMouse = playerMouse;
_cam = cam;
_spriteBatch = spriteBatch;
_frameNumber = 0;
_texture = contentManager.Load<Texture2D>("man");
_animations = new Dictionary<string, Rectangle[]>
{
["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[]
{
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[]
{
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[]
{
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[]
{
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[]
{
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[]
{
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[]
{
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)
},
};
}
private readonly PlayerMouse _playerMouse;
private readonly OrthographicCamera _cam;
private readonly Dictionary<string, Rectangle[]> _animations;
private readonly Texture2D _texture;
private readonly SpriteBatch _spriteBatch;
private string Input { get; set; }
private int _frameNumber;
private int _timeSinceLastFrame;
public void PlayerInput(Main main, PlayerMouse playerMouse, GameTime gameTime)
{
var keyboardState = Keyboard.GetState();
var mouseState = Mouse.GetState();
const int movementSpeed = IRestrictions.MovementSpeed;
if (keyboardState.IsKeyDown(Keys.Escape))
{
Quit(main);
}
if (keyboardState.IsKeyUp(Keys.W) && keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.S) && keyboardState.IsKeyUp(Keys.D))
{
Input = Input.Contains("Right") ? "DownRight" : "DownLeft";
_frameNumber = 0;
_playerMouse.Draw();
}
if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.D))
public InputController(PlayerMouse playerMouse, OrthographicCamera cam, SpriteBatch spriteBatch, ContentManager contentManager)
{
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);
}
if (mouseState.LeftButton == ButtonState.Pressed)
{
Chunks.RemoveBlock(playerMouse.GetSelectedXTile(),playerMouse.GetSelectedYTile());
}
if (mouseState.RightButton == ButtonState.Pressed)
{
Chunks.PlaceBlock(playerMouse.GetSelectedXTile(),playerMouse.GetSelectedYTile(),Blocks.BlockWood);
}
}
private void UpdateAnimationFrame(GameTime gameTime)
{
_timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (_timeSinceLastFrame <= 99) return;
_frameNumber++;
_timeSinceLastFrame = 0;
if (_frameNumber > 3)
{
_playerMouse = playerMouse;
_cam = cam;
_spriteBatch = spriteBatch;
_frameNumber = 0;
_texture = contentManager.Load<Texture2D>("man");
const int tileSize = 32;
_animations = new Dictionary<string, Rectangle[]>
{
["Up"] = new[]
{
new Rectangle(0, 0, tileSize, tileSize),
new Rectangle(1, 0, tileSize, tileSize),
new Rectangle(2, 0, tileSize, tileSize),
new Rectangle(3, 0, tileSize, tileSize)
},
["Right"] = new[]
{
new Rectangle(0 + 4, 1, tileSize, tileSize),
new Rectangle(1 + 4, 1, tileSize, tileSize),
new Rectangle(2 + 4, 1, tileSize, tileSize),
new Rectangle(3 + 4, 1, tileSize, tileSize)
},
["UpRight"] = new[]
{
new Rectangle(0 + 4, 2, tileSize, tileSize),
new Rectangle(1 + 4, 2, tileSize, tileSize),
new Rectangle(2 + 4, 2, tileSize, tileSize),
new Rectangle(3 + 4, 2, tileSize, tileSize)
},
["DownRight"] = new[]
{
new Rectangle(0 + 4, 3, tileSize, tileSize),
new Rectangle(1 + 4, 3, tileSize, tileSize),
new Rectangle(2 + 4, 3, tileSize, tileSize),
new Rectangle(3 + 4, 3, tileSize, tileSize)
},
["Left"] = new[]
{
new Rectangle(0, 1, tileSize, tileSize),
new Rectangle(1, 1, tileSize, tileSize),
new Rectangle(2, 1, tileSize, tileSize),
new Rectangle(3, 1, tileSize, tileSize)
},
["UpLeft"] = new[]
{
new Rectangle(0, 2, tileSize, tileSize),
new Rectangle(1, 2, tileSize, tileSize),
new Rectangle(2, 2, tileSize, tileSize),
new Rectangle(3, 2, tileSize, tileSize)
},
["DownLeft"] = new[]
{
new Rectangle(0, 1, tileSize, tileSize),
new Rectangle(1, 1, tileSize, tileSize),
new Rectangle(2, 1, tileSize, tileSize),
new Rectangle(3, 1, tileSize, tileSize)
},
["Down"] = new[]
{
new Rectangle(0 + 4, 0, tileSize, tileSize),
new Rectangle(1 + 4, 0, tileSize, tileSize),
new Rectangle(2 + 4, 0, tileSize, tileSize),
new Rectangle(3 + 4, 0, tileSize, tileSize)
},
};
}
}
private static void Quit(Game main)
{
main.Exit();
}
public void PlayerInput(Main main, PlayerMouse playerMouse, GameTime gameTime)
{
const int movementSpeed = IRestrictions.MovementSpeed;
var keyboardState = Keyboard.GetState();
var mouseState = Mouse.GetState();
if (keyboardState.IsKeyDown(Keys.Escape))
{
Quit(main);
}
if (keyboardState.IsKeyUp(Keys.W) && keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.S) && keyboardState.IsKeyUp(Keys.D))
{
Input = Input.Contains("Right") ? "DownRight" : "DownLeft";
public void Draw()
{
_spriteBatch.Draw(_texture, new Vector2(Player.X, Player.Y), _animations[Input][_frameNumber], Color.White);
_frameNumber = 0;
_playerMouse.Draw();
}
if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.D))
{
UpdateAnimationFrame(gameTime);
Input = "UpRight";
Player.Move(_cam,movementSpeed,-movementSpeed);
}
else if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.A))
{
UpdateAnimationFrame(gameTime);
Input = "UpLeft";
Player.Move(_cam,-movementSpeed,-movementSpeed);
}
else if (keyboardState.IsKeyDown(Keys.S) && keyboardState.IsKeyDown(Keys.D))
{
UpdateAnimationFrame(gameTime);
Input = "DownRight";
Player.Move(_cam,movementSpeed,movementSpeed);
}
else if (keyboardState.IsKeyDown(Keys.S) && keyboardState.IsKeyDown(Keys.A))
{
UpdateAnimationFrame(gameTime);
Input = "DownLeft";
Player.Move(_cam,-movementSpeed,movementSpeed);
}
else if (keyboardState.IsKeyDown(Keys.W))
{
UpdateAnimationFrame(gameTime);
Input = "Up";
Player.Move(_cam,0,-movementSpeed);
}
else if (keyboardState.IsKeyDown(Keys.A))
{
UpdateAnimationFrame(gameTime);
Player.Move(_cam,-movementSpeed,0);
Input = "Left";
}
else if (keyboardState.IsKeyDown(Keys.S))
{
UpdateAnimationFrame(gameTime);
Player.Move(_cam,0,movementSpeed);
Input = "Down";
}
else if (keyboardState.IsKeyDown(Keys.D))
{
UpdateAnimationFrame(gameTime);
Player.Move(_cam,movementSpeed,0);
Input = "Right";
}
if (keyboardState.IsKeyDown(Keys.Q))
{
_cam.ZoomIn(1f);
}
if (keyboardState.IsKeyDown(Keys.E))
{
_cam.ZoomOut(1f);
}
if (mouseState.LeftButton == ButtonState.Pressed)
{
Chunks.RemoveBlock(PlayerMouse.GetSelectedX(),PlayerMouse.GetSelectedY());
}
if (mouseState.RightButton == ButtonState.Pressed)
{
Chunks.PlaceBlock(PlayerMouse.GetSelectedX(),PlayerMouse.GetSelectedY(),Blocks.BlockWood);
}
}
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 Draw()
{
_spriteBatch.Draw(
_texture
,Player.PlayerBounds
,_animations[Input][_frameNumber]
,Color.White
);
}
}
}

View File

@@ -1,19 +1,35 @@
using System;
using System.Collections.Generic;
using Collector.Dimension;
using Humper;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using MonoGame.Extended;
using MonoGame.Extended.Collections;
using Collision = Collector.Dimension.Collision;
namespace Collector.Character
{
public class Player : IRestrictions
{
public static int X { get; set; }
public static int Y { get; set; }
public Dictionary<string,Rectangle> Animation { get; set; }
//private Inventory PlayerInventory { get; }
public static int X { get; private set; }
public static int Y { get; private set; }
private static Dictionary<string, Rectangle> Animation { get; set; }
public static Rectangle PlayerBounds;
public Player(int x, int y) {
X = x<<IRestrictions.TileShift;
Y = y<<IRestrictions.TileShift;
//PlayerInventory = new Inventory();
public Player(int x, int y)
{
PlayerBounds = new Rectangle(X,Y,IRestrictions.TileSize,IRestrictions.TileSize);
X = x;
Y = y;
}
public static void Move(OrthographicCamera cam, int x, int y)
{
X += x;
Y += y;
cam.Move(new Vector2(x+IRestrictions.TileSize/2, y+IRestrictions.TileSize/2));
PlayerBounds.Location = new Point(X,Y);
}
}
}
}

View File

@@ -1,3 +1,4 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
@@ -9,7 +10,7 @@ namespace Collector.Character
public class PlayerMouse : IRestrictions
{
private readonly SpriteBatch _spriteBatch;
private readonly OrthographicCamera _cam;
private static OrthographicCamera _cam;
private Texture2D Crosshair { get; set; }
public PlayerMouse(ContentManager contentManager, SpriteBatch spriteBatch, OrthographicCamera cam)
@@ -18,30 +19,24 @@ namespace Collector.Character
_cam = cam;
Crosshair = contentManager.Load<Texture2D>("crosshair");
}
public void Draw()
{
_spriteBatch.Draw(Crosshair,new Vector2(GetSelectedX(),GetSelectedY()), Color.White);
}
public int GetSelectedX()
{
return ((int) (Mouse.GetState().X + _cam.Position.X) >> 5) << 5;
_spriteBatch.Draw(
Crosshair,
new Rectangle(GetSelectedX(),GetSelectedY(),1,1),
new Rectangle(0,0,32,32),
Color.White
);
}
public int GetSelectedY()
public static int GetSelectedX()
{
return ((int) (Mouse.GetState().Y + _cam.Position.Y) >> 5) << 5;
}
public int GetSelectedXTile()
{
return (int) (Mouse.GetState().X + _cam.Position.X) >> 5;
return (int) Math.Floor(_cam.ScreenToWorld(Mouse.GetState().X,Mouse.GetState().Y).X);
}
public int GetSelectedYTile()
public static int GetSelectedY()
{
return (int) (Mouse.GetState().Y + _cam.Position.Y) >> 5;
return (int) Math.Floor(_cam.ScreenToWorld(Mouse.GetState().X,Mouse.GetState().Y).Y);
}
}
}