private async Task TurnAndReadActualDirection(double desiredDirection, ConsoleKey key) { // Press Right input.SetKeyState(key, true, true, "PlayerDirection"); var startTime = DateTime.Now; // Wait until we are going the right direction while ((DateTime.Now - startTime).TotalSeconds < 10) { if ((DateTime.Now - startTime).TotalSeconds > 10) { await Task.Delay(1); } System.Threading.Thread.Sleep(1); var actualDirection = playerReader.Direction; bool closeEnoughToDesiredDirection = Math.Abs(actualDirection - desiredDirection) < 0.01; if (closeEnoughToDesiredDirection) { Log("Close enough, stopping turn"); input.SetKeyState(key, false, true, "PlayerDirection"); break; } bool goingTheWrongWay = GetDirectionKeyToPress(desiredDirection) != key; if (goingTheWrongWay) { Log("GOING THE WRONG WAY! Stop turn"); input.SetKeyState(key, false, true, "PlayerDirection"); break; } } }
public async Task Stop() { if (XCoord != playerReader.XCoord || YCoord != playerReader.YCoord) { input.SetKeyState(ConsoleKey.UpArrow, false, false, "StopMoving"); await Task.Delay(1); } this.XCoord = playerReader.XCoord; this.YCoord = playerReader.YCoord; await StopTurn(); }
public async Task StopForward() { if (XCoord != playerReader.XCoord || YCoord != playerReader.YCoord) { if (!input.IsKeyDown(ConsoleKey.DownArrow) && !input.IsKeyDown(ConsoleKey.UpArrow) && (Math.Abs(XCoord - playerReader.XCoord) > MinDist || Math.Abs(YCoord - playerReader.YCoord) > MinDist)) { input.SetKeyState(ConsoleKey.UpArrow, true, false, "StopForward - Cancel interact"); await Task.Delay(1); } input.SetKeyState(ConsoleKey.UpArrow, false, false, ""); input.SetKeyState(ConsoleKey.DownArrow, false, false, "StopForward"); await Task.Delay(10); } this.XCoord = playerReader.XCoord; this.YCoord = playerReader.YCoord; }
public async Task Stop() { if (XCoord != playerReader.XCoord || YCoord != playerReader.YCoord) { input.SetKeyState(ConsoleKey.UpArrow, false, false, "StopMoving"); await Task.Delay(1); } if (Direction != playerReader.Direction) { input.SetKeyState(ConsoleKey.LeftArrow, false, false, "StopMoving"); await Task.Delay(1); input.SetKeyState(ConsoleKey.RightArrow, false, false, "StopMoving"); await Task.Delay(1); } this.Direction = playerReader.Direction; this.XCoord = playerReader.XCoord; this.YCoord = playerReader.YCoord; }