Advertisement
2019PASTEBIN2019

Untitled

Jul 5th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 79.08 KB | None | 0 0
  1. main window
  2. ***************************************************
  3.  
  4.  
  5.  
  6. //------------------------------------------------------------------------------
  7. // <copyright file="MainWindow.xaml.cs" company="Microsoft">
  8. // Copyright (c) Microsoft Corporation. All rights reserved.
  9. // </copyright>
  10. //------------------------------------------------------------------------------
  11.  
  12. // This module contains code to do Kinect NUI initialization,
  13. // processing, displaying players on screen, and sending updated player
  14. // positions to the game portion for hit testing.
  15.  
  16.  
  17. //*******************הערה חשובה************************
  18.  
  19. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$// הוספת הערה שלי
  20.  
  21. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//הוספת הערה שלי לא בטוחה בנכונותה
  22.  
  23. //$$ שורות קוד שאני הוספתי
  24.  
  25. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  26. //... כל הקטע התחום אני הוספתי
  27. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  28. //*******************************************************
  29.  
  30.  
  31.  
  32. namespace ShapeGame
  33. {
  34. using System;
  35. using System.Collections.Generic;
  36. using System.ComponentModel;
  37. using System.Linq;
  38. using System.Media;
  39. using System.Runtime.InteropServices;
  40. using System.Threading;
  41. using System.Windows;
  42. using System.Windows.Controls;
  43. using System.Windows.Data;
  44. using System.Windows.Threading;
  45. using Microsoft.Kinect;
  46. using Microsoft.Kinect.Toolkit;
  47. using Microsoft.Samples.Kinect.WpfViewers;
  48. using ShapeGame.Speech;
  49. using ShapeGame.Utils;
  50.  
  51. /// <summary>
  52. /// Interaction logic for MainWindow.xaml
  53. /// </summary>
  54. ///
  55. public partial class MainWindow : Window
  56. {
  57. public static readonly DependencyProperty KinectSensorManagerProperty =
  58. DependencyProperty.Register(
  59. "KinectSensorManager",
  60. typeof(KinectSensorManager),
  61. typeof(MainWindow),
  62. new PropertyMetadata(null));
  63.  
  64. #region Private State
  65. private const int TimerResolution = 2; // ms
  66. private const int NumIntraFrames = 3;
  67. private const int MaxShapes = 80;
  68. private const double MaxFramerate = 70;
  69. private const double MinFramerate = 15;
  70. private const double MinShapeSize = 12;
  71. private const double MaxShapeSize = 90;
  72. private const double DefaultDropRate = 2.5;
  73. private const double DefaultDropSize = 32.0;
  74. private const double DefaultDropGravity = 1.0;
  75.  
  76. private readonly Dictionary<int, Player> players = new Dictionary<int, Player>();
  77. private readonly SoundPlayer popSound = new SoundPlayer();
  78. private readonly SoundPlayer hitSound = new SoundPlayer();
  79. private readonly SoundPlayer squeezeSound = new SoundPlayer();
  80. private readonly KinectSensorChooser sensorChooser = new KinectSensorChooser();
  81.  
  82. private double dropRate = DefaultDropRate;
  83. private double dropSize = DefaultDropSize;
  84. private double dropGravity = DefaultDropGravity;
  85. private DateTime lastFrameDrawn = DateTime.MinValue;
  86. private DateTime predNextFrame = DateTime.MinValue;
  87. private double actualFrameTime;
  88.  
  89.  
  90.  
  91. private Skeleton[] skeletonData;
  92.  
  93. // Player(s) placement in scene (z collapsed):
  94. private Rect playerBounds;
  95. private Rect screenRect;
  96.  
  97. private double targetFramerate = MaxFramerate;
  98. private int frameCount;
  99. private bool runningGameThread;
  100. private FallingThings myFallingThings;
  101. private int playersAlive;
  102. //$$$$$$$$$$$$$$$$
  103. private int numOfPlayers = 0;
  104. //$$$$$$$$$$$$$$$
  105.  
  106.  
  107. private SpeechRecognizer mySpeechRecognizer;
  108. #endregion Private State
  109.  
  110. #region ctor + Window Events
  111.  
  112. public MainWindow()
  113. {
  114.  
  115.  
  116.  
  117. this.KinectSensorManager = new KinectSensorManager();
  118. this.KinectSensorManager.KinectSensorChanged += this.KinectSensorChanged;
  119. this.DataContext = this.KinectSensorManager;
  120.  
  121. InitializeComponent();
  122.  
  123. this.SensorChooserUI.KinectSensorChooser = sensorChooser;
  124. sensorChooser.Start();
  125.  
  126. // Bind the KinectSensor from the sensorChooser to the KinectSensor on the KinectSensorManager
  127. var kinectSensorBinding = new Binding("Kinect") { Source = this.sensorChooser };
  128. BindingOperations.SetBinding(this.KinectSensorManager, KinectSensorManager.KinectSensorProperty, kinectSensorBinding);
  129.  
  130. this.RestoreWindowState();
  131. }
  132.  
  133. public KinectSensorManager KinectSensorManager
  134. {
  135. get { return (KinectSensorManager)GetValue(KinectSensorManagerProperty); }
  136. set { SetValue(KinectSensorManagerProperty, value); }
  137. }
  138.  
  139. // Since the timer resolution defaults to about 10ms precisely, we need to
  140. // increase the resolution to get framerates above between 50fps with any
  141. // consistency.
  142. [DllImport("Winmm.dll", EntryPoint = "timeBeginPeriod")]
  143. private static extern int TimeBeginPeriod(uint period);
  144.  
  145. private void RestoreWindowState()
  146. {
  147. // Restore window state to that last used
  148. Rect bounds = Properties.Settings.Default.PrevWinPosition;
  149. if (bounds.Right != bounds.Left)
  150. {
  151. this.Top = bounds.Top;
  152. this.Left = bounds.Left;
  153. this.Height = bounds.Height;
  154. this.Width = bounds.Width;
  155. }
  156.  
  157. this.WindowState = (WindowState)Properties.Settings.Default.WindowState;
  158. }
  159.  
  160. private void WindowLoaded(object sender, EventArgs e)
  161. {
  162. playfield.ClipToBounds = true;
  163.  
  164. this.myFallingThings = new FallingThings(MaxShapes, this.targetFramerate, NumIntraFrames);
  165.  
  166. this.UpdatePlayfieldSize();
  167.  
  168. this.myFallingThings.SetGravity(this.dropGravity);
  169. this.myFallingThings.SetDropRate(this.dropRate);
  170. this.myFallingThings.SetSize(this.dropSize);
  171. this.myFallingThings.SetPolies(PolyType.All);
  172. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  173. //בזמן טעינת חלון המשחק מצב המשחק-לכבוי
  174. this.myFallingThings.SetGameMode(GameMode.Off);
  175.  
  176. this.popSound.Stream = Properties.Resources.Pop_5;
  177. this.hitSound.Stream = Properties.Resources.Hit_2;
  178. this.squeezeSound.Stream = Properties.Resources.Squeeze;
  179.  
  180. this.popSound.Play();
  181.  
  182. TimeBeginPeriod(TimerResolution);
  183. var myGameThread = new Thread(this.GameThread);
  184. myGameThread.SetApartmentState(ApartmentState.STA);
  185. myGameThread.Start();
  186.  
  187. FlyingText.NewFlyingText(this.screenRect.Width / 30, new Point(this.screenRect.Width / 2, this.screenRect.Height / 2), "Shapes!");
  188. }
  189.  
  190. private void WindowClosing(object sender, CancelEventArgs e)
  191. {
  192. sensorChooser.Stop();
  193.  
  194. this.runningGameThread = false;
  195. Properties.Settings.Default.PrevWinPosition = this.RestoreBounds;
  196. Properties.Settings.Default.WindowState = (int)this.WindowState;
  197. Properties.Settings.Default.Save();
  198. }
  199.  
  200. private void WindowClosed(object sender, EventArgs e)
  201. {
  202. this.KinectSensorManager.KinectSensor = null;
  203. }
  204.  
  205. #endregion ctor + Window Events
  206.  
  207. #region Kinect discovery + setup
  208.  
  209. private void KinectSensorChanged(object sender, KinectSensorManagerEventArgs<KinectSensor> args)
  210. {
  211. if (null != args.OldValue)
  212. {
  213. this.UninitializeKinectServices(args.OldValue);
  214. }
  215.  
  216. // Only enable this checkbox if we have a sensor
  217. enableAec.IsEnabled = null != args.NewValue;
  218.  
  219. if (null != args.NewValue)
  220. {
  221. this.InitializeKinectServices(this.KinectSensorManager, args.NewValue);
  222. }
  223. }
  224.  
  225. // Kinect enabled apps should customize which Kinect services it initializes here.
  226. private void InitializeKinectServices(KinectSensorManager kinectSensorManager, KinectSensor sensor)
  227. {
  228. // Application should enable all streams first.
  229. kinectSensorManager.ColorFormat = ColorImageFormat.RgbResolution640x480Fps30;
  230. kinectSensorManager.ColorStreamEnabled = true;
  231.  
  232. sensor.SkeletonFrameReady += this.SkeletonsReady;
  233. kinectSensorManager.TransformSmoothParameters = new TransformSmoothParameters
  234. {
  235. Smoothing = 0.5f,
  236. Correction = 0.5f,
  237. Prediction = 0.5f,
  238. JitterRadius = 0.05f,
  239. MaxDeviationRadius = 0.04f
  240. };
  241. kinectSensorManager.SkeletonStreamEnabled = true;
  242. kinectSensorManager.KinectSensorEnabled = true;
  243.  
  244. if (!kinectSensorManager.KinectSensorAppConflict)
  245. {
  246. // Start speech recognizer after KinectSensor started successfully.
  247. this.mySpeechRecognizer = SpeechRecognizer.Create();
  248.  
  249. if (null != this.mySpeechRecognizer)
  250. {
  251. this.mySpeechRecognizer.SaidSomething += this.RecognizerSaidSomething;
  252. this.mySpeechRecognizer.Start(sensor.AudioSource);
  253. }
  254.  
  255. enableAec.Visibility = Visibility.Visible;
  256. this.UpdateEchoCancellation(this.enableAec);
  257. }
  258. }
  259.  
  260. // Kinect enabled apps should uninitialize all Kinect services that were initialized in InitializeKinectServices() here.
  261. private void UninitializeKinectServices(KinectSensor sensor)
  262. {
  263. sensor.SkeletonFrameReady -= this.SkeletonsReady;
  264.  
  265. if (null != this.mySpeechRecognizer)
  266. {
  267. this.mySpeechRecognizer.Stop();
  268. this.mySpeechRecognizer.SaidSomething -= this.RecognizerSaidSomething;
  269. this.mySpeechRecognizer.Dispose();
  270. this.mySpeechRecognizer = null;
  271. }
  272.  
  273. enableAec.Visibility = Visibility.Collapsed;
  274. }
  275.  
  276. #endregion Kinect discovery + setup
  277.  
  278. #region Kinect Skeleton processing
  279. private void SkeletonsReady(object sender, SkeletonFrameReadyEventArgs e)
  280. {
  281. using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
  282. {
  283. if (skeletonFrame != null)
  284. {
  285. int skeletonSlot = 0;
  286.  
  287. if ((this.skeletonData == null) || (this.skeletonData.Length != skeletonFrame.SkeletonArrayLength))
  288. {
  289. this.skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength];
  290. }
  291.  
  292. skeletonFrame.CopySkeletonDataTo(this.skeletonData);
  293.  
  294.  
  295. foreach (Skeleton skeleton in this.skeletonData)
  296. {
  297.  
  298. if (SkeletonTrackingState.Tracked == skeleton.TrackingState)
  299. {
  300. Player player;
  301.  
  302.  
  303. if (this.players.ContainsKey(skeletonSlot))
  304. {
  305.  
  306. player = this.players[skeletonSlot];
  307. }
  308. else
  309. {
  310.  
  311. player = new Player(skeletonSlot);
  312.  
  313. player.SetJointsAndBonesBrushesOfPlayers(players.Count);
  314.  
  315. player.SetBounds(this.playerBounds);
  316. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  317. //קן חדש כלומר בתחילת המשחק/כאשר נקודת הכיול נעלמה
  318. //כאשר נקודת הכיול נעלמת הוא מסיר את השחקן מהמערך-גודל המערך כמו בתחילת המשחק שווה ל0
  319. //במקרה שיש יותר משחקן אחד מול המצלמה הוא בעצם רוצה להוסיף למערך פעמיים
  320. //התנאי הזה יגרום לו להוסיף תמיד פעם אחת בלבד
  321. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  322.  
  323. if (players.Count == 0) //$$
  324. {
  325. this.players.Add(skeletonSlot, player);
  326. string s = "add" + skeletonSlot.ToString() + "," + player.GetId();
  327. }
  328.  
  329. }
  330.  
  331. player.LastUpdated = DateTime.Now;
  332.  
  333. // Update player's bone and joint positions
  334. if (skeleton.Joints.Count > 0)
  335. {
  336. player.IsAlive = true;
  337.  
  338. // Head, hands, feet (hit testing happens in order here)
  339. player.UpdateJointPosition(skeleton.Joints, JointType.Head);
  340. player.UpdateJointPosition(skeleton.Joints, JointType.HandLeft);
  341. player.UpdateJointPosition(skeleton.Joints, JointType.HandRight);
  342. player.UpdateJointPosition(skeleton.Joints, JointType.FootLeft);
  343. player.UpdateJointPosition(skeleton.Joints, JointType.FootRight);
  344.  
  345. // Hands and arms
  346. player.UpdateBonePosition(skeleton.Joints, JointType.HandRight, JointType.WristRight);
  347. player.UpdateBonePosition(skeleton.Joints, JointType.WristRight, JointType.ElbowRight);
  348. player.UpdateBonePosition(skeleton.Joints, JointType.ElbowRight, JointType.ShoulderRight);
  349.  
  350. player.UpdateBonePosition(skeleton.Joints, JointType.HandLeft, JointType.WristLeft);
  351. player.UpdateBonePosition(skeleton.Joints, JointType.WristLeft, JointType.ElbowLeft);
  352. player.UpdateBonePosition(skeleton.Joints, JointType.ElbowLeft, JointType.ShoulderLeft);
  353.  
  354. // Head and Shoulders
  355. player.UpdateBonePosition(skeleton.Joints, JointType.ShoulderCenter, JointType.Head);
  356. player.UpdateBonePosition(skeleton.Joints, JointType.ShoulderLeft, JointType.ShoulderCenter);
  357. player.UpdateBonePosition(skeleton.Joints, JointType.ShoulderCenter, JointType.ShoulderRight);
  358.  
  359. // Legs
  360. player.UpdateBonePosition(skeleton.Joints, JointType.HipLeft, JointType.KneeLeft);
  361. player.UpdateBonePosition(skeleton.Joints, JointType.KneeLeft, JointType.AnkleLeft);
  362. player.UpdateBonePosition(skeleton.Joints, JointType.AnkleLeft, JointType.FootLeft);
  363.  
  364. player.UpdateBonePosition(skeleton.Joints, JointType.HipRight, JointType.KneeRight);
  365. player.UpdateBonePosition(skeleton.Joints, JointType.KneeRight, JointType.AnkleRight);
  366. player.UpdateBonePosition(skeleton.Joints, JointType.AnkleRight, JointType.FootRight);
  367.  
  368. player.UpdateBonePosition(skeleton.Joints, JointType.HipLeft, JointType.HipCenter);
  369. player.UpdateBonePosition(skeleton.Joints, JointType.HipCenter, JointType.HipRight);
  370.  
  371. // Spine
  372. player.UpdateBonePosition(skeleton.Joints, JointType.HipCenter, JointType.ShoulderCenter);
  373. }
  374. }
  375.  
  376. skeletonSlot++;
  377. }
  378. }
  379. }
  380. }
  381.  
  382. private void CheckPlayers()
  383. {
  384. foreach (var player in this.players)
  385. {
  386.  
  387. if (!player.Value.IsAlive)
  388. {
  389. // Player left scene since we aren't tracking it anymore, so remove from dictionary
  390. this.players.Remove(player.Value.GetId());
  391. break;
  392. }
  393. }
  394.  
  395. // Count alive players
  396. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  397. //alive-מספר השחקנים שיש במשחק
  398. int alive = this.players.Count(player => player.Value.IsAlive);
  399.  
  400. if (alive != this.playersAlive)
  401. {
  402. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  403. //ישנם 2 שחקנים במשחק ולכן מצב המשחק =ל2 שחקנים
  404. if (alive == 2)
  405. {
  406. this.myFallingThings.SetGameMode(GameMode.TwoPlayer);
  407. }
  408. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  409. //ישנו שחקן אחד בלבד במשחק ולכן מצב המשחק =ל2שחקן אחד
  410. else if (alive == 1)
  411. {
  412. this.myFallingThings.SetGameMode(GameMode.Solo);
  413. }
  414. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  415. //אים אף שחקן במשחק ולכן מצב המשחק=ל כבוי
  416. else if (alive == 0)
  417. {
  418. this.myFallingThings.SetGameMode(GameMode.Off);
  419. }
  420.  
  421. if ((this.playersAlive == 0) && (this.mySpeechRecognizer != null))
  422. {
  423. BannerText.NewBanner(
  424. Properties.Resources.Vocabulary,
  425. this.screenRect,
  426. true,
  427. System.Windows.Media.Color.FromArgb(200, 255, 255, 255));
  428. }
  429.  
  430. this.playersAlive = alive;
  431. }
  432. }
  433.  
  434. private void PlayfieldSizeChanged(object sender, SizeChangedEventArgs e)
  435. {
  436. this.UpdatePlayfieldSize();
  437. }
  438.  
  439. private void UpdatePlayfieldSize()
  440. {
  441. // Size of player wrt size of playfield, putting ourselves low on the screen.
  442. this.screenRect.X = 0;
  443. this.screenRect.Y = 0;
  444. this.screenRect.Width = this.playfield.ActualWidth;
  445. this.screenRect.Height = this.playfield.ActualHeight;
  446.  
  447. BannerText.UpdateBounds(this.screenRect);
  448.  
  449. this.playerBounds.X = 0;
  450. this.playerBounds.Width = this.playfield.ActualWidth;
  451. this.playerBounds.Y = this.playfield.ActualHeight * 0.2;
  452. this.playerBounds.Height = this.playfield.ActualHeight * 0.75;
  453.  
  454. foreach (var player in this.players)
  455. {
  456. player.Value.SetBounds(this.playerBounds);
  457. }
  458.  
  459. Rect fallingBounds = this.playerBounds;
  460. fallingBounds.Y = 0;
  461. fallingBounds.Height = playfield.ActualHeight;
  462. if (this.myFallingThings != null)
  463. {
  464. this.myFallingThings.SetBoundaries(fallingBounds);
  465. }
  466. }
  467. #endregion Kinect Skeleton processing
  468.  
  469. #region GameTimer/Thread
  470. private void GameThread()
  471. {
  472. this.runningGameThread = true;
  473. this.predNextFrame = DateTime.Now;
  474. this.actualFrameTime = 1000.0 / this.targetFramerate;
  475.  
  476. // Try to dispatch at as constant of a framerate as possible by sleeping just enough since
  477. // the last time we dispatched.
  478. while (this.runningGameThread)
  479. {
  480. // Calculate average framerate.
  481. DateTime now = DateTime.Now;
  482. if (this.lastFrameDrawn == DateTime.MinValue)
  483. {
  484. this.lastFrameDrawn = now;
  485. }
  486.  
  487. double ms = now.Subtract(this.lastFrameDrawn).TotalMilliseconds;
  488. this.actualFrameTime = (this.actualFrameTime * 0.95) + (0.05 * ms);
  489. this.lastFrameDrawn = now;
  490.  
  491. // Adjust target framerate down if we're not achieving that rate
  492. this.frameCount++;
  493. if ((this.frameCount % 100 == 0) && (1000.0 / this.actualFrameTime < this.targetFramerate * 0.92))
  494. {
  495. this.targetFramerate = Math.Max(MinFramerate, (this.targetFramerate + (1000.0 / this.actualFrameTime)) / 2);
  496. }
  497.  
  498. if (now > this.predNextFrame)
  499. {
  500. this.predNextFrame = now;
  501. }
  502. else
  503. {
  504. double milliseconds = this.predNextFrame.Subtract(now).TotalMilliseconds;
  505. if (milliseconds >= TimerResolution)
  506. {
  507. Thread.Sleep((int)(milliseconds + 0.5));
  508. }
  509. }
  510.  
  511. this.predNextFrame += TimeSpan.FromMilliseconds(1000.0 / this.targetFramerate);
  512.  
  513. this.Dispatcher.Invoke(DispatcherPriority.Send, new Action<int>(this.HandleGameTimer), 0);
  514. }
  515. }
  516.  
  517. private void HandleGameTimer(int param)
  518. {
  519. // Every so often, notify what our actual framerate is
  520. if ((this.frameCount % 100) == 0)
  521. {
  522. this.myFallingThings.SetFramerate(1000.0 / this.actualFrameTime);
  523. }
  524.  
  525. // Advance animations, and do hit testing.
  526. for (int i = 0; i < NumIntraFrames; ++i)
  527. {
  528. foreach (var pair in this.players)
  529. {
  530.  
  531.  
  532. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  533. //ברגע שיש כבר שחקן אחד במערך-ברגע שיציג שחקן לפחות אחד על המסך יתחיל לחפש פגיעה
  534. HitType hit = this.myFallingThings.LookForHits(pair.Value.Segments, pair.Value.GetId());
  535.  
  536.  
  537. //@@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  538. //@@אם היתה פגיעה ב"סינג" לא משנה מאיזה סוג
  539. if (hit != HitType.None)//@@
  540. { //@@
  541. //@@var id =pair.Key;
  542. //@@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  543. //@@צביעת הסגמנט שפגע ב"סינג
  544. //@@KeyValuePair<Bone, BoneData> s = myFallingThings.GetSegmentOfCurrentHitThing();
  545. //@@players[id].SetSegmentBrushOfCurrentHitThing(s);
  546.  
  547. this.squeezeSound.Play();
  548. this.players[pair.Key].jointsBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 189, 0));//צהוב
  549. this.players[pair.Key].bonesBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(13, 120, 61)); //ירוק
  550.  
  551. }//@@
  552. if ((hit & HitType.Squeezed) != 0)//bbb
  553. {
  554. this.squeezeSound.Play();
  555. }
  556. else if ((hit & HitType.Popped) != 0)//bbb
  557. {
  558. this.popSound.Play();
  559. }
  560. else if ((hit & HitType.Hand) != 0)//bbb
  561. {
  562. this.hitSound.Play();
  563. }
  564. }
  565.  
  566. this.myFallingThings.AdvanceFrame();
  567. }
  568.  
  569. // Draw new Wpf scene by adding all objects to canvas
  570. playfield.Children.Clear();
  571. this.myFallingThings.DrawFrame(this.playfield.Children);
  572. foreach (var player in this.players)
  573. {
  574.  
  575. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  576. //לדעת כמה שחקנים יש במערך בכל זמן-לשם בדיקה
  577. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  578.  
  579. if (players.Count > 1)//$$
  580. numOfPlayers= players.Count;//$$
  581. player.Value.Draw(playfield.Children);
  582. }
  583.  
  584. BannerText.Draw(playfield.Children);
  585. FlyingText.Draw(playfield.Children);
  586.  
  587.  
  588.  
  589. this.CheckPlayers();
  590. }
  591. #endregion GameTimer/Thread
  592.  
  593. #region Kinect Speech processing
  594. private void RecognizerSaidSomething(object sender, SpeechRecognizer.SaidSomethingEventArgs e)
  595. {
  596. FlyingText.NewFlyingText(this.screenRect.Width / 30, new Point(this.screenRect.Width / 2, this.screenRect.Height / 2), e.Matched);
  597. switch (e.Verb)
  598. {
  599. case SpeechRecognizer.Verbs.Pause:
  600. this.myFallingThings.SetDropRate(0);
  601. this.myFallingThings.SetGravity(0);
  602. break;
  603. case SpeechRecognizer.Verbs.Resume:
  604. this.myFallingThings.SetDropRate(this.dropRate);
  605. this.myFallingThings.SetGravity(this.dropGravity);
  606. break;
  607. case SpeechRecognizer.Verbs.Reset:
  608. this.dropRate = DefaultDropRate;
  609. this.dropSize = DefaultDropSize;
  610. this.dropGravity = DefaultDropGravity;
  611. this.myFallingThings.SetPolies(PolyType.All);
  612. this.myFallingThings.SetDropRate(this.dropRate);
  613. this.myFallingThings.SetGravity(this.dropGravity);
  614. this.myFallingThings.SetSize(this.dropSize);
  615. this.myFallingThings.SetShapesColor(System.Windows.Media.Color.FromRgb(0, 0, 0), true);
  616. this.myFallingThings.Reset();
  617. break;
  618. case SpeechRecognizer.Verbs.DoShapes:
  619. this.myFallingThings.SetPolies(e.Shape);
  620. break;
  621. case SpeechRecognizer.Verbs.RandomColors:
  622. this.myFallingThings.SetShapesColor(System.Windows.Media.Color.FromRgb(0, 0, 0), true);
  623. break;
  624. case SpeechRecognizer.Verbs.Colorize:
  625. this.myFallingThings.SetShapesColor(e.RgbColor, false);
  626. break;
  627. case SpeechRecognizer.Verbs.ShapesAndColors:
  628. this.myFallingThings.SetPolies(e.Shape);
  629. this.myFallingThings.SetShapesColor(e.RgbColor, false);
  630. break;
  631. case SpeechRecognizer.Verbs.More:
  632. this.dropRate *= 1.5;
  633. this.myFallingThings.SetDropRate(this.dropRate);
  634. break;
  635. case SpeechRecognizer.Verbs.Fewer:
  636. this.dropRate /= 1.5;
  637. this.myFallingThings.SetDropRate(this.dropRate);
  638. break;
  639. case SpeechRecognizer.Verbs.Bigger:
  640. this.dropSize *= 1.5;
  641. if (this.dropSize > MaxShapeSize)
  642. {
  643. this.dropSize = MaxShapeSize;
  644. }
  645.  
  646. this.myFallingThings.SetSize(this.dropSize);
  647. break;
  648. case SpeechRecognizer.Verbs.Biggest:
  649. this.dropSize = MaxShapeSize;
  650. this.myFallingThings.SetSize(this.dropSize);
  651. break;
  652. case SpeechRecognizer.Verbs.Smaller:
  653. this.dropSize /= 1.5;
  654. if (this.dropSize < MinShapeSize)
  655. {
  656. this.dropSize = MinShapeSize;
  657. }
  658.  
  659. this.myFallingThings.SetSize(this.dropSize);
  660. break;
  661. case SpeechRecognizer.Verbs.Smallest:
  662. this.dropSize = MinShapeSize;
  663. this.myFallingThings.SetSize(this.dropSize);
  664. break;
  665. case SpeechRecognizer.Verbs.Faster:
  666. this.dropGravity *= 1.25;
  667. if (this.dropGravity > 4.0)
  668. {
  669. this.dropGravity = 4.0;
  670. }
  671.  
  672. this.myFallingThings.SetGravity(this.dropGravity);
  673. break;
  674. case SpeechRecognizer.Verbs.Slower:
  675. this.dropGravity /= 1.25;
  676. if (this.dropGravity < 0.25)
  677. {
  678. this.dropGravity = 0.25;
  679. }
  680.  
  681. this.myFallingThings.SetGravity(this.dropGravity);
  682. break;
  683. }
  684. }
  685.  
  686. private void EnableAecChecked(object sender, RoutedEventArgs e)
  687. {
  688. var enableAecCheckBox = (CheckBox)sender;
  689. this.UpdateEchoCancellation(enableAecCheckBox);
  690. }
  691.  
  692. private void UpdateEchoCancellation(CheckBox aecCheckBox)
  693. {
  694. this.mySpeechRecognizer.EchoCancellationMode = aecCheckBox.IsChecked != null && aecCheckBox.IsChecked.Value
  695. ? EchoCancellationMode.CancellationAndSuppression
  696. : EchoCancellationMode.None;
  697. }
  698.  
  699. #endregion Kinect Speech processing
  700. }
  701. }
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720. player
  721. *********************************************
  722. //------------------------------------------------------------------------------
  723. // <copyright file="Player.cs" company="Microsoft">
  724. // Copyright (c) Microsoft Corporation. All rights reserved.
  725. // </copyright>
  726. //------------------------------------------------------------------------------
  727.  
  728. namespace ShapeGame
  729. {
  730. using System;
  731. using System.Collections.Generic;
  732. using System.Linq;
  733. using System.Windows;
  734. using System.Windows.Controls;
  735. using System.Windows.Media;
  736. using System.Windows.Shapes;
  737. using Microsoft.Kinect;
  738. using ShapeGame.Utils;
  739.  
  740. public class Player
  741. {
  742. private const double BoneSize = 0.01;
  743. private const double HeadSize = 0.075;
  744. private const double HandSize = 0.03;
  745.  
  746. // Keeping track of all bone segments of interest as well as head, hands and feet
  747. private /*readonly*/ Dictionary<Bone, BoneData> segments = new Dictionary<Bone, BoneData>();
  748. public /*$$ readonly $$*/ System.Windows.Media.Brush jointsBrush;
  749. public /*$$ readonly $$*/ System.Windows.Media.Brush bonesBrush;
  750. /*private$$*/ public readonly int id;
  751. private static int colorId;
  752. private Rect playerBounds;
  753. private System.Windows.Point playerCenter;
  754. private double playerScale;
  755. //$$$$$$$$$$$$4
  756. private int i =0;
  757. int indexSegmentHit = 0;
  758. /*private$$*/ public KeyValuePair<System.Windows.Media.Brush, System.Windows.Media.Brush> jointsAndBonesBrushesBeforeHit ;
  759. private KeyValuePair<Bone, BoneData> segmentHit;
  760.  
  761. //$$$$$$$$$$$$$$
  762.  
  763.  
  764. public Player(int skeletonSlot)
  765. {
  766. this.id = skeletonSlot;
  767.  
  768.  
  769.  
  770. }
  771.  
  772. public bool IsAlive { get; set; }
  773.  
  774. public DateTime LastUpdated { get; set; }
  775.  
  776. public Dictionary<Bone, BoneData> Segments
  777. {
  778. get
  779. {
  780. return this.segments;
  781. }
  782. }
  783.  
  784. public int GetId()
  785. {
  786. return this.id;
  787. }
  788.  
  789.  
  790. public void SetJointsAndBonesBrushesOfPlayers(int numOfPlayers) //$$
  791. {
  792.  
  793. if (numOfPlayers == 0) //$$
  794. {
  795.  
  796. this.jointsBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(10, 2, 18)); //$$סגול
  797. this.bonesBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(130, 20, 240)); //$$שחור
  798. this.LastUpdated = DateTime.Now;//$$
  799. }
  800. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  801. //במקרה של שני שחקנים צבע השלד יוגרל
  802.  
  803. else
  804. {
  805. // Generate one of 7 colors for player
  806. int[] mixR = { 1, 1, 1, 0, 1, 0, 0 };
  807. int[] mixG = { 1, 1, 0, 1, 0, 1, 0 };
  808. int[] mixB = { 1, 0, 1, 1, 0, 0, 1 };
  809. byte[] jointCols = { 245, 200 };
  810. byte[] boneCols = { 235, 160 };
  811.  
  812.  
  813. int i = colorId;
  814. colorId = (colorId + 1) % mixR.Count();
  815.  
  816. this.bonesBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(jointCols[mixR[i]], jointCols[mixG[i]], jointCols[mixB[i]]));
  817. this.jointsBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(boneCols[mixR[i]], boneCols[mixG[i]], boneCols[mixB[i]]));
  818. this.LastUpdated = DateTime.Now;
  819. }
  820. }
  821.  
  822. //@@$$$$$$$$$$$4
  823. //@@public void SetSegmentBrushOfCurrentHitThing(KeyValuePair<Bone, BoneData> seg)
  824. //@@{
  825. //@@segmentHit = seg;
  826.  
  827. //@@foreach (var segment in segments)
  828. //@@{
  829. //@@if ( (Segments.ContainsKey(seg.Key)) && (Segments.ContainsValue(seg.Value)) )
  830. //@@{
  831. //@@ System.Windows.Media.Brush jointsBrushBeforeHit = this.bonesBrush;
  832. //@@System.Windows.Media.Brush bonesBrushBeforeHit = this.bonesBrush;
  833. //@@jointsAndBonesBrushesBeforeHit = new KeyValuePair<Brush, Brush>(jointsBrushBeforeHit, jointsBrushBeforeHit);
  834.  
  835.  
  836. //@@BoneData boneDataChangedColorAfterHit = seg.Value;
  837. //@@boneDataChangedColorAfterHit.jointB = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 189, 0)); //צהוב
  838. //@@boneDataChangedColorAfterHit.boneB = new SolidColorBrush(System.Windows.Media.Color.FromRgb(13, 120, 61)); //$$ירוק
  839.  
  840.  
  841. //@@segmentHit = new KeyValuePair<Bone, BoneData>(seg.Key, boneDataChangedColorAfterHit);
  842. //@@var x = segments ;
  843. //@@ var y = segments[seg.Key];
  844. //@@var z = segmentHit;
  845. //@@var k = segmentHit.Value;
  846. //@@var t = seg;
  847. //@@var g = seg.Key;
  848. //@@var qqq = 5;
  849. //@@segments[seg.Key] = segmentHit.Value;
  850.  
  851.  
  852.  
  853. //@@}
  854.  
  855.  
  856. //@@}
  857. //@@ }
  858. //@@$$$$$$$$$$$$$
  859.  
  860.  
  861. public void SetBounds(Rect r)
  862. {
  863. this.playerBounds = r;
  864. this.playerCenter.X = (this.playerBounds.Left + this.playerBounds.Right) / 2;
  865. this.playerCenter.Y = (this.playerBounds.Top + this.playerBounds.Bottom) / 2;
  866. this.playerScale = Math.Min(this.playerBounds.Width, this.playerBounds.Height / 2);
  867. }
  868.  
  869. public void UpdateBonePosition(Microsoft.Kinect.JointCollection joints, JointType j1, JointType j2)
  870. {
  871. var seg = new Segment(
  872. (joints[j1].Position.X * this.playerScale) + this.playerCenter.X,
  873. this.playerCenter.Y - (joints[j1].Position.Y * this.playerScale),
  874. (joints[j2].Position.X * this.playerScale) + this.playerCenter.X,
  875. this.playerCenter.Y - (joints[j2].Position.Y * this.playerScale))
  876. { Radius = Math.Max(3.0, this.playerBounds.Height * BoneSize) / 2 };
  877. this.UpdateSegmentPosition(j1, j2, seg);
  878. }
  879.  
  880. public void UpdateJointPosition(Microsoft.Kinect.JointCollection joints, JointType j)
  881. {
  882. var seg = new Segment(
  883. (joints[j].Position.X * this.playerScale) + this.playerCenter.X,
  884. this.playerCenter.Y - (joints[j].Position.Y * this.playerScale))
  885. { Radius = this.playerBounds.Height * ((j == JointType.Head) ? HeadSize : HandSize) / 2 };
  886. this.UpdateSegmentPosition(j, j, seg);
  887. }
  888.  
  889. public void Draw(UIElementCollection children)
  890. {
  891.  
  892. if (!this.IsAlive)
  893. {
  894. return;
  895. }
  896.  
  897. // Draw all bones first, then circles (head and hands).
  898. DateTime cur = DateTime.Now;
  899. foreach (var segment in this.segments)
  900. {
  901.  
  902. Segment seg = segment.Value.GetEstimatedSegment(cur);
  903. if (!seg.IsCircle())
  904. {
  905. var line = new Line
  906. {
  907. StrokeThickness = seg.Radius * 2,
  908. X1 = seg.X1,
  909. Y1 = seg.Y1,
  910. X2 = seg.X2,
  911. Y2 = seg.Y2,
  912. Stroke = this.bonesBrush,
  913. StrokeEndLineCap = PenLineCap.Round,
  914. StrokeStartLineCap = PenLineCap.Round
  915. };
  916. children.Add(line);
  917. }
  918. }
  919.  
  920. foreach (var segment in this.segments)
  921. {
  922. Segment seg = segment.Value.GetEstimatedSegment(cur);
  923. if (seg.IsCircle())
  924. {
  925. var circle = new Ellipse { Width = seg.Radius * 2, Height = seg.Radius * 2 };
  926. circle.SetValue(Canvas.LeftProperty, seg.X1 - seg.Radius);
  927. circle.SetValue(Canvas.TopProperty, seg.Y1 - seg.Radius);
  928. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  929. //צבע המתאר של מקומות עגולים כמו יד שמסמן כמו בוקס (עגול) והראש יהיה בצבע של המפרקים
  930. circle.Stroke = this.jointsBrush;
  931. circle.StrokeThickness = 1;
  932. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  933. //צבע המתאר של מקומות עגולים כמו יד שמסמן כמו בוקס (עגול) והראש יהיה בצבע של: העצמות/המפרקים
  934. //circle.Fill = this.bonesBrush;//צבע העצמות
  935. circle.Fill = this.jointsBrush;//צבע המפרקים //$$
  936.  
  937. children.Add(circle);
  938. }
  939. }
  940.  
  941. // Remove unused players after 1/2 second.
  942. if (DateTime.Now.Subtract(this.LastUpdated).TotalMilliseconds > 500)
  943. {
  944. this.IsAlive = false;
  945. }
  946. }
  947.  
  948. private void UpdateSegmentPosition(JointType j1, JointType j2, Segment seg)
  949. {
  950. var bone = new Bone(j1, j2);
  951. if (this.segments.ContainsKey(bone))
  952. {
  953. BoneData data = this.segments[bone];
  954. data.UpdateSegment(seg);
  955. this.segments[bone] = data;
  956.  
  957. }
  958. else
  959. {
  960. this.segments.Add(bone, new BoneData(seg));
  961. }
  962. }
  963. }
  964. }
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981. falling thing
  982. ***************************************************************
  983.  
  984. //------------------------------------------------------------------------------
  985. // <copyright file="FallingThings.cs" company="Microsoft">
  986. // Copyright (c) Microsoft Corporation. All rights reserved.
  987. // </copyright>
  988. //------------------------------------------------------------------------------
  989.  
  990. // This module contains code to do display falling shapes, and do
  991. // hit testing against a set of segments provided by the Kinect NUI, and
  992. // have shapes react accordingly.
  993.  
  994. namespace ShapeGame
  995. {
  996. using System;
  997. using System.Collections.Generic;
  998. using System.Globalization;
  999. using System.Windows;
  1000. using System.Windows.Controls;
  1001. using System.Windows.Media;
  1002. using System.Windows.Shapes;
  1003. using Microsoft.Kinect;
  1004. using ShapeGame.Utils;
  1005.  
  1006. // FallingThings is the main class to draw and maintain positions of falling shapes. It also does hit testing
  1007. // and appropriate bouncing.
  1008. public class FallingThings
  1009. {
  1010. private const double BaseGravity = 0.017;
  1011. private const double BaseAirFriction = 0.994;
  1012.  
  1013. private readonly Dictionary<PolyType, PolyDef> polyDefs = new Dictionary<PolyType, PolyDef>
  1014. {
  1015. { PolyType.Triangle, new PolyDef { Sides = 3, Skip = 1 } },
  1016. { PolyType.Star, new PolyDef { Sides = 5, Skip = 2 } },
  1017. { PolyType.Pentagon, new PolyDef { Sides = 5, Skip = 1 } },
  1018. { PolyType.Square, new PolyDef { Sides = 4, Skip = 1 } },
  1019. { PolyType.Hex, new PolyDef { Sides = 6, Skip = 1 } },
  1020. { PolyType.Star7, new PolyDef { Sides = 7, Skip = 3 } },
  1021. { PolyType.Circle, new PolyDef { Sides = 1, Skip = 1 } },
  1022. { PolyType.Bubble, new PolyDef { Sides = 0, Skip = 1 } }
  1023. };
  1024.  
  1025. private readonly List<Thing> things = new List<Thing>();
  1026. private readonly Random rnd = new Random();
  1027. private readonly int maxThings;
  1028. private readonly int intraFrames = 1;
  1029. private readonly Dictionary<int, int> scores = new Dictionary<int, int>();
  1030. private const double DissolveTime = 0.4;
  1031. private Rect sceneRect;
  1032. private double targetFrameRate = 60;
  1033. private double dropRate = 2.0;
  1034. private double shapeSize = 1.0;
  1035. private double baseShapeSize = 20;
  1036. private GameMode gameMode = GameMode.Off;
  1037. private double gravity = BaseGravity;
  1038. private double gravityFactor = 1.0;
  1039. private double airFriction = BaseAirFriction;
  1040. private int frameCount;
  1041. private bool doRandomColors = true;
  1042. private double expandingRate = 1.0;
  1043. private System.Windows.Media.Color baseColor = System.Windows.Media.Color.FromRgb(0, 0, 0);
  1044. private PolyType polyTypes = PolyType.All;
  1045. private DateTime gameStartTime;
  1046. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1047. //@@לדעת מהי העצם שפגע ב"סינג"
  1048. //@@private KeyValuePair<Bone, BoneData> segmentOfCurrentHitThing;
  1049. //@@public KeyValuePair<Bone, BoneData> GetSegmentOfCurrentHitThing() { return segmentOfCurrentHitThing; }
  1050. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056. public FallingThings(int maxThings, double framerate, int intraFrames)
  1057. {
  1058. this.maxThings = maxThings;
  1059. this.intraFrames = intraFrames;
  1060. this.targetFrameRate = framerate * intraFrames;
  1061. this.SetGravity(this.gravityFactor);
  1062. this.sceneRect.X = this.sceneRect.Y = 0;
  1063. this.sceneRect.Width = this.sceneRect.Height = 100;
  1064. this.shapeSize = this.sceneRect.Height * this.baseShapeSize / 1000.0;
  1065. this.expandingRate = Math.Exp(Math.Log(6.0) / (this.targetFrameRate * DissolveTime));
  1066. }
  1067.  
  1068. public enum ThingState
  1069. {
  1070. Falling = 0, //$$-נופל
  1071. Bouncing = 1,//$$-מתפוצץ
  1072. Dissolving = 2,//$$-מעומעם
  1073. Remove = 3//$$-מוסר
  1074. }
  1075.  
  1076. public static Label MakeSimpleLabel(string text, Rect bounds, System.Windows.Media.Brush brush)
  1077. {
  1078. Label label = new Label { Content = text };
  1079. if (bounds.Width != 0)
  1080. {
  1081. label.SetValue(Canvas.LeftProperty, bounds.Left);
  1082. label.SetValue(Canvas.TopProperty, bounds.Top);
  1083. label.Width = bounds.Width;
  1084. label.Height = bounds.Height;
  1085. }
  1086.  
  1087. label.Foreground = brush;
  1088. label.FontFamily = new System.Windows.Media.FontFamily("Arial");
  1089. label.FontWeight = FontWeight.FromOpenTypeWeight(600);
  1090. label.FontStyle = FontStyles.Normal;
  1091. label.HorizontalAlignment = HorizontalAlignment.Center;
  1092. label.VerticalAlignment = VerticalAlignment.Center;
  1093. return label;
  1094. }
  1095.  
  1096. public void SetFramerate(double actualFramerate)
  1097. {
  1098. this.targetFrameRate = actualFramerate * this.intraFrames;
  1099. this.expandingRate = Math.Exp(Math.Log(6.0) / (this.targetFrameRate * DissolveTime));
  1100. if (this.gravityFactor != 0)
  1101. {
  1102. this.SetGravity(this.gravityFactor);
  1103. }
  1104. }
  1105.  
  1106. public void SetBoundaries(Rect r)
  1107. {
  1108. this.sceneRect = r;
  1109. this.shapeSize = r.Height * this.baseShapeSize / 1000.0;
  1110. }
  1111.  
  1112. public void SetDropRate(double f)
  1113. {
  1114. this.dropRate = f;
  1115. }
  1116.  
  1117. public void SetSize(double f)
  1118. {
  1119. this.baseShapeSize = f;
  1120. this.shapeSize = this.sceneRect.Height * this.baseShapeSize / 1000.0;
  1121. }
  1122.  
  1123. public void SetShapesColor(System.Windows.Media.Color color, bool doRandom)
  1124. {
  1125. this.doRandomColors = doRandom;
  1126. this.baseColor = color;
  1127. }
  1128.  
  1129. public void Reset()
  1130. {
  1131. for (int i = 0; i < this.things.Count; i++)
  1132. {
  1133. Thing thing = this.things[i];
  1134. if ((thing.State == ThingState.Bouncing) || (thing.State == ThingState.Falling))//bbb
  1135. {
  1136. thing.State = ThingState.Dissolving;//bbb
  1137. thing.Dissolve = 0;
  1138. this.things[i] = thing;
  1139. }
  1140. }
  1141.  
  1142. this.gameStartTime = DateTime.Now;
  1143. this.scores.Clear();
  1144. }
  1145.  
  1146. public void SetGameMode(GameMode mode)
  1147. {
  1148. this.gameMode = mode;
  1149. this.gameStartTime = DateTime.Now;
  1150. this.scores.Clear();
  1151. }
  1152.  
  1153. public void SetGravity(double f)
  1154. {
  1155. this.gravityFactor = f;
  1156. this.gravity = f * BaseGravity / this.targetFrameRate / Math.Sqrt(this.targetFrameRate) / Math.Sqrt(this.intraFrames);
  1157. this.airFriction = f == 0 ? 0.997 : Math.Exp(Math.Log(1.0 - ((1.0 - BaseAirFriction) / f)) / this.intraFrames);
  1158.  
  1159. if (f == 0)
  1160. {
  1161. // Stop all movement as well!
  1162. for (int i = 0; i < this.things.Count; i++)
  1163. {
  1164. Thing thing = this.things[i];
  1165. thing.XVelocity = thing.YVelocity = 0;
  1166. this.things[i] = thing;
  1167. }
  1168. }
  1169. }
  1170.  
  1171. public void SetPolies(PolyType polies)
  1172. {
  1173. this.polyTypes = polies;
  1174. }
  1175.  
  1176. public HitType LookForHits(Dictionary<Bone, BoneData> segments, int playerId)
  1177. {
  1178. DateTime cur = DateTime.Now;
  1179.  
  1180. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//?
  1181. //מאתחלים את ערך החזרת הפונקציה(סוג הפגיעה-פןפ,שום פגיעה,סקוויז וכו)בשום פגיעה
  1182. HitType allHits = HitType.None; //bbb
  1183.  
  1184. // Zero out score if necessary
  1185. if (!this.scores.ContainsKey(playerId))
  1186. {
  1187. this.scores.Add(playerId, 0);
  1188. }
  1189.  
  1190. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//?
  1191. //עוברים במקביל על כל מקטעי השלד ועל הצורות ובודקים אם היתה פגיעה
  1192. foreach (var pair in segments)
  1193. {
  1194. for (int i = 0; i < this.things.Count; i++)
  1195. {
  1196.  
  1197. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1198. //מאתחל משתנה שישמור את סוג הפגיעה לשום פגיעה
  1199.  
  1200. HitType hit = HitType.None; //bbb
  1201. Thing thing = this.things[i];
  1202. switch (thing.State)
  1203. {
  1204.  
  1205. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//?
  1206. //מצבים בהם ישנה אפשרות לפגוע
  1207. case ThingState.Bouncing://bbb
  1208. case ThingState.Falling://bbb
  1209. {
  1210. var hitCenter = new System.Windows.Point(0, 0); //bbb
  1211. double lineHitLocation = 0; //bbb
  1212. Segment seg = pair.Value.GetEstimatedSegment(cur);
  1213.  
  1214. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1215. //אם הפונקציה "איט" החזריה "טרו" סימן שהיתה פגיעה
  1216.  
  1217. if (thing.Hit(seg, ref hitCenter, ref lineHitLocation)) //bbb
  1218. {
  1219. //$$$$$$$$$$$$$$$$$$$$$$
  1220. //@@בשביל לצבוע את הסגמנט שפגע ב"סינג
  1221. //@@segmentOfCurrentHitThing = pair;//$$
  1222.  
  1223.  
  1224. double fMs = 1000;
  1225.  
  1226. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1227. //כאשר זמן פגיעה בצורה שונה מהערך שאתחלו אותה
  1228. if (thing.TimeLastHit != DateTime.MinValue)//bbb
  1229. {
  1230.  
  1231. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1232. //fMs-מחשבים את מספר השניות שעברו מזמן שהצורה אותחלה והוצגה בעצם על המסך עד לזמן שנפגעה
  1233. //ומחשבים את ממוצע משך זמן הפגיעה
  1234. //cur-זהו הזמן האמיתי של הפגיעה
  1235. fMs = cur.Subtract(thing.TimeLastHit).TotalMilliseconds; //bbb
  1236. thing.AvgTimeBetweenHits = (thing.AvgTimeBetweenHits * 0.8) + (0.2 * fMs); //bbb
  1237. }
  1238. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1239. //לשם בדיקה-מתי הזמן של הסינג נשאר אותו דבר
  1240. else if(thing.TimeLastHit != DateTime.MinValue)
  1241. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1242. {
  1243. ;
  1244. }
  1245.  
  1246. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1247. //נעדכן את "סינג" בזמן האמיתי של הפגיעה
  1248. thing.TimeLastHit = cur; //bbb
  1249.  
  1250. // Bounce off head and hands
  1251.  
  1252. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1253. //פגיעה בעצמות ראש/ידיים/רגלים
  1254. if (seg.IsCircle())
  1255. {
  1256. // Bounce off of hand/head/foot
  1257. thing.BounceOff(
  1258. hitCenter.X,
  1259. hitCenter.Y,
  1260. seg.Radius,
  1261. pair.Value.XVelocity / this.targetFrameRate,
  1262. pair.Value.YVelocity / this.targetFrameRate);
  1263.  
  1264. if (fMs > 100.0)
  1265. {
  1266. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1267. //במצב שהיתה פגיעה חלשה--נגיעה קלה עם היד
  1268. hit |= HitType.Hand; //bbb
  1269. }
  1270. }
  1271. else
  1272. {
  1273. // Bounce off line segment
  1274. double velocityX = (pair.Value.XVelocity * (1.0 - lineHitLocation)) + (pair.Value.XVelocity2 * lineHitLocation);
  1275. double velocityY = (pair.Value.YVelocity * (1.0 - lineHitLocation)) + (pair.Value.YVelocity2 * lineHitLocation);
  1276.  
  1277. thing.BounceOff(
  1278. hitCenter.X,
  1279. hitCenter.Y,
  1280. seg.Radius,
  1281. velocityX / this.targetFrameRate,
  1282. velocityY / this.targetFrameRate);
  1283.  
  1284. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1285. //במצב של פגיעה בזרוע
  1286. if (fMs > 100.0)
  1287. {
  1288. hit |= HitType.Arm; //bbb
  1289. }
  1290. }
  1291.  
  1292. if (this.gameMode == GameMode.TwoPlayer)
  1293. {
  1294. if (thing.State == ThingState.Falling)//bbb
  1295. {
  1296. thing.State = ThingState.Bouncing;//bbb
  1297. thing.TouchedBy = playerId;
  1298. thing.Hotness = 1;
  1299. thing.FlashCount = 0;
  1300. }
  1301. else if (thing.State == ThingState.Bouncing)//bbb
  1302. {
  1303. if (thing.TouchedBy != playerId)
  1304. {
  1305. if (seg.IsCircle())
  1306. {
  1307. thing.TouchedBy = playerId;
  1308. thing.Hotness = Math.Min(thing.Hotness + 1, 4);
  1309. }
  1310. else
  1311. {
  1312. hit |= HitType.Popped; //bbb
  1313. this.AddToScore(thing.TouchedBy, 5 << (thing.Hotness - 1), thing.Center);
  1314. }
  1315. }
  1316. }
  1317. }
  1318. else if (this.gameMode == GameMode.Solo)
  1319. {
  1320. if (seg.IsCircle())
  1321. {
  1322.  
  1323. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1324. //היתה פגיעה והצורה היתה במצב נפילה(מצב בו היא מאותחלת ברגע שנכנסת למערך ה"סינגים" ןמןצגת על המסך) תעדכן אותה להיות במצב מתפוצץ
  1325. if (thing.State == ThingState.Falling)//bbb
  1326. {
  1327. thing.State = ThingState.Bouncing;//bbb
  1328. thing.TouchedBy = playerId;
  1329. thing.Hotness = 1;
  1330. thing.FlashCount = 0;
  1331. }
  1332. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1333. //כאשר ה"סינג" כבר במצב מעומעם-נגדיר את סוג הפגיעה להיות "פופ" ונרצה להציג ניקוד על הפגיעה(במקום הצורה שנפגעה) ולעדכן ניקוד כללי
  1334. else if ((thing.State == ThingState.Bouncing) && (fMs > 100.0))//bbb
  1335. {
  1336. hit |= HitType.Popped; //bbb
  1337. int points = (pair.Key.Joint1 == JointType.FootLeft
  1338. || pair.Key.Joint1 == JointType.FootRight)
  1339. ? 10
  1340. : 5;
  1341. this.AddToScore(
  1342. thing.TouchedBy,
  1343. points,
  1344. thing.Center);
  1345. thing.TouchedBy = playerId;
  1346. }
  1347. }
  1348. }
  1349.  
  1350. this.things[i] = thing;
  1351. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1352. //אם ממוצע משך זמן הפגיעה קטן מ8 זה אומר שהפגיעה היתה מסוג "פופ" או "סקייז
  1353. if (thing.AvgTimeBetweenHits < 8) //bbb
  1354. {
  1355. hit |= HitType.Popped | HitType.Squeezed; //bbb
  1356. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1357. //כאשר ה"סינג" כבר במצב מעומעם נרצה להציג ניקוד על הפגיעה(במקום הצורה שהתפוצצה) ולעדכן ניקוד כללי אך זאת בתנאי שמצב המשחק אינו כבוי כלומר יש 1/2 שחקנים במשחק
  1358. if (this.gameMode != GameMode.Off)
  1359. {
  1360. this.AddToScore(playerId, 1, thing.Center);
  1361. }
  1362. }
  1363. }
  1364. }
  1365.  
  1366. break;
  1367. }
  1368. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1369. //היתה פגיעה והיא והיא מסוג "פופ" תעדכןאת מצב ה"סינג" להיות מעומעם ותעדכן כמה דברים שצריך בכמה דברים שדרושים כדי להיכנס למצב הזה
  1370. if ((hit & HitType.Popped) != 0) //bbb
  1371. {
  1372. thing.State = ThingState.Dissolving;//bbb
  1373. thing.Dissolve = 0;
  1374. thing.XVelocity = thing.YVelocity = 0;
  1375. thing.SpinRate = (thing.SpinRate * 6) + 0.2;
  1376. this.things[i] = thing;
  1377. }
  1378.  
  1379. allHits |= hit; //bbb
  1380. }
  1381. }
  1382.  
  1383. return allHits; //bbb
  1384. }
  1385.  
  1386. public void AdvanceFrame()
  1387. {
  1388. // Move all things by one step, accounting for gravity
  1389. for (int thingIndex = 0; thingIndex < this.things.Count; thingIndex++)
  1390. {
  1391. Thing thing = this.things[thingIndex];
  1392. thing.Center.Offset(thing.XVelocity, thing.YVelocity);
  1393. thing.YVelocity += this.gravity * this.sceneRect.Height;
  1394. thing.YVelocity *= this.airFriction;
  1395. thing.XVelocity *= this.airFriction;
  1396. thing.Theta += thing.SpinRate;
  1397.  
  1398. // bounce off walls
  1399. if ((thing.Center.X - thing.Size < 0) || (thing.Center.X + thing.Size > this.sceneRect.Width))
  1400. {
  1401. thing.XVelocity = -thing.XVelocity;
  1402. thing.Center.X += thing.XVelocity;
  1403. }
  1404.  
  1405. // Then get rid of one if any that fall off the bottom
  1406. if (thing.Center.Y - thing.Size > this.sceneRect.Bottom)
  1407. {
  1408. thing.State = ThingState.Remove;//bbb
  1409. }
  1410.  
  1411. // Get rid of after dissolving.
  1412.  
  1413.  
  1414. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1415. // במצב שה"סינג" במצב מעומעם תעמעם את ה"סינג" שלו כאשר המשתנה "דיסולב" שהפך בין מצב התפוצצות למצב עמעום ל0
  1416. //עולה לאט לאט וכשהוא גדול שווה ל1 מצב ה"סינג" הופך להיות במצב מוסר
  1417. if (thing.State == ThingState.Dissolving)//bbb
  1418. {
  1419. thing.Dissolve += 1 / (this.targetFrameRate * DissolveTime);
  1420. thing.Size *= this.expandingRate;
  1421. if (thing.Dissolve >= 1.0)
  1422. {
  1423. thing.State = ThingState.Remove;//bbb
  1424. }
  1425. }
  1426.  
  1427. this.things[thingIndex] = thing;
  1428. }
  1429.  
  1430. // Then remove any that should go away now
  1431.  
  1432. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1433. //לאחר שעבר על מערך ה"סינגים" בודק אם יש "סינגים" שצריך להסיר
  1434. for (int i = 0; i < this.things.Count; i++)
  1435. {
  1436. Thing thing = this.things[i];
  1437. if (thing.State == ThingState.Remove)//bbb
  1438. {
  1439. this.things.Remove(thing);
  1440. i--;
  1441. }
  1442. }
  1443.  
  1444. // Create any new things to drop based on dropRate
  1445. if ((this.things.Count < this.maxThings) && (this.rnd.NextDouble() < this.dropRate / this.targetFrameRate) && (this.polyTypes != PolyType.None))
  1446. {
  1447. PolyType[] alltypes =
  1448. {
  1449. PolyType.Triangle, PolyType.Square, PolyType.Star, PolyType.Pentagon,
  1450. PolyType.Hex, PolyType.Star7, PolyType.Circle, PolyType.Bubble
  1451. };
  1452. byte r;
  1453. byte g;
  1454. byte b;
  1455.  
  1456. if (this.doRandomColors)
  1457. {
  1458. r = (byte)(this.rnd.Next(215) + 40);
  1459. g = (byte)(this.rnd.Next(215) + 40);
  1460. b = (byte)(this.rnd.Next(215) + 40);
  1461. }
  1462. else
  1463. {
  1464. r = (byte)Math.Min(255.0, this.baseColor.R * (0.7 + (this.rnd.NextDouble() * 0.7)));
  1465. g = (byte)Math.Min(255.0, this.baseColor.G * (0.7 + (this.rnd.NextDouble() * 0.7)));
  1466. b = (byte)Math.Min(255.0, this.baseColor.B * (0.7 + (this.rnd.NextDouble() * 0.7)));
  1467. }
  1468.  
  1469. PolyType tryType;
  1470. do
  1471. {
  1472. tryType = alltypes[this.rnd.Next(alltypes.Length)];
  1473. }
  1474. while ((this.polyTypes & tryType) == 0);
  1475.  
  1476. this.DropNewThing(tryType, this.shapeSize, System.Windows.Media.Color.FromRgb(r, g, b));
  1477. }
  1478. }
  1479.  
  1480. public void DrawFrame(UIElementCollection children)
  1481. {
  1482. this.frameCount++;
  1483.  
  1484. // Draw all shapes in the scene
  1485. for (int i = 0; i < this.things.Count; i++)
  1486. {
  1487. Thing thing = this.things[i];
  1488. if (thing.Brush == null)
  1489. {
  1490. thing.Brush = new SolidColorBrush(thing.Color);
  1491. double factor = 0.4 + (((double)thing.Color.R + thing.Color.G + thing.Color.B) / 1600);
  1492. thing.Brush2 =
  1493. new SolidColorBrush(
  1494. System.Windows.Media.Color.FromRgb(
  1495. (byte)(255 - ((255 - thing.Color.R) * factor)),
  1496. (byte)(255 - ((255 - thing.Color.G) * factor)),
  1497. (byte)(255 - ((255 - thing.Color.B) * factor))));
  1498. thing.BrushPulse = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 255, 255));
  1499. }
  1500.  
  1501.  
  1502. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1503. //כאשר היתה פגיעה והצורה היתה במצב מתפוצץ
  1504. if (thing.State == ThingState.Bouncing)//bbb
  1505. {
  1506. // Pulsate edges
  1507. double alpha = Math.Cos((0.15 * (thing.FlashCount++) * thing.Hotness) * 0.5) + 0.5;
  1508.  
  1509. children.Add(
  1510. this.MakeSimpleShape(
  1511. this.polyDefs[thing.Shape].Sides,
  1512. this.polyDefs[thing.Shape].Skip,
  1513. thing.Size,
  1514. thing.Theta,
  1515. thing.Center,
  1516. thing.Brush,
  1517. thing.BrushPulse,
  1518. thing.Size * 0.1,
  1519. alpha));
  1520. this.things[i] = thing;
  1521. }
  1522. else
  1523. {
  1524. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//??
  1525. //במצב מעומעם נרצה לשנות את אטימות הצבע של ה"סינג
  1526. if (thing.State == ThingState.Dissolving)//bbb
  1527. {
  1528. thing.Brush.Opacity = 1.0 - (thing.Dissolve * thing.Dissolve);
  1529. }
  1530.  
  1531. children.Add(
  1532. this.MakeSimpleShape(
  1533. this.polyDefs[thing.Shape].Sides,
  1534. this.polyDefs[thing.Shape].Skip,
  1535. thing.Size,
  1536. thing.Theta,
  1537. thing.Center,
  1538. thing.Brush,
  1539. (thing.State == ThingState.Dissolving) ? null : thing.Brush2,
  1540. 1,
  1541. 1));
  1542. }
  1543. }
  1544.  
  1545. // Show scores
  1546. if (this.scores.Count != 0)
  1547. {
  1548. int i = 0;
  1549. foreach (var score in this.scores)
  1550. {
  1551. Label label = MakeSimpleLabel(
  1552. score.Value.ToString(CultureInfo.InvariantCulture),
  1553. new Rect(
  1554. (0.02 + (i * 0.6)) * this.sceneRect.Width,
  1555. 0.01 * this.sceneRect.Height,
  1556. 0.4 * this.sceneRect.Width,
  1557. 0.3 * this.sceneRect.Height),
  1558. new SolidColorBrush(System.Windows.Media.Color.FromArgb(200, 255, 255, 255)));
  1559. label.FontSize = Math.Max(1, Math.Min(this.sceneRect.Width / 12, this.sceneRect.Height / 12));
  1560. children.Add(label);
  1561. i++;
  1562. }
  1563. }
  1564.  
  1565. // Show game timer
  1566. if (this.gameMode != GameMode.Off)
  1567. {
  1568. TimeSpan span = DateTime.Now.Subtract(this.gameStartTime);
  1569. string text = span.Minutes.ToString(CultureInfo.InvariantCulture) + ":" + span.Seconds.ToString("00");
  1570.  
  1571. Label timeText = MakeSimpleLabel(
  1572. text,
  1573. new Rect(
  1574. 0.1 * this.sceneRect.Width, 0.25 * this.sceneRect.Height, 0.89 * this.sceneRect.Width, 0.72 * this.sceneRect.Height),
  1575. new SolidColorBrush(System.Windows.Media.Color.FromArgb(160, 255, 255, 255)));
  1576. timeText.FontSize = Math.Max(1, this.sceneRect.Height / 16);
  1577. timeText.HorizontalContentAlignment = HorizontalAlignment.Right;
  1578. timeText.VerticalContentAlignment = VerticalAlignment.Bottom;
  1579. children.Add(timeText);
  1580. }
  1581. }
  1582.  
  1583. private static double SquaredDistance(double x1, double y1, double x2, double y2)
  1584. {
  1585. return ((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1));
  1586. }
  1587.  
  1588. private void AddToScore(int player, int points, System.Windows.Point center)
  1589. {
  1590. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1591. //ניקוד על פגיעה ספציפית
  1592. if (this.scores.ContainsKey(player))
  1593. {
  1594. this.scores[player] = this.scores[player] + points;
  1595. }
  1596. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1597. //ניקוד כללי
  1598. else
  1599. {
  1600. this.scores.Add(player, points);
  1601. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1602. //לשם בדיקה-מתי מציג פעמיים ניקוד באותו מסך
  1603. if(scores.Count>1)
  1604. {
  1605. ;
  1606. }
  1607. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1608.  
  1609. }
  1610.  
  1611. FlyingText.NewFlyingText(this.sceneRect.Width / 300, center, "+" + points);
  1612. }
  1613.  
  1614. private void DropNewThing(PolyType newShape, double newSize, System.Windows.Media.Color newColor)
  1615. {
  1616. // Only drop within the center "square" area
  1617. double dropWidth = this.sceneRect.Bottom - this.sceneRect.Top;
  1618. if (dropWidth > this.sceneRect.Right - this.sceneRect.Left)
  1619. {
  1620. dropWidth = this.sceneRect.Right - this.sceneRect.Left;
  1621. }
  1622.  
  1623. var newThing = new Thing
  1624. {
  1625. Size = newSize,
  1626. YVelocity = ((0.5 * this.rnd.NextDouble()) - 0.25) / this.targetFrameRate,
  1627. XVelocity = 0,
  1628. Shape = newShape,
  1629. Center = new System.Windows.Point((this.rnd.NextDouble() * dropWidth) + ((this.sceneRect.Left + this.sceneRect.Right - dropWidth) / 2), this.sceneRect.Top - newSize),
  1630. SpinRate = ((this.rnd.NextDouble() * 12.0) - 6.0) * 2.0 * Math.PI / this.targetFrameRate / 4.0,
  1631. Theta = 0,
  1632. TimeLastHit = DateTime.MinValue,
  1633. AvgTimeBetweenHits = 100,
  1634. Color = newColor,
  1635. Brush = null,
  1636. Brush2 = null,
  1637. BrushPulse = null,
  1638. Dissolve = 0,
  1639. State = ThingState.Falling,
  1640. TouchedBy = 0,
  1641. Hotness = 0,
  1642. FlashCount = 0
  1643. };
  1644.  
  1645. this.things.Add(newThing);
  1646. }
  1647.  
  1648. private Shape MakeSimpleShape(
  1649. int numSides,
  1650. int skip,
  1651. double size,
  1652. double spin,
  1653. System.Windows.Point center,
  1654. System.Windows.Media.Brush brush,
  1655. System.Windows.Media.Brush brushStroke,
  1656. double strokeThickness,
  1657. double opacity)
  1658. {
  1659. if (numSides <= 1)
  1660. {
  1661. var circle = new Ellipse { Width = size * 2, Height = size * 2, Stroke = brushStroke };
  1662. if (circle.Stroke != null)
  1663. {
  1664. circle.Stroke.Opacity = opacity;
  1665. }
  1666.  
  1667. circle.StrokeThickness = strokeThickness * ((numSides == 1) ? 1 : 2);
  1668. circle.Fill = (numSides == 1) ? brush : null;
  1669. circle.SetValue(Canvas.LeftProperty, center.X - size);
  1670. circle.SetValue(Canvas.TopProperty, center.Y - size);
  1671. return circle;
  1672. }
  1673.  
  1674. var points = new PointCollection(numSides + 2);
  1675. double theta = spin;
  1676. for (int i = 0; i <= numSides + 1; ++i)
  1677. {
  1678. points.Add(new System.Windows.Point((Math.Cos(theta) * size) + center.X, (Math.Sin(theta) * size) + center.Y));
  1679. theta = theta + (2.0 * Math.PI * skip / numSides);
  1680. }
  1681.  
  1682. var polyline = new Polyline { Points = points, Stroke = brushStroke };
  1683. if (polyline.Stroke != null)
  1684. {
  1685. polyline.Stroke.Opacity = opacity;
  1686. }
  1687.  
  1688. polyline.Fill = brush;
  1689. polyline.FillRule = FillRule.Nonzero;
  1690. polyline.StrokeThickness = strokeThickness;
  1691. return polyline;
  1692. }
  1693.  
  1694. internal struct PolyDef
  1695. {
  1696. public int Sides;
  1697. public int Skip;
  1698. }
  1699.  
  1700. // The Thing struct represents a single object that is flying through the air, and
  1701. // all of its properties.
  1702. private struct Thing
  1703. {
  1704. public System.Windows.Point Center;
  1705. public double Size;
  1706. public double Theta;
  1707. public double SpinRate;
  1708. public double YVelocity;
  1709. public double XVelocity;
  1710. public PolyType Shape;
  1711. public System.Windows.Media.Color Color;
  1712. public System.Windows.Media.Brush Brush;
  1713. public System.Windows.Media.Brush Brush2;
  1714. public System.Windows.Media.Brush BrushPulse;
  1715. public double Dissolve;
  1716. public ThingState State;
  1717. public DateTime TimeLastHit;
  1718. public double AvgTimeBetweenHits;
  1719. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1720. //השחקן שנגע בצורה
  1721. public int TouchedBy; // Last player to touch this thing
  1722.  
  1723. public int Hotness; // Score level
  1724. public int FlashCount;
  1725.  
  1726. // Hit testing between this thing and a single segment. If hit, the center point on
  1727. // the segment being hit is returned, along with the spot on the line from 0 to 1 if
  1728. // a line segment was hit.
  1729.  
  1730. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
  1731. //מחזירה טרו אם היתה פגיעה בין "סינג" ואחת מהסגמטים=מקטעי השלד
  1732. public bool Hit(Segment seg, ref System.Windows.Point hitCenter, ref double lineHitLocation)
  1733. {
  1734. double minDxSquared = this.Size + seg.Radius;
  1735. minDxSquared *= minDxSquared;
  1736.  
  1737. // See if falling thing hit this body segment
  1738. if (seg.IsCircle())
  1739. {
  1740. if (SquaredDistance(this.Center.X, this.Center.Y, seg.X1, seg.Y1) <= minDxSquared)
  1741. {
  1742. hitCenter.X = seg.X1; //bbb
  1743. hitCenter.Y = seg.Y1;//bbb
  1744. lineHitLocation = 0;//bbb
  1745. return true;
  1746. }
  1747. }
  1748. else
  1749. {
  1750. double sqrLineSize = SquaredDistance(seg.X1, seg.Y1, seg.X2, seg.Y2);
  1751. if (sqrLineSize < 0.5)
  1752. {
  1753. // if less than 1/2 pixel apart, just check dx to an endpoint
  1754. return SquaredDistance(this.Center.X, this.Center.Y, seg.X1, seg.Y1) < minDxSquared;
  1755. }
  1756.  
  1757. // Find dx from center to line
  1758. double u = ((this.Center.X - seg.X1) * (seg.X2 - seg.X1)) + (((this.Center.Y - seg.Y1) * (seg.Y2 - seg.Y1)) / sqrLineSize);
  1759. if ((u >= 0) && (u <= 1.0))
  1760. { // Tangent within line endpoints, see if we're close enough
  1761. double intersectX = seg.X1 + ((seg.X2 - seg.X1) * u);
  1762. double intersectY = seg.Y1 + ((seg.Y2 - seg.Y1) * u);
  1763.  
  1764. if (SquaredDistance(this.Center.X, this.Center.Y, intersectX, intersectY) < minDxSquared)
  1765. {
  1766. lineHitLocation = u;//bbb
  1767. hitCenter.X = intersectX;//bbb
  1768. hitCenter.Y = intersectY;//bbb
  1769. return true;
  1770. }
  1771. }
  1772. else
  1773. {
  1774. // See how close we are to an endpoint
  1775. if (u < 0)
  1776. {
  1777. if (SquaredDistance(this.Center.X, this.Center.Y, seg.X1, seg.Y1) < minDxSquared)
  1778. {
  1779. lineHitLocation = 0;//bbb
  1780. hitCenter.X = seg.X1;//bbb
  1781. hitCenter.Y = seg.Y1;//bbb
  1782. return true;
  1783. }
  1784. }
  1785. else
  1786. {
  1787. if (SquaredDistance(this.Center.X, this.Center.Y, seg.X2, seg.Y2) < minDxSquared)
  1788. {
  1789. lineHitLocation = 1;//bbb
  1790. hitCenter.X = seg.X2;//bbb
  1791. hitCenter.Y = seg.Y2;//bbb
  1792. return true;
  1793. }
  1794. }
  1795. }
  1796.  
  1797. return false;
  1798. }
  1799.  
  1800. return false;
  1801. }
  1802.  
  1803. // Change our velocity based on the object's velocity, our velocity, and where we hit.
  1804. public void BounceOff(double x1, double y1, double otherSize, double fXv, double fYv)
  1805. {
  1806. double x0 = this.Center.X;
  1807. double y0 = this.Center.Y;
  1808. double xv0 = this.XVelocity - fXv;
  1809. double yv0 = this.YVelocity - fYv;
  1810. double dist = otherSize + this.Size;
  1811. double dx = Math.Sqrt(((x1 - x0) * (x1 - x0)) + ((y1 - y0) * (y1 - y0)));
  1812. double xdif = x1 - x0;
  1813. double ydif = y1 - y0;
  1814. double newvx1 = 0;
  1815. double newvy1 = 0;
  1816.  
  1817. x0 = x1 - (xdif / dx * dist);
  1818. y0 = y1 - (ydif / dx * dist);
  1819. xdif = x1 - x0;
  1820. ydif = y1 - y0;
  1821.  
  1822. double bsq = dist * dist;
  1823. double b = dist;
  1824. double asq = (xv0 * xv0) + (yv0 * yv0);
  1825. double a = Math.Sqrt(asq);
  1826. if (a > 0.000001)
  1827. {
  1828. // if moving much at all...
  1829. double cx = x0 + xv0;
  1830. double cy = y0 + yv0;
  1831. double csq = ((x1 - cx) * (x1 - cx)) + ((y1 - cy) * (y1 - cy));
  1832. double tt = asq + bsq - csq;
  1833. double bb = 2 * a * b;
  1834. double power = a * (tt / bb);
  1835. newvx1 -= 2 * (xdif / dist * power);
  1836. newvy1 -= 2 * (ydif / dist * power);
  1837. }
  1838.  
  1839. this.XVelocity += newvx1;
  1840. this.YVelocity += newvy1;
  1841. this.Center.X = x0;
  1842. this.Center.Y = y0;
  1843. }
  1844. }
  1845. }
  1846. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement