示例#1
0
        protected override void Update(GameTime gameTime)
        {
            switcher.CheckStates();

            if (GameState == 0) //menu
            {
                menu.Update(gameTime);
                CurrentLevel = 1;
                scoreBoard   = 0;
                won          = false;
            }
            else if (GameState == 6) //howtoplay
            {
                if ((Keyboard.GetState().IsKeyDown(Keys.Escape)) || (Keyboard.GetState().IsKeyDown(Keys.Back)))
                {
                    GameState = 0;
                }
            }
            if ((GameState == 2) && (!GameOver))
            {
                if ((enemiesToKill == 0) && (!GameOver)) //prechod do dalsieho levelu
                {
                    if (!switcher.switching)
                    {
                        CurrentLevel += 1;
                        if (CurrentLevel > Constants.TotalLevels)
                        {
                            switcher.SwitchState(4, 0);
                            won = true;
                        }
                        else
                        {
                            switcher.SwitchState(3, 3000);
                        }
                    }
                }

                if ((scoreBoard / 10 > 0) && (!rewardOnScene))
                //if ((!rewardOnScene)&&((Player1.Score+player2.Score) % 1600 == 0)&&(Player1.Score+player2.Score>=0))
                {
                    scoreBoard -= rnd.Next(8) + 4;
                    int x = rnd.Next(Constants.LevelWidth - Constants.BrickSize / 2) + Constants.OriginX + Constants.BrickSize;
                    int y = rnd.Next(Constants.LevelHeigth - Constants.BrickSize * 2) + Constants.OriginY + Constants.BrickSize;

                    reward = new Animation();
                    reward.Initialize(x, y, 40, 40, 20, true, true, 7);
                    reward.Type = rnd.Next(4);
                    reward.LoadGraphicsContent(spriteBatch, Res.Textures["Reward_" + reward.Type]);
                    Animations.Add(reward);
                    Rewards.Add(reward);
                    planner.PlanEvent("cancelReward", rnd.Next(3000) + 6000, 0);
                    rewardOnScene = true;
                }
            }


            for (int i = 0; i < Animations.Count; i++)
            {
                Animations[i].Update(gameTime);
                if (Animations[i].Removable)
                {
                    Animations.RemoveAt(i);
                }
            }


            if (GameState == 2) //hraj
            {
                timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds / 2;
                if (timeSinceLastFrame > milisecondsPerFrame)
                {
                    timeSinceLastFrame = 0;

                    myKeyboard.Update(this, gameTime);
                    myKeyboard2.Update(this, gameTime);

                    //nastavi x,y prirastok k suradniciam tanku podla daneho smeru
                    foreach (Tank Player in Players)
                    {
                        if (Player.Alive)
                        {
                            Player.CalculateXY(Constants.TanksSpeed[Player.Type]);
                        }
                    }

                    if ((Player1.Lives == 0) && (Player2.Lives == 0))
                    {
                        GameOver = true;
                    }

                    #region CheckRewardsCollision

                    foreach (Tank Player in Players)
                    {
                        if (Player.Alive)
                        {
                            Animation temp;
                            for (int i = 0; i < Rewards.Count; i++)
                            {
                                temp = Rewards[i];
                                if (Player.BoundingRectangle.Intersects(temp.BoundingRectangle))
                                {
                                    Player.Score += 400;
                                    for (int j = 0; j < Animations.Count; j++)
                                    {
                                        if (Rewards[i] == Animations[j])
                                        {
                                            Animations.RemoveAt(j);
                                        }
                                    }
                                    Rewards.RemoveAt(i);
                                    rewardOnScene = false;
                                    switch (temp.Type)
                                    {
                                    case 0: Player.Lives += 1; break;

                                    case 1: if (Player.MaxShots < 3)
                                        {
                                            Player.MaxShots += 1;
                                        }
                                        break;

                                    case 2: if (Player.ShotSpeed <= 12)
                                        {
                                            Player.ShotSpeed += 2;
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    #endregion

                    //skontroluje, ci hrac narazi v danom smere do nejakej tehly, ak ano nastavi Collision na true
                    #region CheckCollisionTank

                    Player1.Collision = false;
                    Player2.Collision = false;
                    if ((player2.Alive) && (player1.NextFrameBound().Intersects(player2.BoundingRectangle)))
                    {
                        player1.Collision = true;
                    }
                    if ((player1.Alive) && (player2.NextFrameBound().Intersects(player1.BoundingRectangle)))
                    {
                        player2.Collision = true;
                    }

                    if ((player1.BoundingRectangle.Intersects(player2.BoundingRectangle)))
                    {
                        player1.Collision = false;
                        player2.Collision = false;
                    }

                    foreach (Tank Player in Players)
                    {
                        if (Player.Alive)
                        {
                            for (int j = 0; j < Bricks.Count; j++)
                            {
                                brick = Bricks[j];
                                if (!brick.Alive)
                                {
                                    Bricks.RemoveAt(j--);
                                }
                                if ((!brick.Passable) && (brick.BoundingRectangle.Intersects(Player.NextFrameBound())))
                                {
                                    Player.Collision = true;
                                }
                            }

                            if ((Player.NextFrameBound().Top < Constants.OriginY) || (Player.NextFrameBound().Bottom > Constants.LevelHeigth + Constants.OriginY) ||
                                (Player.NextFrameBound().Left < Constants.OriginX) || (Player.NextFrameBound().Right > Constants.LevelWidth + Constants.OriginX)
                                )
                            {
                                Player.Collision = true;
                            }

                            for (int j = 0; j < Enemies.Count; j++)
                            {
                                if (Player.NextFrameBound().Intersects(Enemies[j].BoundingRectangle))
                                {
                                    Player.Collision = true;
                                }
                                if (Player.BoundingRectangle.Intersects(Enemies[j].BoundingRectangle))
                                {
                                    Player.Collision = false;
                                }
                            }
                        }
                    }

                    for (int i = 0; i < Enemies.Count; i++)
                    {
                        EnemyTank tempEnemy = Enemies[i];
                        if (tempEnemy.Alive)
                        {
                            for (int j = 0; j < Bricks.Count; j++)
                            {
                                brick = Bricks[j];
                                if (!brick.Alive)
                                {
                                    Bricks.RemoveAt(j--);
                                }
                                if ((!brick.Passable) && (brick.BoundingRectangle.Intersects(tempEnemy.NextFrameBound())))
                                {
                                    tempEnemy.Collision = true;
                                }
                            }

                            if ((tempEnemy.NextFrameBound().Top < Constants.OriginY) || (tempEnemy.NextFrameBound().Bottom > Constants.LevelHeigth + Constants.OriginY) ||
                                (tempEnemy.NextFrameBound().Left < Constants.OriginX) || (tempEnemy.NextFrameBound().Right > Constants.LevelWidth + Constants.OriginX)
                                )
                            {
                                tempEnemy.Collision = true;
                            }

                            for (int j = 0; j < Enemies.Count; j++)
                            {
                                if ((tempEnemy.BoundingRectangleTop.Intersects(Enemies[j].BoundingRectangle)) && (j != i))
                                {
                                    tempEnemy.Otoc();
                                }
                            }

                            foreach (Tank Player in Players)
                            {
                                if (Player.Alive)
                                {
                                    if ((tempEnemy.NextFrameBound().Intersects(Player.BoundingRectangle)))
                                    {
                                        tempEnemy.Collision = true;
                                    }
                                }
                            }
                        }
                    }

                    #endregion

                    //ak je Collision true, nepusti ho v danom smere

                    #region MoveTanks

                    if (Player1.Alive)
                    {
                        if ((myKeyboard.KeyPressed) && (!Player1.Collision))
                        {
                            Player1.PositionX += Player1.X;
                            Player1.PositionY += Player1.Y;
                        }
                    }

                    if (Player2.Alive)
                    {
                        if ((myKeyboard2.KeyPressed) && (!Player2.Collision))
                        {
                            Player2.PositionX += Player2.X;
                            Player2.PositionY += Player2.Y;
                        }
                    }

                    for (int j = 0; j < Enemies.Count; j++)
                    {
                        if (Enemies[j].Alive)
                        {
                            Enemies[j].UpdateEnemy(gameTime);
                        }
                        else
                        {
                            Enemies.RemoveAt(j);
                        }
                    }
                    #endregion

                    //skontroluje kolizie striel s tehlami, ak nastane, strela.Expode();
                    #region CheckCollisionsShots

                    for (int i = 0; i < Strely.Count; i++)
                    {
                        strela = Strely[i];

                        for (int j = 0; j < Strely.Count; j++)
                        {
                            if ((i != j))
                            {
                                if (strela.CheckCollisionsShot(Strely[j]))
                                {
                                    Vybuch((int)strela.PositionX, (int)strela.PositionY);
                                }
                            }
                        }

                        if (strela.Alive)
                        {
                            if ((strela.BoundingRectangle.Intersects(Eagle.BoundingRectangleBrick)) && (strela.Alive))
                            {
                                Eagle.CalculateSource(2);
                                GameOver     = true;
                                strela.Alive = false;
                            }

                            for (int j = 0; j < Bricks.Count; j++)
                            {
                                brick = Bricks[j];
                                if (!brick.ShotPassable)
                                {
                                    if (strela.CheckCollisions(brick))
                                    {
                                        Vybuch((int)strela.PositionX, (int)strela.PositionY);
                                    }
                                }
                            }

                            EnemyTank temp;

                            if ((strela.ShotBy == player1) || (strela.ShotBy == player2))
                            {
                                for (int j = 0; j < Enemies.Count; j++)
                                {
                                    temp = Enemies[j];
                                    if (temp.Alive)
                                    {
                                        if (strela.CheckCollisionsTank(temp))
                                        {
                                            int value = 0;
                                            switch (temp.Type)
                                            {
                                            case 1: value = 100; break;

                                            case 2: value = 200; break;

                                            case 3: value = 400; break;
                                            }

                                            enemiesToKill -= 1;
                                            temp.Explode();
                                            strela.ShotBy.Score += value;
                                            if (!rewardOnScene)
                                            {
                                                scoreBoard += 1;
                                            }
                                            if (planner.CountEvent("newEnemy") < 1)
                                            {
                                                planner.PlanEvent("enemyBorn", 1300, 1);
                                                planner.PlanEvent("newEnemy", IntervalEnemy, 1);
                                            }
                                            for (int k = 0; k < Strely.Count; k++)
                                            {
                                                if ((Strely[k].ShotBy == temp) && (strela.BoundingRectangle.Intersects(temp.BoundingRectangle)))
                                                {
                                                    Strely[k].Alive = false;
                                                }
                                            }
                                            VybuchTank((int)temp.PositionX, (int)temp.PositionY);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int j = 0; j <= 1; j++)
                                {
                                    if (strela.CheckCollisionsTank(Players[j]))
                                    {
                                        if (!Players[j].Indestructable)
                                        {
                                            Players[j].Lives -= 1;
                                            Players[j].Alive  = false;
                                            if (Players[j].Lives > 0)
                                            {
                                                planner.PlanEvent("bornPlayer", IntervalPlayer, j + 1);
                                                planner.PlanEvent("enemyBorn", 1300, j + 2);
                                            }
                                            VybuchTank((int)Players[j].PositionX, (int)Players[j].PositionY);
                                        }
                                        else
                                        {
                                            Vybuch((int)strela.PositionX, (int)strela.PositionY);
                                        }
                                        strela.Explode();
                                        Strely.RemoveAt(i);
                                    }
                                }
                            }

                            if ((strela.PositionY < Constants.OriginY + strela.Size.Y / 2) || (strela.PositionY > Constants.LevelHeigth + Constants.OriginY - strela.Size.Y / 2) || (strela.PositionX < Constants.OriginX + strela.Size.X / 2) || (strela.PositionX > Constants.LevelWidth + Constants.OriginX - strela.Size.X / 2))
                            {
                                strela.Alive = false;
                                Vybuch((int)strela.PositionX, (int)strela.PositionY);
                            }

                            strela.Update();
                        }
                        else
                        {
                            strela.Explode();
                            Strely.RemoveAt(i--);
                        }
                    }
                }

                previousState = Keyboard.GetState();
                #endregion
            }

            previousState = Keyboard.GetState();
            base.Update(gameTime);
        }