diff --git a/MinecraftClient/Commands/Look.cs b/MinecraftClient/Commands/Look.cs index 2f17c441..77c79e6b 100644 --- a/MinecraftClient/Commands/Look.cs +++ b/MinecraftClient/Commands/Look.cs @@ -18,25 +18,49 @@ namespace MinecraftClient.Commands string[] args = getArgs(command); 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) { - float yaw = Single.Parse(args[0]), - pitch = Single.Parse(args[1]); + try + { + 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) { - int x = int.Parse(args[0]), - y = int.Parse(args[1]), - z = int.Parse(args[2]); + try + { + int x = int.Parse(args[0]), + y = int.Parse(args[1]), + z = int.Parse(args[2]); - Location block = new Location(x, y, z); - handler.LookAtBlock(block); + Location block = new Location(x, y, z); + handler.LookAtBlock(block); - return "Looking at " + block; + return "Looking at " + block; + } + catch (FormatException) { return CMDDesc; } + } else return CMDDesc; } diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 7e2f651d..8732c7e1 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -426,23 +426,61 @@ namespace MinecraftClient /// The block to look at public void LookAtBlock(Location block) { - double dx = block.X - location.X, - dy = block.Y - location.Y, - dz = block.Z - location.Z; + double dx = block.X - (location.X - 0.5), + dy = block.Y - (location.Y + 1), + dz = block.Z - (location.Z - 0.5); double r = Math.Sqrt(dx * dx + dy * dy + dz * dz); float yaw = Convert.ToSingle(-Math.Atan2(dx, dz) / Math.PI * 180), pitch = Convert.ToSingle(-Math.Asin(dy / r) / Math.PI * 180); if (yaw < 0) yaw += 360; - - - ConsoleIO.WriteLineFormatted("YAW: " + yaw); - ConsoleIO.WriteLineFormatted("PITCH: " + pitch); UpdateLocation(location, yaw, pitch); } + /// + /// Look at specified angle + /// + /// The yaw to look at + /// The pitch to look at + public void LookAtAngle(float yaw, float pitch) + { + UpdateLocation(location, yaw, pitch); + } + + /// + /// Look in specified direction + /// + /// The direction too look in + 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); + } /// /// Move to the specified location