//void OnPlayerDrowned(DisplayObjectContainer sender)
 //{
 //    sender.PlayheadWrap -= new AnimationEvent(OnPlayerDrowned);
 //    sender.Stop();
 //    sender.DestroyAfterUpdate();
 //}
 private void KillPlayer(SmuckPlayer p)
 {
     p.LivingState = LivingState.Dying;
     if (this is SteamRollerScreen || p.isExploding)
     {
         p.body.SetLinearVelocity(Vector2.Zero);
     }
     //if (p.IsNaked)
     //{
     //    p.LivingState = LivingState.Dead;
     //}
     //else
     //{
     //    p.LivingState = LivingState.Dying;
     //}
     //    if (p.LivingState == LivingState.Alive)
     //    {
     //        if (p.DefinitionName.StartsWith("swim"))
     //        {
     //            p.ReplaceView("drowningPlayer");
     //            p.PlayAll();
     //            p.PlayheadWrap += new AnimationEvent(OnPlayerDrowned);
     //        }
     //        else
     //        {
     //            p.ReplaceView("deadPlayer0");
     //        }
     //        p.LivingState = LivingState.Dying;
     //    }
 }
        public override void EndContact(Contact contact)
        {
            V2DSprite objA = (V2DSprite)contact.GetFixtureA().GetBody().GetUserData();
            V2DSprite objB = (V2DSprite)contact.GetFixtureB().GetBody().GetUserData();

            SmuckPlayer p = objA is SmuckPlayer ? (SmuckPlayer)objA : objB is SmuckPlayer ? (SmuckPlayer)objB : null;
            LaneVehicle v = objA is LaneVehicle ? (LaneVehicle)objA : objB is LaneVehicle ? (LaneVehicle)objB : null;
            V2DSprite   w = objA.InstanceName.StartsWith("water") ? objA : objB.InstanceName.StartsWith("water") ? objB : null;

            if (p != null)
            {
                if (w != null)
                {
                    p.IsOnWater = false;
                }
                else if (v != null)
                {
                    if (v.Lane.LaneKind == LaneKind.DrownWater)
                    {
                        p.aboardVehicle = null;
                        if (p.IsOnWater)
                        {
                            p.LivingState = LivingState.Dying;
                            stage.audio.PlaySound(Sfx.bigSplash);
                        }
                    }
                    else
                    {
                        this.KillPlayer(p);
                    }
                }
            }
        }
        protected override SmuckPlayer CreatePlayer(int gamerIndex)
        {
            SmuckPlayer p = base.CreatePlayer(gamerIndex);

            p.dampingRatio     = .99f;
            p.autoCenterOnLane = false;
            p.impulseScale     = 10f;
            return(p);
        }
 public override void DestroyElement(DisplayObject obj)
 {
     base.DestroyElement(obj);
     if (obj is SmuckPlayer)
     {
         SmuckPlayer p = (SmuckPlayer)obj;
         p.LivingState = LivingState.Dead;
         if (p.roundScore >= pointsToWinRound && p.skipDeathMarker)
         {
             isLevelOver = true;
         }
         else if (!p.skipDeathMarker)
         {
             CreateDeadIcon(p, DeadIconType.Death);
         }
     }
 }
        private void CreateDeadIcon(SmuckPlayer p, DeadIconType type)
        {
            int      dIndex = deadIcons.Count;
            DeadIcon di;

            if (this is SteamRollerScreen)
            {
                di = (DeadIcon)CreateInstanceAt("flatteningPlayer" + (int)p.gamePadIndex, "deadIcons" + dIndex, p.X, p.Y, 0, iconDepthCount++);
            }
            else if (p.isExploding)
            {
                di = (DeadIcon)CreateInstanceAt("explodingPlayer", "deadIcons" + dIndex, p.X, p.Y, 0, iconDepthCount++);
            }
            else
            {
                di = (DeadIcon)CreateInstanceAt("deadIcon", "deadIcons" + dIndex, p.X, p.Y, 0, iconDepthCount++);
            }

            // Compact framework doesn't support Enum.GetValues
            di.IconType      = (DeadIconType)1;// (DeadIconType)rnd.Next(deadIconCount);
            di.Depth         = iconDepthCount;
            di.PlayheadWrap += new AnimationEvent(di_PlayheadWrap);
            di.Play();
        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (needsTrialPitch && !showingTrialPitch && gameTime.TotalGameTime > trialDuration)
            {
                needsTrialPitch = false;
                gameOverlay.TrialExpired();
            }
            if (!firstUpdate && gameOverlay.hasActivePanel)
            {
                base.ManageInput(gameTime);
                gameOverlay.Update(gameTime);
            }
            else if (isActive)
            {
                base.Update(gameTime);
                firstUpdate = false;
                for (int i = 0; i < lanes.Length; i++)
                {
                    lanes[i].Update(gameTime, this);
                }

                for (int i = 0; i < players.Count; i++)
                {
                    SmuckPlayer p = players[i];
                    if (p != null)
                    {
                        if (p.LivingState == LivingState.Alive)
                        {
                            int curLane = GetLaneFromY((int)(p.Y + p.Height / 2));
                            if (curLane != p.Lane.laneIndex)
                            {
                                p.snapLaneY = lanes[curLane].yLocation + lanes[curLane].laneHeight / 2;
                                gameOverlay.SetScore(p);
                                p.Lane = lanes[curLane];

                                // make it harder and harder after success level of points
                                if (p.roundScore >= pointsToWinRound)
                                {
                                    if (winningPlayer == -1)
                                    {
                                        winningPlayer = i;
                                        p.CreateFlameEffect();
                                        if (defaultShader != null)
                                        {
                                            ((DesaturationShader)defaultShader).level = .2f;// (70f - starParticles.NumChildren) / 70f;
                                        }
                                    }

                                    if (endOnLastLane)
                                    {
                                        isLevelOver = true;
                                    }

                                    if (p.Lane.LaneKind != LaneKind.Sidewalk)
                                    {
                                        p.Lane.minCreationDelay = (int)(p.Lane.minCreationDelay * .9);
                                        p.Lane.maxCreationDelay = (int)(p.Lane.maxCreationDelay * .9);
                                        p.Lane.vehicleSpeed    += 3;
                                    }
                                }
                            }
                        }
                        else if (p.IsOnStage && !p.skipDeathMarker && p.LivingState == LivingState.Dying)
                        {
                            float vel = p.body.GetLinearVelocity().LengthSquared();
                            if (p.body.GetLinearVelocity().LengthSquared() < 100)
                            {
                                p.DestroyAfterUpdate();
                            }
                        }
                    }
                }
            }
            stage.audio.Update();
        }
        public override void BeginContact(Contact contact)
        {
            collisionObjA = (V2DSprite)contact.GetFixtureA().GetBody().GetUserData();
            collisionObjB = (V2DSprite)contact.GetFixtureB().GetBody().GetUserData();

            SmuckPlayer p            = null;
            V2DSprite   nonPlayerObj = null;

            if (collisionObjA is SmuckPlayer)
            {
                p            = (SmuckPlayer)collisionObjA;
                nonPlayerObj = collisionObjB;
            }
            else if (collisionObjB is SmuckPlayer)
            {
                p            = (SmuckPlayer)collisionObjB;
                nonPlayerObj = collisionObjA;
            }

            if (p != null)
            {
                LaneVehicle v = nonPlayerObj is LaneVehicle ? (LaneVehicle)nonPlayerObj : null;
                if (v != null)
                {
                    if (v.Lane.LaneKind == LaneKind.DrownWater && !collideWithBoats)
                    {
                        if (p.LivingState == LivingState.Alive)
                        {
                            p.aboardVehicle = v;
                        }
                        else
                        {
                            p.DestroyAfterUpdate(); // dont want drowning amin over boats
                        }
                    }
                    else
                    {
                        Manifold m;
                        contact.GetManifold(out m);
                        Vector2 dir = m._localNormal * (p == collisionObjA ? 20 : -20) + v.body.GetLinearVelocity() * 10;
                        if (Math.Abs(dir.Y) < 60)
                        {
                            dir.Y += rnd.Next(-400, 400);
                        }
                        p.isExploding = false;
                        p.body.ApplyLinearImpulse(dir, p.body.GetPosition());
                        float torque = dir.Y > 0 ? 1 : -1;
                        p.body.ApplyTorque(torque);
                        p.body.SetAngularVelocity(rnd.Next(15) * torque);

                        if (p.LivingState == LivingState.Alive) // first hit a whack
                        {
                            stage.audio.PlaySound(Sfx.whack);
                        }
                        stage.audio.PlaySound(Sfx.secondWhack);

                        this.KillPlayer(p);
                    }
                }
                else
                {
                    if (nonPlayerObj.InstanceName.StartsWith("water"))
                    {
                        p.IsOnWater = true;
                        if (p.aboardVehicle == null)
                        {
                            p.Lane        = lanes[GetLaneFromY((int)nonPlayerObj.Y)];
                            p.LivingState = LivingState.Dying;
                            stage.audio.PlaySound(Sfx.bigSplash);
                        }
                    }
                    else if (p.LivingState == LivingState.Dying && nonPlayerObj.InstanceName.StartsWith("border"))
                    {
                        // no death icon when flying off left or right side of highway
                        if (nonPlayerObj.Index == 0 || nonPlayerObj.Index == 2)
                        {
                            p.skipDeathMarker = true;
                        }
                        p.DestroyAfterUpdate();
                    }
                }
            }
            else if (collisionObjA is LaneVehicle && collisionObjB is LaneVehicle)
            {
                LaneVehicle vA    = (LaneVehicle)collisionObjA;
                LaneVehicle vB    = (LaneVehicle)collisionObjB;
                const float boost = 15;
                if (vA.Lane.movesRight && vB.Lane.movesRight)
                {
                    if (vA.Position.X > vB.Position.X)
                    {
                        vA.MaxSpeed = vA.Lane.vehicleSpeed + boost;
                        vB.MaxSpeed = vA.MaxSpeed - boost;
                    }
                    else
                    {
                        vB.MaxSpeed = vB.Lane.vehicleSpeed + boost;
                        vA.MaxSpeed = vB.MaxSpeed - boost;
                    }
                }
                else if (!vA.Lane.movesRight && !vB.Lane.movesRight)
                {
                    if (vA.Position.X > vB.Position.X)
                    {
                        vB.MaxSpeed = vB.Lane.vehicleSpeed + boost;
                        vA.MaxSpeed = vB.MaxSpeed - boost;
                    }
                    else
                    {
                        vA.MaxSpeed = vA.Lane.vehicleSpeed + boost;
                        vB.MaxSpeed = vA.MaxSpeed - boost;
                    }
                }
            }
        }
        protected virtual SmuckPlayer CreatePlayer(int gamerIndex)
        {
            NetworkGamer g = null;
            GamerCollection <LocalNetworkGamer> lngc = NetworkManager.Session.LocalGamers;

            for (int i = 0; i < lngc.Count; i++)
            {
                if (lngc[i].SignedInGamer != null && (int)lngc[i].SignedInGamer.PlayerIndex == gamerIndex)
                {
                    g = lngc[i];
                    break;
                }
            }

            SmuckPlayer result = players[gamerIndex];

            if (result != null)
            {
                //result.manualPlayerDestroy = true;
                //result.ReplaceView(playerDefinitionName + gamerIndex);
                result.Stop();
                if (result.body != null)
                {
                    result.body.SetLinearVelocity(Vector2.Zero);
                }

                if (!this.Contains(result))
                {
                    AddChild(result);
                }

                stage.audio.PlaySound(Sfx.respawn);
                //result.manualPlayerDestroy = false;
            }
            else
            {
                //PlayerIndex gamePadIndex = (PlayerIndex)(players.FindAll(p => p.isLocal).Count);
                result = (SmuckPlayer)CreateInstanceAt("player" + gamerIndex, "players" + gamerIndex,
                                                       startLocations[gamerIndex, 0],
                                                       0,
                                                       startLocations[gamerIndex, 2],
                                                       playerDepthCount++);

                //result.isLocal = g.IsLocal;
                //result.NetworkGamer = g;
                //result.NetworkId = g.Id;
                result.Depth        = playerDepthCount;
                result.gamePadIndex = (PlayerIndex)gamerIndex;
                if (g is LocalNetworkGamer)
                {
                    result.gamePadIndex = ((LocalNetworkGamer)g).SignedInGamer.PlayerIndex;

                    // sometimes that isn't right, or even connected it seems, so find a connected controller
                    if (!GamePad.GetState(result.gamePadIndex).IsConnected)
                    {
                        for (PlayerIndex pi = PlayerIndex.One; pi < PlayerIndex.Four; pi++)
                        {
                            if (GamePad.GetState(pi).IsConnected)
                            {
                                result.gamePadIndex = pi;
                                break;
                            }
                        }
                        GamePadCapabilities gpt = GamePad.GetCapabilities(result.gamePadIndex);
                    }
                    g.Tag = result;
                }



                // compact framework doesn't support Array.Find
                // InputManager im = Array.Find(inputManagers, m => (m != null) && (m.NetworkGamer == g));
                InputManager im = inputManagers[gamerIndex];
                im.Player = result;
            }

            result.laneCount = lanes.Length;
            int startLane = startLocations[gamerIndex, 1] == 0 ? 0 : result.laneCount - 1;

            result.snapLaneY = lanes[startLane].yLocation + lanes[startLane].laneHeight / 2;
            result.Reset(startLocations[gamerIndex, 0], result.snapLaneY, startLocations[gamerIndex, 2]);
            result.Lane = lanes[GetLaneFromY((int)result.Y)];

            gameOverlay.SetPlayer(result);

            return(result);
        }
        public override bool OnPlayerInput(int playerIndex, Move move, TimeSpan time)
        {
            bool result = base.OnPlayerInput(playerIndex, move, time);

            if (result && players[playerIndex] != null)
            {
                SmuckPlayer pl = players[playerIndex];
                if (move == Move.Start)
                {
                    PauseAllVehicleSounds();
                    gameOverlay.PauseGame();
                }
                else if (gameOverlay.hasActivePanel)
                {
                    gameOverlay.OnPlayerInput(playerIndex, move, time);
                }
                else if (move == Move.ButtonA && !CheckRoundOver())
                {
                    if (pl == null || (pl != null && pl.LivingState == LivingState.Dead))
                    {
                        InputManager im = inputManagers[playerIndex];
                        CreatePlayer(playerIndex);
                    }
                }
                else if (move == Move.ButtonY && !isLevelOver && pl.LivingState == LivingState.Alive)
                {
                    if (rnd.Next(5) == 0)
                    {
                        Cue c = stage.audio.PlaySound(Sfx.screamTaz, onTazFinished);
                        pl.isExploding = true;
                        KillPlayer(pl);
                    }
                    else
                    {
                        stage.audio.PlaySound(Sfx.scream);
                    }
                }
                //else if (move == Move.ButtonA)
                //{
                //    if (pl != null)
                //    {
                //        pl.Jump();
                //    }
                //}
                //else if (move == Move.ButtonY)
                //{
                //   pl.CreateFlameEffect();
                //}
            }
            else
            {
                // new player joining
                if (move == Move.ButtonA)
                {
                    InputManager im = inputManagers[playerIndex];
                    im.PlayerJoinState = PlayerJoinState.Joined;
                    CreatePlayer(playerIndex);
                }
            }
            return(result);
        }