/// <summary> /// Move along MyPath - always turn to face the direction of travel (or else I can get stuck /// behind a block that I cannot see). /// Return false if this move is likely to fail because the next square is blocked /// </summary> public bool MoveAlongMyPath() { if (MyPath != null && MyPathIndex < MyPath.Count) // not yet at end of path { PathSquare nextsquare = MyPath[MyPathIndex]; if (nextsquare.IsBlocked) { return(false); // next square is blocked so find a new path } Command.Move dir = GetMoveDirection(nextsquare.Parent, nextsquare); Command.Move rot = GetMoveRotation(dir); if (rot != Command.Move.Stay) { MyCommand = new Command(rot, false); // rotate to face direction of travel } else { MyCommand = new Command(dir, false); // move along path MyPathIndex++; // so go to next square of path } return(true); } else { return(false); } }
/// <summary> /// Get the Command.Move rotation that rotates your tank to face in Command.Move direction dir, /// or Command.Move.Stay if none exists. /// </summary> Command.Move GetMoveRotation(Command.Move dir) { switch (dir) { case Command.Move.Up: switch (MyWorldState.MyFacing) { case PlayerWorldState.Facing.Up: return(Command.Move.Stay); case PlayerWorldState.Facing.Down: return(Command.Move.RotateLeft); case PlayerWorldState.Facing.Left: return(Command.Move.RotateRight); case PlayerWorldState.Facing.Right: return(Command.Move.RotateLeft); } break; case Command.Move.Down: switch (MyWorldState.MyFacing) { case PlayerWorldState.Facing.Up: return(Command.Move.RotateLeft); case PlayerWorldState.Facing.Down: return(Command.Move.Stay); case PlayerWorldState.Facing.Left: return(Command.Move.RotateLeft); case PlayerWorldState.Facing.Right: return(Command.Move.RotateRight); } break; case Command.Move.Left: switch (MyWorldState.MyFacing) { case PlayerWorldState.Facing.Up: return(Command.Move.RotateLeft); case PlayerWorldState.Facing.Down: return(Command.Move.RotateRight); case PlayerWorldState.Facing.Left: return(Command.Move.Stay); case PlayerWorldState.Facing.Right: return(Command.Move.RotateLeft); } break; case Command.Move.Right: switch (MyWorldState.MyFacing) { case PlayerWorldState.Facing.Up: return(Command.Move.RotateRight); case PlayerWorldState.Facing.Down: return(Command.Move.RotateLeft); case PlayerWorldState.Facing.Left: return(Command.Move.RotateLeft); case PlayerWorldState.Facing.Right: return(Command.Move.Stay); } break; } return(Command.Move.Stay); // should never get here }