Exemple #1
0
 public ship(Game1 _game, ContentManager _content, Rectangle viewPort, Texture2D _sprite, int _health)
     : base(_game, _content, viewPort, _sprite, _health, false, false, 0.2f)
 {
     //this.position = new Vector2(200f,100f);
     //this.velocity = new Vector2(3f, 0f);
     laser = new Laser(game, content, viewPort, content.Load<Texture2D>("pixel"));
     game.objectManager.Add(laser);
     laser.laserColor = Color.Firebrick;
     mainGun = new GameObject(game, content, viewPort, content.Load<Texture2D>("MainGun"), false, 0.3f);
     game.objectManager.Add(mainGun);
     //mainGun.layer = 0;
     bullets = new Bullet[MAX_BULLETS];
     for (int count = 0; count < MAX_BULLETS; count++)
     {
         bullets[count] = new Bullet(game, game.Content, game.viewportRect, game.Content.Load<Texture2D>("Bullet"), 1);
         game.objectManager.Add(bullets[count]);
     }
     position = new Vector2(120, 280);
     //layer = 1;
 }
Exemple #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            //foreach (Enemy enemy in enemies)
            //{
            //    foreach (StandardAsteroid sa in standardAsteroids)
            //    {
            //        if (enemy.alive && sa.alive)
            //        {
            //            if (checkCollision(enemy, sa))
            //            {
            //                sa.alive = false;
            //            }
            //        }
            //    }
            //}

            float elapsedTime = (float)gameTime.TotalGameTime.TotalMilliseconds;
            explosion.UpdateParticles(elapsedTime);
            objectManager.Update(elapsedTime);

            //Make sure the enemies never overlap
            for (int i = 0; i < MaxEnemies; i++)
            {
                for (int j = 0; j < MaxEnemies; j++)
                {
                    if (i != j)
                    {
                        while (checkCollision(enemies[j], enemies[i]))
                        {
                            enemies[j].position.X = random.Next(0, viewportRect.Right);
                        }
                    }
                }
            }

            //Check if you've shot any enemies
            foreach (Bullet bullet in theShip.bullets)
            {
                if (bullet.alive)
                {

                    foreach (Enemy enemy in enemies)
                    {
                        if ( checkCollision(bullet, enemy) )
                        {
                            bullet.health--;
                            enemy.health--;
                            score++;
                            explosion.AddExplosion(bullet.position, 10, 80.0f, 2000.0f, elapsedTime);
                            break;
                        }
                    }
                }
            }

            //Check if you've been shot by any enemies
            foreach (Enemy enemy in enemies)
            {
                foreach (Bullet bullet in enemy.bullets)
                {
                    if (bullet.alive)
                    {
                        if ( checkCollision(bullet, theShip) )
                        {
                            bullet.alive = false;
                            theShip.health-= random.Next(1,5);
                            healthBar.health = theShip.health;
                            explosion.AddExplosion(bullet.position, 10, 80.0f, 2000.0f, elapsedTime);
                        }
                    }
                }
            }

            if (theShip.health <= 0)
            {
                lives--;
                theShip.health = 100;
            }

            //Check to see if you've shot any asteroids
            foreach (StandardAsteroid sa in standardAsteroids)
            {
                foreach (Bullet bullet in theShip.bullets)
                {
                    if (bullet.alive)
                    {
                        if (checkCollision(bullet, sa))
                        {
                            bullet.health--;
                            sa.health-=5;
                            explosion.AddExplosion(bullet.position, 10, 80.0f, 2000.0f, elapsedTime);
                        }
                    }
                }
            }

            //Check if any asteroids have hit each other
            foreach (StandardAsteroid sa in standardAsteroids)
            {
                foreach (StandardAsteroid sa2 in standardAsteroids)
                {
                    if (!sa.Equals(sa2) && (!sa.hasTouched || !sa2.hasTouched))
                    {
                        if (checkCollision(sa, sa2) )
                        {
                            //make them drift apart
                            //First deal with their velocities
                            Vector2 ov = sa2.velocity;
                            sa2.velocity = sa2.velocity * -1 +  (sa.velocity/2);//sa2.velocity - sa.velocity * 0.5f;
                            sa.velocity = sa.velocity * -1 + (ov/2);
                            sa.hasTouched = true;
                            sa2.hasTouched = true;
                            //Then deal with making them rotate accordingly
                            sa.rotationVelocity += sa2.velocity.X * 0.01f + sa2.velocity.Y * 0.01f;
                            //sa.health--;
                            continue;
                        }
                    }
                }
            }
            foreach (StandardAsteroid sa in standardAsteroids)
            {
                sa.hasTouched = false;
            }
            foreach (StandardAsteroid sa in standardAsteroids)
            {
                foreach (StandardAsteroid sa2 in standardAsteroids)
                {
                    if (!sa.Equals(sa2))
                    {
                        if (checkCollision(sa, sa2))
                        {
                            sa.hasTouched = true;
                        }
                        else
                        {
                            //sa.hasTouched = false;
                        }
                    }
                }
            }
            //Firing the Ship's laser!
            if (Mouse.GetState().LeftButton == ButtonState.Pressed && theShip.laser.lived < theShip.laser.life) //If Imma Firin' mah lazors!
            {

                Boolean hitSomething = false;
                theShip.laser.laserLength = 0;
                theShip.laser.alive = true;
                do
                {
                    //update the laser's position
                    theShip.laser.startPoint = theShip.position + new Vector2((float)Math.Sin(theShip.Rotation),
                            (float)Math.Cos(theShip.Rotation) * -1) * (theShip.sprite.Height / 2);
                    currLaserPoint = theShip.laser.startPoint + new Vector2((float)Math.Sin(theShip.laser.Rotation),
                                (float)Math.Cos(theShip.laser.Rotation) * -1) * (theShip.laser.laserLength) * -1;
                    GameObject pixel = new GameObject(this, Content, viewportRect, Content.Load<Texture2D>("pixel"), false, 0.2f);
                    pixel.position = currLaserPoint;
                    theShip.laser.laserLength++;

                    //Check to see if the laser has hit any asteroids
                    foreach (StandardAsteroid asteroid in standardAsteroids)
                    {
                        if ( checkCollision(pixel, asteroid))
                        {
                            asteroid.health--;
                            if (asteroid.health <= 0)
                            {
                                explosion.AddExplosion(asteroid.position, 10, 80.0f, 2000.0f, elapsedTime);
                            }
                            hitSomething = true;
                            break;
                        }
                    }
                    if (hitSomething)
                    {
                        break;
                    }

                    //Check to see if the laser has hit any enemies
                    foreach (Enemy enemy in enemies)
                    {
                        if (checkCollision(pixel, enemy))
                        {
                            enemy.health--;
                            if (enemy.health <= 0)
                            {
                                explosion.AddExplosion(enemy.position, 10, 80.0f, 2000.0f, elapsedTime);
                            }
                            hitSomething = true;
                            break;
                        }
                    }
                    if (hitSomething)
                    {
                        break;
                    }

                }
                while (viewportRect.Contains((int)currLaserPoint.X, (int)currLaserPoint.Y)) ;

                //if (laser.lived < laser.life)
                //{

                //laser.lived++;
                //}
                //else
                //{
                //laser.alive = false;
                //laser.laserLength = 0;
                //}
            }
            else
            {
                theShip.laser.canFire = true;
                theShip.laser.alive = false;
                if (Mouse.GetState().LeftButton != ButtonState.Pressed)
                {
                    theShip.laser.lived = 0;
                }
                theShip.laser.laserLength = 0;
                currLaserPoint = theShip.position;
            }
            base.Update(gameTime);
        }
