Guest User

Untitled

a guest
Jun 8th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. using SaveTheWorld.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices.WindowsRuntime;
  7. using Windows.Foundation;
  8. using Windows.Foundation.Collections;
  9. using Windows.UI.Xaml;
  10. using Windows.UI.Xaml.Controls;
  11. using Windows.UI.Xaml.Controls.Primitives;
  12. using Windows.UI.Xaml.Data;
  13. using Windows.UI.Xaml.Input;
  14. using Windows.UI.Xaml.Media;
  15. using Windows.UI.Xaml.Navigation;
  16.  
  17. using Windows.UI.Xaml.Media.Animation;
  18.  
  19. // The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237
  20.  
  21. namespace SaveTheWorld
  22. {
  23. /// <summary>
  24. /// A basic page that provides characteristics common to most applications.
  25. /// </summary>
  26. public sealed partial class MainPage : Page
  27. {
  28. Random random = new Random();
  29. DispatcherTimer enemyTimer = new DispatcherTimer();
  30. DispatcherTimer targetTimer = new DispatcherTimer();
  31. bool humanCaptured = false;
  32.  
  33. private NavigationHelper navigationHelper;
  34. private ObservableDictionary defaultViewModel = new ObservableDictionary();
  35.  
  36. /// <summary>
  37. /// This can be changed to a strongly typed view model.
  38. /// </summary>
  39. public ObservableDictionary DefaultViewModel
  40. {
  41. get { return this.defaultViewModel; }
  42. }
  43.  
  44. /// <summary>
  45. /// NavigationHelper is used on each page to aid in navigation and
  46. /// process lifetime management
  47. /// </summary>
  48. public NavigationHelper NavigationHelper
  49. {
  50. get { return this.navigationHelper; }
  51. }
  52.  
  53.  
  54. public MainPage()
  55. {
  56. this.InitializeComponent();
  57. enemyTimer.Tick += enemyTimer_Tick;
  58. enemyTimer.Interval = TimeSpan.FromSeconds(2);
  59. targetTimer.Tick += targetTimer_Tick;
  60. targetTimer.Tick = TimeSpan.FromSeconds(.1);
  61. this.navigationHelper = new NavigationHelper(this);
  62. this.navigationHelper.LoadState += navigationHelper_LoadState;
  63. this.navigationHelper.SaveState += navigationHelper_SaveState;
  64. }
  65.  
  66. void targetTimer_Tick(object sender, object e)
  67. {
  68.  
  69. AddEnemy();
  70. }
  71.  
  72. private void EndTheGame()
  73. {
  74. if(!playArea.Children.Contains(gameOverText))
  75. {
  76. enemyTimer.Stop();
  77. targetTimer.Stop();
  78. humanCaptured = true;
  79. startButton.Visibility = Visibility.Visible;
  80. playArea.Children.Add(gameOverText);
  81. }
  82. }
  83.  
  84. void enemyTimer_Tick(object sender, object e)
  85. {
  86. progressBar.Value += 1;
  87. if (progressBar.Value >= progressBar.Maximum);
  88. EndTheGame();
  89. }
  90.  
  91. /// <summary>
  92. /// Populates the page with content passed during navigation. Any saved state is also
  93. /// provided when recreating a page from a prior session.
  94. /// </summary>
  95. /// <param name="sender">
  96. /// The source of the event; typically <see cref="NavigationHelper"/>
  97. /// </param>
  98. /// <param name="e">Event data that provides both the navigation parameter passed to
  99. /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
  100. /// a dictionary of state preserved by this page during an earlier
  101. /// session. The state will be null the first time a page is visited.</param>
  102. private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
  103. {
  104. }
  105.  
  106. /// <summary>
  107. /// Preserves state associated with this page in case the application is suspended or the
  108. /// page is discarded from the navigation cache. Values must conform to the serialization
  109. /// requirements of <see cref="SuspensionManager.SessionState"/>.
  110. /// </summary>
  111. /// <param name="sender">The source of the event; typically <see cref="NavigationHelper"/></param>
  112. /// <param name="e">Event data that provides an empty dictionary to be populated with
  113. /// serializable state.</param>
  114. private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
  115. {
  116. }
  117.  
  118. #region NavigationHelper registration
  119.  
  120. /// The methods provided in this section are simply used to allow
  121. /// NavigationHelper to respond to the page's navigation methods.
  122. ///
  123. /// Page specific logic should be placed in event handlers for the
  124. /// <see cref="GridCS.Common.NavigationHelper.LoadState"/>
  125. /// and <see cref="GridCS.Common.NavigationHelper.SaveState"/>.
  126. /// The navigation parameter is available in the LoadState method
  127. /// in addition to page state preserved during an earlier session.
  128.  
  129. protected override void OnNavigatedTo(NavigationEventArgs e)
  130. {
  131. navigationHelper.OnNavigatedTo(e);
  132. }
  133.  
  134. protected override void OnNavigatedFrom(NavigationEventArgs e)
  135. {
  136. navigationHelper.OnNavigatedFrom(e);
  137. }
  138.  
  139. #endregion
  140.  
  141. private void Button_Click(object sender, RoutedEventArgs e)
  142. {
  143. StartGame();
  144. }
  145.  
  146. private void StartGame()
  147. {
  148. human.IsHitTestVisible = true;
  149. humanCaptured = false;
  150. progressBar.Value = 0;
  151. startButton.Visibility = Visibility.Collapsed;
  152. playArea.Children.Clear();
  153. playArea.Children.Add(target);
  154. playArea.Children.Add(human);
  155. enemyTimer.Start();
  156. }
  157.  
  158. private void AddEnemy()
  159. {
  160. ContentControl enemy = new ContentControl();
  161. enemy.Template = Resources["enemyTemplate"] as ControlTemplate;
  162. AnimateEnemy(enemy, 0, playArea.ActualWidth - 100, "(Canvas.Left)");
  163. AnimateEnemy(enemy, random.Next((int)playArea.ActualHeight - 100),
  164. random.Next((int)playArea.ActualHeight - 100), "(Canvas.Top)");
  165. playArea.Children.Add(enemy);
  166. }
  167.  
  168. private void AnimateEnemy(ContentControl enemy, double from, double to, string propertyToAnimate)
  169. {
  170. Storyboard storyboard = new Storyboard()
  171. {
  172. AutoReverse = true,
  173. RepeatBehavior = RepeatBehavior.Forever
  174. };
  175. DoubleAnimation animation = new DoubleAnimation()
  176. {
  177. From = from,
  178. To = to,
  179. Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)))
  180. };
  181. Storyboard.SetTarget(animation, enemy);
  182. Storyboard.SetTargetProperty(animation, propertyToAnimate);
  183. storyboard.Children.Add(animation);
  184. storyboard.Begin();
  185.  
  186. }
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment