Interaction logic for SightsControl.xaml
Inheritance: System.Windows.Controls.UserControl
示例#1
0
        /// <summary>
        /// Handles the end of a shot animation.
        /// </summary>
        /// <param name="p">Player who took the shot</param>
        /// <param name="x">X coordinate of the shot</param>
        /// <param name="y">Y coordinate of the shot</param>
        /// <param name="s">SightsControl being fired</param>
        public void ShotEnded(Player p, float x, float y, SightsControl s)
        {
            // Remove the control.
            MainCanvas.Children.Remove(s);

            // Locking here prevents concurrence from the network listener.
            lock (this.players)
            {
                if (p.PlayerActive && hitSkeleton.DamageTaken < 1.0)
                {
                    float pX = x * (float)this.ActualWidth;
                    float pY = y * (float)this.ActualHeight;

                    // Build bullet polygon.
                    Polygon pol = new Polygon();
                    pol.Points = hitSkeleton.RegularPolygonToPointCollection(new Vector2(pX, pY), (float)p.Sights.ActualWidth * 0.5f, 15);

                    // Check for weapon hits.
                    if (Tools.IsPolygonColliding(pol, hitSkeleton.PaddleRight.Shape))
                    {
                        hitSkeleton.PaddleRight.State = PaddleState.Hit;
                        SetFlyout("DEFEND!", pX, pY, Brushes.MidnightBlue);
                        p.Status = ShotStatus.Defend;
                    }
                    else if (Tools.IsPolygonColliding(pol, hitSkeleton.PaddleLeft.Shape))
                    {
                        hitSkeleton.PaddleLeft.State = PaddleState.Hit;
                        SetFlyout("DEFEND!", pX, pY, Brushes.MidnightBlue);
                        p.Status = ShotStatus.Defend;
                    }
                    // Possible body part hits.
                    else
                    {
                        var hitParts = hitSkeleton.BodyParts.Where(pa =>
                            pa.Key != HitSkeletonBodyPart.ForearmRight &&
                            pa.Key != HitSkeletonBodyPart.ForearmLeft &&
                            Tools.IsPolygonColliding(pa.Value.Shape, pol));
                        BodyPart part = hitParts.FirstOrDefault(pa => pa.Value.State == BodyPartState.NotHit).Value;

                        // Only one part may be hit per shot, but precedence is
                        // always given to parts that haven't been shot before.
                        if (part != null)
                        {
                            part.State = BodyPartState.Hit;
                            SetFlyout("HIT!", pX, pY, Brushes.MidnightBlue);
                            p.Status = ShotStatus.Hit;
                        }
                        // A part previously hit was hit again.
                        else if (hitParts.Count() > 0)
                        {
                            SetFlyout("HIT!", pX, pY, Brushes.MidnightBlue);
                            p.Status = ShotStatus.Hit;
                        }
                        // Nothing was hit.
                        else
                        {
                            SetFlyout("MISSED!", pX, pY, Brushes.MidnightBlue);
                            p.Status = ShotStatus.Miss;
                        }
                    }

                    // Build response to server.
                    StringBuilder response = new StringBuilder("");
                    foreach (Player pl in this.players)
                    {
                        // Current shot player.
                        if (p == pl)
                        {
                            switch (pl.Status)
                            {
                                case ShotStatus.Defend:
                                    response.Append("3,");
                                    break;
                                case ShotStatus.Hit:
                                    response.Append("2,");
                                    break;

                                case ShotStatus.Miss:
                                    response.Append("1,");
                                    break;

                                case ShotStatus.None:
                                    response.Append("0,");
                                    break;
                            }
                            pl.Status = ShotStatus.None;
                        }
                        // Other players.
                        else
                        {
                            // If player is active.
                            if (pl.PlayerActive)
                            {
                                response.Append("0,");
                            }
                            // If player is not active.
                            else
                            {
                                response.Append("-1,");
                            }
                        }
                    }

                    // Append player health to response and send it.
                    float damage = hitSkeleton.DamageTaken;
                    response.Append(damage);
                    ws.Send(response.ToString());
                }
            }
        }
示例#2
0
 /// <summary>
 /// Fire a shot from a player.
 /// </summary>
 /// <param name="index">Index of the player who took the shot</param>
 public void FirePlayer(int index)
 {
     Player p = this.players[index];
     if (p.PlayerActive)
     {
         // Resize all sights.
         float size = (float)this.ActualWidth * 0.05f;
         float sightsWidth = size * 0.1f;
         float sightsClose = size * 0.5f;
         float sightsInnerWidth = size * 0.2f;
         SightsControl s = new SightsControl
         {
             SightsColor = p.Sights.SightsColor,
             Width = size,
             Height = size,
             SightsWidth = 0.0f,
             SightsClose = sightsClose,
             SightsInnerWidth = 0.0f
         };
         MainCanvas.Children.Add(s);
         Canvas.SetLeft(s, p.X - size / 2.0f);
         Canvas.SetTop(s, p.Y - size / 2.0f);
         float x = p.X / (float)this.ActualWidth;
         float y = p.Y / (float)this.ActualHeight;
         s.FireAnimationStoryBoard.Completed += (snd, arg) => ShotEnded(p, x, y, s);
         s.Fire();
     }
 }