Exemple #3
0
        public Boolean checkCollision( GameObject o1, GameObject o2 )
        {
            o1.spriteTransform =
                    Matrix.CreateTranslation(new Vector3(-o1.center, 0.0f)) *
                        // Matrix.CreateScale(block.Scale) *  would go here
                    Matrix.CreateRotationZ(o1.Rotation) *
                    Matrix.CreateTranslation(new Vector3(o1.position, 0.0f));
                    o1.spriteRectangle = CalculateBoundingRectangle(
                        new Rectangle(0, 0, o1.sprite.Width, o1.sprite.Height),
                        o1.spriteTransform);

                    o2.spriteTransform =
                    Matrix.CreateTranslation(new Vector3(-o2.center, 0.0f)) *
                            // Matrix.CreateScale(block.Scale) *  would go here
                    Matrix.CreateRotationZ(o2.Rotation) *
                    Matrix.CreateTranslation(new Vector3(o2.position, 0.0f));
                    o2.spriteRectangle = CalculateBoundingRectangle(
                        new Rectangle(0, 0, o2.sprite.Width, o2.sprite.Height),
                        o2.spriteTransform);
                        if (o1.spriteRectangle.Intersects(o2.spriteRectangle))
                        {
                            if (IntersectPixels(o1.spriteTransform, o1.sprite.Width,
                                               o1.sprite.Height, o1.spriteTextureData,
                                               o2.spriteTransform, o2.sprite.Width,
                                               o2.sprite.Height, o2.spriteTextureData))
                            {
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        }
            return false;
        }
 public void Add(GameObject someObject)
 {
     gameObjects.Add(someObject);
 }