예제 #1
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            fireDirection  = 0;
            phaserStrength = 0;

            canFireTorpedo = true;
            canFirePhaser  = true;
            canFire        = canFireTorpedo && canFirePhaser;

            LSUCharge         = 0;
            power             = 1;
            propulsion        = 1;
            hasAddedExplosion = true;

            turretFrame = new Rectangle(300, 140, 200, 200);
            tubeFrames  = new Rectangle[]
            {
                new Rectangle(392, 110, 20, 40),
                new Rectangle(490, 230, 40, 20),
                new Rectangle(392, 330, 20, 40),
                new Rectangle(270, 230, 40, 20)
            };

            torpedo = new Torpedo();
            phaser  = new Phaser();

            borgs       = new List <Borg>();
            borgBullets = new List <BorgBullet>();
            explosions  = new List <Explosion>();
            random      = new Random();

            LSUChargeBar   = new Rectangle(70, 12, 100, 20);
            powerBar       = new Rectangle(70, 42, 100, 20);
            propulsionBar  = new Rectangle(70, 72, 100, 20);
            phaserPowerBar = new Rectangle(70, 102, 100, 20);

            IsMouseVisible = true;
            base.Initialize();
        }
예제 #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();
            }

            torpedo.Move();
            phaser.Update();
            canFireTorpedo = torpedo.hasExploded;
            canFirePhaser  = !phaser.isActive;
            canFire        = canFireTorpedo && canFirePhaser;

            GamePadState currState = GamePad.GetState(PlayerIndex.One);

            if (currState.IsButtonDown(Buttons.DPadUp))
            {
                fireDirection = 0;
            }

            if (currState.IsButtonDown(Buttons.DPadRight))
            {
                fireDirection = 1;
            }

            if (currState.IsButtonDown(Buttons.DPadDown))
            {
                fireDirection = 2;
            }

            if (currState.IsButtonDown(Buttons.DPadLeft))
            {
                fireDirection = 3;
            }

            if (currState.IsButtonDown(Buttons.A))
            {
                phaserStrength = 0;
            }

            if (currState.IsButtonDown(Buttons.B))
            {
                phaserStrength = 1;
            }

            if (currState.IsButtonDown(Buttons.X))
            {
                phaserStrength = 2;
            }

            if (currState.IsButtonDown(Buttons.Y))
            {
                phaserStrength = 3;
            }

            propulsion += (int)currState.ThumbSticks.Left.X;
            power      += (int)currState.ThumbSticks.Left.Y;

            if (propulsion < 1)
            {
                propulsion = 1;
            }
            if (propulsion > 9)
            {
                propulsion = 9;
            }

            if (power < 1)
            {
                power = 1;
            }
            if (power > 9)
            {
                power = 9;
            }


            if (currState.IsButtonDown(Buttons.LeftTrigger) && canFire)
            {
                canFireTorpedo    = false;
                hasAddedExplosion = false;


                if (power + propulsion > LSUCharge)
                {
                    torpedo   = new Torpedo(fireDirection, LSUCharge / 2, LSUCharge / 2);
                    LSUCharge = 0;
                }

                else
                {
                    torpedo    = new Torpedo(fireDirection, power, propulsion);
                    LSUCharge -= power;
                    LSUCharge -= propulsion;
                }

                torpedoEffect.Play();
            }

            if (currState.IsButtonDown(Buttons.RightTrigger) && canFire)
            {
                canFirePhaser = false;
                phaser        = new Phaser(phaserStrength, fireDirection);
                phaserEffect.Play();
            }

            if (!hasAddedExplosion && torpedo.hasExploded)
            {
                hasAddedExplosion = true;
                explosions.Add(new Explosion(new Rectangle(torpedo.frame.X, torpedo.frame.Y, torpedo.frame.Height, torpedo.frame.Height)));
                explosionEffect.Play();
            }

            if (tics % 60 == 0)
            {
                LSUCharge += LSUCharge + 3 < 100 ? 3 : 100 - LSUCharge;
            }

            if (random.NextDouble() < 0.0167)
            {
                int direction = random.Next(4);

                switch (direction)
                {
                case 0:
                    borgs.Add(new Borg(0));
                    break;

                case 1:
                    borgs.Add(new Borg(1));
                    break;

                case 2:
                    borgs.Add(new Borg(2));
                    break;

                case 3:
                    borgs.Add(new Borg(3));
                    break;
                }
            }

            for (int i = 0; i < borgs.Count; i++)
            {
                Borg temp = borgs.ElementAt(i);

                if (temp.frame.Intersects(torpedo.frame) || temp.frame.Intersects(phaser.frame) || temp.timeVisible == 0)
                {
                    borgs.RemoveAt(i);
                    i--;
                    continue;
                }

                borgs.ElementAt(i).DecrementTime();

                if (temp.shotTime == 0)
                {
                    borgBullets.Add(new BorgBullet(temp.GetShotRectangle(), temp.side));
                    borgEffect.Play();
                }
            }

            for (int i = 0; i < borgBullets.Count; i++)
            {
                borgBullets.ElementAt(i).Move();
                if (borgBullets.ElementAt(i).frame.Intersects(turretFrame))
                {
                    borgBullets.RemoveAt(i);
                    i--;
                }
            }

            for (int i = 0; i < explosions.Count; i++)
            {
                explosions.ElementAt(i).timer--;

                if (explosions.ElementAt(i).timer == 0)
                {
                    explosions.RemoveAt(i);
                    i--;
                }
            }

            tics++;

            base.Update(gameTime);
        }