Problems:
- Sprite moves in a zig-zag manner in-between tiles
- I can move the sprite on the x or y axis in a straight line, but I can't get it to move on the x axis then on the y axis, ie. moving a tile below would require moving either on the x towards the right or on the y towards the left then move on y towards left or on the x towards the right respectively
Code:
public void HandleInput(TileGrid grid, SpriteAnimation playerSprite, InputHandler input)
{
Vector2 selectionLocation = Camera.ScreenToWorld(new Vector2(Mouse.GetState().X, Mouse.GetState().Y));
Point selectionPoint = grid.WorldToMapCell(new Point((int)selectionLocation.X, (int)selectionLocation.Y));
int selectionOffset = 0;
if ((selectionPoint.Y) % 2 == 1)
selectionOffset = Tile.OddRowOffset;
if (input.leftClick() && canMove == true)
{
desiredPosition.X = (selectionPoint.X * 64) + 32 + selectionOffset;
desiredPosition.Y = (selectionPoint.Y * 16) + 48;
}
if (playerSprite.Position != desiredPosition)
{
canMove = false;
if ((playerSprite.Position.X > desiredPosition.X) && (playerSprite.Position.Y > desiredPosition.Y))
{
MoveNorthWest();
}
else if ((playerSprite.Position.X > desiredPosition.X) && (playerSprite.Position.Y < desiredPosition.Y))
{
MoveSouthWest();
}
else if ((playerSprite.Position.X < desiredPosition.X) && (playerSprite.Position.Y > desiredPosition.Y))
{
MoveNorthEast();
}
else if ((playerSprite.Position.X < desiredPosition.X) && (playerSprite.Position.Y < desiredPosition.Y))
{
MoveSouthEast();
}
}
else
{
moveDir = Vector2.Zero;
}
if (grid.GetCellAtWorldPoint(playerSprite.Position + moveDir).Collidable == true)
{
moveDir = Vector2.Zero;
}
if (Math.Abs(grid.GetOverallDepth(playerSprite.Position) - grid.GetOverallDepth(playerSprite.Position + moveDir)) > 10)
{
moveDir = Vector2.Zero;
}
if (moveDir.Length() != 0)
{
playerSprite.MoveBy((int)moveDir.X, (int)moveDir.Y);
if (playerSprite.CurrentAnimation != animation)
playerSprite.CurrentAnimation = animation;
}
else
{
playerSprite.CurrentAnimation = "Idle" + playerSprite.CurrentAnimation.Substring(4);
canMove = true;
}
}
public void MoveNorthEast()
{
moveDir = new Vector2(2, -1);
animation = "WalkNorthEast";
}
public void MoveNorthWest()
{
moveDir = new Vector2(-2, -1);
animation = "WalkNorthWest";
}
public void MoveSouthEast()
{
moveDir = new Vector2(2, 1);
animation = "WalkSouthEast";
}
public void MoveSouthWest()
{
moveDir = new Vector2(-2, 1);
animation = "WalkSouthWest";
}