Exemple #1
0
        public override void Update(GameTime gameTime)
        {
            CheckToSpawnEnemy(gameTime);
            //UpdateModels(gameTime);

            //Quadtree is use for reduce cpu time in relation to
            //the collisions between bullets and enemy tanks
            int worldSize = 2400;
            int maxDepth = 7;
            int maxNodeObject = 5;
            Point center = new Point(0, 0);
            Quadtree quadtree_Enemy = new Quadtree (worldSize,maxDepth,maxNodeObject,center);
            Quadtree quadtree_obstacles = new Quadtree(worldSize, maxDepth, maxNodeObject, center);

            //if (pursuitenemy.pathdebug != null)
            //{
            //    foreach (Vector3 track in pursuitenemy.pathdebug)
            //    {
            //        pathtrack.Add(new Wallstone(Game.Content.Load<Model>(@"Models/Obstacle/stone"), track));

            //    }
            //    pursuitenemy.pathdebug = null;
            //    foreach (BasicModel track in pathtrack)
            //    {
            //        models.Add(track);
            //    }
            //    pathtrack.Clear();

            //}

            foreach (BasicModel model in models)
            {
                model.Update(gameTime);
            }
            currentPosition = tank.CurrentPosition.ToString();
            pickPosition = tank.PickPosition.ToString();

            foreach (BasicModel model in bullets)
            {

                model.Update(gameTime);
            }

            foreach (BasicModel model in obstacles)
            {
                quadtree_obstacles.Add(model);
                model.Update(gameTime);
            }

            foreach (BasicModel model in enemies)
            {
                quadtree_Enemy.Add(model);
                model.Update(gameTime);
            }

            for(int i = 0; i<enemies.Count;i ++)
            {
                if (enemies[i].CollidesWith(tank.model, tank.world))
                {
                    if (enemies[i] is Human)
                    {
                        enemies.RemoveAt(i);

                        --i;
                        ((Game1)Game).reduceHealth();
                        ((Game1)Game).DeductPoints();
                        break;
                    }
                    else
                    {
                    enemies.RemoveAt(i);
                    ((Game1)Game).kill();
                    --i;
                    ((Game1)Game).reduceHealth();
                    break;
                }
            }
            }

            for (int i = 0; i< bullets.Count;i++)
            {
                float x = bullets[i].world.Translation.X;
                float y = bullets[i].world.Translation.Z;
                Quadtree nearWalls = quadtree_obstacles.GetNodeContaining(x, y);

                foreach (BasicModel walls in obstacles)
                {
                    if (bullets[i].CollidesWith(walls.model, walls.world))
                    {
                        bullets.RemoveAt(i);
                        i--;
                        break;
                    }
                }
            }

            for (int i = 0;i< bullets.Count;i++)
            {
                float x= bullets[i].world.Translation.X;
                float y = bullets[i].world.Translation.Z;
                //Enemies collides with player (player health -)
                Quadtree nearEnemies = quadtree_Enemy.GetNodeContaining(x, y);

                foreach (BasicModel enemy in nearEnemies.models)
                {
                    if (bullets[i].CollidesWith(enemy.model, enemy.world))
                    {
                        bullets.RemoveAt(i);
                        ((Game1)Game).soundHit.Play();
                        if (enemy is Human)
                        {
                            ((Game1)Game).DeductPoints();
                             ((Game1)Game).reduceHealth();
                        }
                        else
                        {
                        ((Game1)Game).AddPoints();
                        }
                        enemies.Remove(enemy);

                        --i;
                        break;
                    }
                }
            }

            Quadtree nearObstacles = quadtree_obstacles.GetNodeContaining(tank.translation.Translation.X, tank.translation.Translation.Z);

            foreach (BasicModel model in nearObstacles.models )
            {

                if (model.CollidesWith(tank.model,tank.world))
                {

                    tank.CurrentPosition = tank.CurrentPosition - tank.velocity*(gameTime.ElapsedGameTime.Milliseconds) /100;
                    tank.velocity = Vector3.Zero;
                    //tank.tankDirection = -tank.tankDirection;
                    //tank.PickPosition = tank.CurrentPosition;

                }
            }

            updateShots(gameTime);

            base.Update(gameTime);
        }