MatchVM.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 = true;
  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.HeadingTrueNorth);
  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. double b = Util.Distance(e.Position.Coordinate.Point.Position, App.Game.Destination) * 1000;
  40. HeadingVisible = (int)b > 250;
  41. NotifyPropertyChanged(nameof(HeadingVisible));
  42. NotifyPropertyChanged(nameof(InvHeadingVisible));
  43. });
  44. }
  45. private void Game_OnDestinationLeave(object sender, System.EventArgs e)
  46. {
  47. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  48. {
  49. if (App.Game.Selected)
  50. Error = "Return to location!";
  51. NotifyPropertyChanged(nameof(ErrorVisible));
  52. NotifyPropertyChanged(nameof(Error));
  53. });
  54. }
  55. private void Game_OnDestinationEnter(object sender, System.EventArgs e)
  56. {
  57. dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  58. {
  59. if (App.Game.Selected)
  60. Error = "";
  61. if (!App.Game.Selected)
  62. Message = "You reached the destination!";
  63. NotifyPropertyChanged(nameof(ErrorVisible));
  64. NotifyPropertyChanged(nameof(Error));
  65. NotifyPropertyChanged(nameof(MessageVisible));
  66. NotifyPropertyChanged(nameof(Message));
  67. });
  68. }
  69. public int Degrees
  70. {
  71. get; private set;
  72. }
  73. public bool HeadingVisible
  74. {
  75. get; private set;
  76. }
  77. public bool InvHeadingVisible
  78. {
  79. get
  80. {
  81. return !HeadingVisible;
  82. }
  83. }
  84. public bool MessageVisible
  85. {
  86. get
  87. {
  88. return Message != "";
  89. }
  90. }
  91. public bool ErrorVisible
  92. {
  93. get
  94. {
  95. return Error != "";
  96. }
  97. }
  98. public string Message
  99. {
  100. get; private set;
  101. }
  102. public string Error
  103. {
  104. get; private set;
  105. }
  106. public ImageSource Photo
  107. {
  108. get
  109. {
  110. return new BitmapImage(new System.Uri(App.Photo.Photo));
  111. }
  112. }
  113. }
  114. }