MatchVM.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Diagnostics;
  2. using Windows.UI.Xaml.Media;
  3. using Windows.UI.Xaml.Media.Imaging;
  4. using YJMPD_UWP.Helpers;
  5. namespace YJMPD_UWP.ViewModels
  6. {
  7. public class MatchVM : TemplateVM
  8. {
  9. private int angle;
  10. public MatchVM() : base("Match")
  11. {
  12. Error = "";
  13. Message = "";
  14. Degrees = 0;
  15. angle = (int)Util.DegreeBearing(App.Geo.Position.Coordinate.Point.Position, App.Game.Destination);
  16. HeadingVisible = false;
  17. App.Game.OnDestinationEnter += Game_OnDestinationEnter;
  18. App.Game.OnDestinationLeave += Game_OnDestinationLeave;
  19. App.Geo.OnPositionUpdate += Geo_OnPositionUpdate;
  20. App.Compass.OnHeadingUpdate += Compass_OnHeadingUpdate;
  21. }
  22. private void Compass_OnHeadingUpdate(object sender, Helpers.EventArgs.HeadingUpdatedEventArgs e)
  23. {
  24. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  25. {
  26. Degrees = (int)(angle + -e.Heading.HeadingMagneticNorth);
  27. NotifyPropertyChanged(nameof(Degrees));
  28. Debug.WriteLine("Degrees " + Degrees + " / " + angle);
  29. });
  30. }
  31. private void Geo_OnPositionUpdate(object sender, Helpers.EventArgs.PositionUpdatedEventArgs e)
  32. {
  33. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  34. {
  35. if (App.Game.Status == Model.GameHandler.GameStatus.STARTED)
  36. return;
  37. angle = (int)Util.DegreeBearing(e.Position.Coordinate.Point.Position, App.Game.Destination);
  38. HeadingVisible = Util.Distance(e.Position.Coordinate.Point.Position, App.Game.Destination) > 500;
  39. Debug.WriteLine(HeadingVisible);
  40. NotifyPropertyChanged(nameof(HeadingVisible));
  41. });
  42. }
  43. private void Game_OnDestinationLeave(object sender, System.EventArgs e)
  44. {
  45. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  46. {
  47. if (App.Game.Selected)
  48. Error = "Return to location!";
  49. NotifyPropertyChanged(nameof(ErrorVisible));
  50. NotifyPropertyChanged(nameof(Error));
  51. });
  52. }
  53. private void Game_OnDestinationEnter(object sender, System.EventArgs e)
  54. {
  55. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  56. {
  57. if (App.Game.Selected)
  58. Error = "";
  59. if (!App.Game.Selected)
  60. Message = "You reached the destination!";
  61. NotifyPropertyChanged(nameof(ErrorVisible));
  62. NotifyPropertyChanged(nameof(Error));
  63. NotifyPropertyChanged(nameof(MessageVisible));
  64. NotifyPropertyChanged(nameof(Message));
  65. });
  66. }
  67. public int Degrees
  68. {
  69. get; private set;
  70. }
  71. public bool HeadingVisible
  72. {
  73. get; private set;
  74. }
  75. public bool MessageVisible
  76. {
  77. get
  78. {
  79. return Message != "";
  80. }
  81. }
  82. public bool ErrorVisible
  83. {
  84. get
  85. {
  86. return Error != "";
  87. }
  88. }
  89. public string Message
  90. {
  91. get; private set;
  92. }
  93. public string Error
  94. {
  95. get; private set;
  96. }
  97. public ImageSource Photo
  98. {
  99. get
  100. {
  101. return new BitmapImage(new System.Uri(App.Photo.Photo));
  102. }
  103. }
  104. }
  105. }