MatchVM.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if (App.Game.Status != Model.GameHandler.GameStatus.STARTED)
  27. return;
  28. Degrees = (int)(angle + -e.Heading.HeadingMagneticNorth);
  29. NotifyPropertyChanged(nameof(Degrees));
  30. });
  31. }
  32. private void Geo_OnPositionUpdate(object sender, Helpers.EventArgs.PositionUpdatedEventArgs e)
  33. {
  34. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  35. {
  36. if (App.Game.Status != Model.GameHandler.GameStatus.STARTED)
  37. return;
  38. angle = (int)Util.DegreeBearing(e.Position.Coordinate.Point.Position, App.Game.Destination);
  39. HeadingVisible = Util.Distance(e.Position.Coordinate.Point.Position, App.Game.Destination) > 500;
  40. NotifyPropertyChanged(nameof(HeadingVisible));
  41. NotifyPropertyChanged(nameof(InvHeadingVisible));
  42. });
  43. }
  44. private void Game_OnDestinationLeave(object sender, System.EventArgs e)
  45. {
  46. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  47. {
  48. if (App.Game.Selected)
  49. Error = "Return to location!";
  50. NotifyPropertyChanged(nameof(ErrorVisible));
  51. NotifyPropertyChanged(nameof(Error));
  52. });
  53. }
  54. private void Game_OnDestinationEnter(object sender, System.EventArgs e)
  55. {
  56. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  57. {
  58. if (App.Game.Selected)
  59. Error = "";
  60. if (!App.Game.Selected)
  61. Message = "You reached the destination!";
  62. NotifyPropertyChanged(nameof(ErrorVisible));
  63. NotifyPropertyChanged(nameof(Error));
  64. NotifyPropertyChanged(nameof(MessageVisible));
  65. NotifyPropertyChanged(nameof(Message));
  66. });
  67. }
  68. public int Degrees
  69. {
  70. get; private set;
  71. }
  72. public bool HeadingVisible
  73. {
  74. get; private set;
  75. }
  76. public bool InvHeadingVisible
  77. {
  78. get
  79. {
  80. return !HeadingVisible;
  81. }
  82. }
  83. public bool MessageVisible
  84. {
  85. get
  86. {
  87. return Message != "";
  88. }
  89. }
  90. public bool ErrorVisible
  91. {
  92. get
  93. {
  94. return Error != "";
  95. }
  96. }
  97. public string Message
  98. {
  99. get; private set;
  100. }
  101. public string Error
  102. {
  103. get; private set;
  104. }
  105. public ImageSource Photo
  106. {
  107. get
  108. {
  109. return new BitmapImage(new System.Uri(App.Photo.Photo));
  110. }
  111. }
  112. }
  113. }