public void Add(AnimatedSpriteEx sprite)
 {
     mSprites.Add(sprite);
 }
Exemplo n.º 2
0
        public override void Update(GameTime gameTime)
        {
            float mls = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;

            if (!bInitialized)
            {
                ResetPosition();
                bInitialized = true;
                return;
            }

            Vector2 mergedMoveDirection = new Vector2();

            if (this.IsMerged)
            {
                if (!Player.IsThisPlayerCaptain(this))
                {
                    base.Update(gameTime);
                    return;
                }
                else
                {
                    if (m_PlayerMerges.ContainsKey(this.id))
                    {
                        // Check if someone is trying to break the merge which
                        // anyone in the merge group is allowed to do
                        if (m_PlayerMerges[this.id].IsAnyoneTryingToBreakTheMerge() || IsMergeBreakButtonPressed())
                        {
                            // If yes then this captain and its merge need to be removed from our
                            // merge list
                            Player.RemoveMergeList(this.id);

                            this.IsMerged = false;

                            this.Reset(mDefaultSize);
                        }
                        else
                        {
                            // Set state to always running.  Update the animation here!
                            //
                            this.PlayerState = ePlayerState.RUN;
                            this.m_runAnim.Update(gameTime);
                            Player.m_mergeMonsterAnim.Update(gameTime);

                            mergedMoveDirection = new Vector2();
                            mergedMoveDirection = GetAvgDirectionForAllPlayers(this);

                            // There is always a speed movement applied when the players
                            // are merged
                            //
                            if (mergedMoveDirection.Length() == 0)
                                mergedMoveDirection = new Vector2(1, 1);

                            this.mVelocity.X = mls * (mergedMoveDirection.X * 800.0f);
                            this.mVelocity.Y = mls * (mergedMoveDirection.Y * 800.0f);

                            //mergedMoveDirection *= new Vector2(5, 5);

                            this.mPositionX += this.mVelocity.X;// mergedMoveDirection.X;
                            this.mPositionY += this.mVelocity.Y;//mergedMoveDirection.Y;

                            base.Update(gameTime);

                            return;
                        }
                    }
                }
            }

            // Check for collisions with other players
            foreach (Player p in Program.Instance.GamePlayers)
            {
                // Don't bother checking if this player has collided with itself
                //
                if (p.id == this.id)
                    continue;

                if (CheckForCollision(p))
                {
                    // Case 1 - I am not merged with any other players
                    if ( !IsMerged || !p.IsMerged )
                    {
                        // TODO - Remove for final game!
                        if (IsMergeButtonPressed() && p.IsMergeButtonPressed())
                        {
                            m_bIsMerged = true;

                            // Has a captain been selected?
                            // If yes then add me to the list of merged players for this list
                            if (m_PlayerMerges.ContainsKey(this.id))
                            {
                                m_PlayerMerges[this.id].Add(p);
                                p.IsMerged = true;
                            }
                            // If no then make me the captain
                            else
                            {
                                m_PlayerMerges[this.id] = new PlayerMergeList();

                                p.IsMerged = true;
                                m_PlayerMerges[this.id].Add(p);
                            }
                        }
                    }

                    // Case 2 - I am merged with one or more players
                    else
                    {
                        // Another player is trying to join the collective.  Since all parties involved in the
                        // collision will send a collision event only the captain should check if members
                        // of its list have merge keys pressed.  To avoid doing too much processing.
                        //
                       if (Player.IsThisPlayerCaptain(this))
                       {
                           // Checked if this player is already merged in with this captain.  If so
                           // then don't bother checking if we should merge it.
                           //
                           if (!p.IsMerged)
                           {
                               //bool bAllPlayersReadyToMerge = false;
                               //foreach (Player pMerged in Player.m_PlayerMerges[this.id].players)
                               //{
                                   // All players in the collective will need to have their merge
                                   // buttons pressed as well as the new player wanting to join to add
                                   // the new player

                                   // If all players have their merge buttons pressed
                                   // If yes then add the new player to the merged player list
                                   //if (!pMerged.IsMergeButtonPressed())
                                   //{
                                     //  bAllPlayersReadyToMerge = false;
                                       //break;
                                   //}
                               //}

                               // Check if all the players who are already merged and the new merged player have their keys
                               // pressed to merge.  If they do then merge them!
                               //
                               if (Player.m_PlayerMerges[this.id].AreAllPlayersReadyToMerge() && p.IsMergeButtonPressed())
                                   Player.m_PlayerMerges[this.id].Add(p);
                           }
                       }
                    }
                }
            }

            SetState();

            switch(this.PlayerState)
            {
                case ePlayerState.IDLE:
                    this.m_idleFrontAnim.Update(gameTime);
                    break;

                case ePlayerState.RUN:
                    this.m_runAnim.Update(gameTime);
                    break;
            }

             	        List<Enemy> destroy = new List<Enemy>();

            foreach (Enemy e in Program.Instance.mEnemies)
            {

                if (CheckForCollision(e))
                {
                    if (e.PixelWidth < this.PixelWidth)
                    {
                        AudioManager.Instance.PlaySound("MonsterDie");

                        AnimatedSpriteEx killAnim = new AnimatedSpriteEx(this.Game);

                        killAnim.Position =
                            new Vector2(e.mPosition.X - (e.PixelWidth * 2),
                                        e.mPosition.Y - (e.PixelHeight * 2));

                        killAnim.Load(this.Game.Content, new Vector2(0, 0), "\\kill anim\\Kill", 8, 20);

                        AnimatedSpriteManager.Instance.Add(killAnim);

                        e.Destroy();
                        destroy.Add(e);

                        Vector2 pos = mPosition;

                        PixelWidth += 5;
                        PixelHeight += 5;

                        //mScale.X *= 1.1f;
                        //mScale.Y = mScale.X;
                        mPosition = pos;
                        //mBounds.Radius = Radius;
                        //mBounds.Center.X = mPositionX;
                        //mBounds.Center.Y = mPositionY;
                        this.SetBboxPos(mPosition);

                        this.m_idleFrontAnim.Scale = mScale.X;
                        this.m_runAnim.Scale = mScale.X;

                        Program.Instance.mEnemiesKilled++;
                        if (Program.Instance.mEnemiesKilled % 3 == 0)
                        {
                            if (Program.Instance.mMaxEnemies < 100)
                                Program.Instance.mMaxEnemies++;
                        }
                    }
                    else
                    {
                        e.Destroy();
                        destroy.Add(e);

                        Reset((int)(PixelWidth*0.5f));
                    }
                }
            }

            foreach (Enemy e in destroy)
            {
                Program.Instance.mEnemies.Remove(e);
            }

            GamePadState state = GamePad.GetState(id);

            if (mIsUsingKeyboard)
            {
                if (Keyboard.GetState().GetPressedKeys().Length > 0)
                {
                    if (id == PlayerIndex.One)
                    {
                        this.moveY = Keyboard.GetState().IsKeyDown(Keys.W) ? -1 : 0;
                        this.moveX = Keyboard.GetState().IsKeyDown(Keys.D) ? 1 : 0;

                        if (moveX == 0)
                            moveX = Keyboard.GetState().IsKeyDown(Keys.A) ? -1 : 0;

                        if (moveY == 0)
                            moveY = Keyboard.GetState().IsKeyDown(Keys.S) ? 1 : 0;
                    }
                    else if (id == PlayerIndex.Two)
                    {
                        this.moveY = Keyboard.GetState().IsKeyDown(Keys.I) ? -1 : 0;
                        this.moveX = Keyboard.GetState().IsKeyDown(Keys.L) ? 1 : 0;

                        if (moveX == 0)
                            moveX = Keyboard.GetState().IsKeyDown(Keys.J) ? -1 : 0;

                        if (moveY == 0)
                            moveY = Keyboard.GetState().IsKeyDown(Keys.K) ? 1 : 0;
                    }

                    mVelocity.X += mls * (moveX * 1000);
                    mVelocity.Y += mls * (moveY * 1000);

                    Vector2 drag = new Vector2(-mVelocity.X, -mVelocity.Y);
                    if (drag.Length() != 0)
                    {
                        drag.Normalize();
                        mVelocity = mVelocity + mls * (drag * (mVelocity.Length()*2));
                    }

                    if (mVelocity.Length() > mMaxSpeed)
                    {
                        mVelocity.Normalize();
                        mVelocity = mVelocity * mMaxSpeed;
                    }

                    // set position
                    mPositionX = mPosition.X + mls * mVelocity.X;
                    mPositionY = mPosition.Y + mls * mVelocity.Y;

                    if (mPositionX + Radius > Config.Instance.GetAsInt("ScreenWidth"))
                        mPositionX = Config.Instance.GetAsInt("ScreenWidth") - Radius;
                    if (mPositionX - Radius < 0)
                        mPositionX = Radius;
                    if (mPositionY + Radius > Config.Instance.GetAsInt("ScreenHeight"))
                        mPositionY = Config.Instance.GetAsInt("ScreenHeight") - Radius;
                    if (mPositionY - Radius < 0)
                        mPositionY = Radius;

                    base.Update(gameTime);
                    return;
                }
            }

            this.moveX = state.ThumbSticks.Left.X;
            this.moveY = state.ThumbSticks.Left.Y;

            state.ThumbSticks.Left.Normalize();

            this.mVelocity.X += mls * (state.ThumbSticks.Left.X*1000);
            this.mVelocity.Y += mls * (-state.ThumbSticks.Left.Y*1000);

            Vector2 drag2 = new Vector2(-mVelocity.X, -mVelocity.Y);
            if (drag2.Length() != 0)
            {
                drag2.Normalize();
                mVelocity = mVelocity + mls * (drag2 * (mVelocity.Length() * 2));
            }

            if (mVelocity.Length() > mMaxSpeed)
            {
                mVelocity.Normalize();
                mVelocity = mVelocity * mMaxSpeed;
            }

            // set position
            mPositionX = mPosition.X + mls * mVelocity.X;
            mPositionY = mPosition.Y + mls * mVelocity.Y;

            if (mPositionX + Radius > Config.Instance.GetAsInt("ScreenWidth"))
                mPositionX = Config.Instance.GetAsInt("ScreenWidth") - Radius;
            if (mPositionX - Radius < 0)
                mPositionX = Radius;
            if (mPositionY + Radius > Config.Instance.GetAsInt("ScreenHeight"))
                mPositionY = Config.Instance.GetAsInt("ScreenHeight") - Radius;
            if (mPositionY - Radius < 0)
                mPositionY = Radius;

            base.Update(gameTime);
        }