MatchVM.cs 4.0 KB

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