Explicit yawpitch, cardinal directions, and cleanup.

This commit is contained in:
Stanley Powers 2019-04-09 20:19:27 -07:00 committed by ORelio
parent 8bfdb2ab59
commit 4f54a4060a
2 changed files with 79 additions and 17 deletions

View file

@ -18,25 +18,49 @@ namespace MinecraftClient.Commands
string[] args = getArgs(command); string[] args = getArgs(command);
if (args.Length == 1) if (args.Length == 1)
{ {
return "ok."; string dirStr = getArg(command).Trim().ToLower();
Direction direction;
switch (dirStr)
{
case "up": direction = Direction.Up; break;
case "down": direction = Direction.Down; break;
case "east": direction = Direction.East; break;
case "west": direction = Direction.West; break;
case "north": direction = Direction.North; break;
case "south": direction = Direction.South; break;
default: return "Unknown direction '" + dirStr + "'.";
}
handler.LookAtDirection(direction);
return "Looking " + dirStr;
} }
else if (args.Length == 2) else if (args.Length == 2)
{ {
float yaw = Single.Parse(args[0]), try
pitch = Single.Parse(args[1]); {
float yaw = Single.Parse(args[0]),
pitch = Single.Parse(args[1]);
return $"Looking at YAW: {yaw} PITCH: {pitch}"; handler.LookAtAngle(yaw, pitch);
return $"Looking at YAW: {yaw} PITCH: {pitch}";
}
catch (FormatException) { return CMDDesc; }
} }
else if (args.Length == 3) else if (args.Length == 3)
{ {
int x = int.Parse(args[0]), try
y = int.Parse(args[1]), {
z = int.Parse(args[2]); int x = int.Parse(args[0]),
y = int.Parse(args[1]),
z = int.Parse(args[2]);
Location block = new Location(x, y, z); Location block = new Location(x, y, z);
handler.LookAtBlock(block); handler.LookAtBlock(block);
return "Looking at " + block; return "Looking at " + block;
}
catch (FormatException) { return CMDDesc; }
} }
else return CMDDesc; else return CMDDesc;
} }

View file

@ -426,23 +426,61 @@ namespace MinecraftClient
/// <param name="block">The block to look at</param> /// <param name="block">The block to look at</param>
public void LookAtBlock(Location block) public void LookAtBlock(Location block)
{ {
double dx = block.X - location.X, double dx = block.X - (location.X - 0.5),
dy = block.Y - location.Y, dy = block.Y - (location.Y + 1),
dz = block.Z - location.Z; dz = block.Z - (location.Z - 0.5);
double r = Math.Sqrt(dx * dx + dy * dy + dz * dz); double r = Math.Sqrt(dx * dx + dy * dy + dz * dz);
float yaw = Convert.ToSingle(-Math.Atan2(dx, dz) / Math.PI * 180), float yaw = Convert.ToSingle(-Math.Atan2(dx, dz) / Math.PI * 180),
pitch = Convert.ToSingle(-Math.Asin(dy / r) / Math.PI * 180); pitch = Convert.ToSingle(-Math.Asin(dy / r) / Math.PI * 180);
if (yaw < 0) yaw += 360; if (yaw < 0) yaw += 360;
ConsoleIO.WriteLineFormatted("YAW: " + yaw);
ConsoleIO.WriteLineFormatted("PITCH: " + pitch);
UpdateLocation(location, yaw, pitch); UpdateLocation(location, yaw, pitch);
} }
/// <summary>
/// Look at specified angle
/// </summary>
/// <param name="yaw">The yaw to look at</param>
/// <param name="pitch">The pitch to look at</param>
public void LookAtAngle(float yaw, float pitch)
{
UpdateLocation(location, yaw, pitch);
}
/// <summary>
/// Look in specified direction
/// </summary>
/// <param name="direction">The direction too look in</param>
public void LookAtDirection(Direction direction)
{
float yaw = 0,
pitch = 0;
switch (direction)
{
case Direction.Up:
pitch = -90;
break;
case Direction.Down:
pitch = 90;
break;
case Direction.East:
yaw = 270;
break;
case Direction.West:
yaw = 90;
break;
case Direction.North:
yaw = 90;
break;
case Direction.South:
break;
default:
throw new ArgumentException("Unknown direction", "direction");
}
UpdateLocation(location, yaw, pitch);
}
/// <summary> /// <summary>
/// Move to the specified location /// Move to the specified location