Example #1
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (Alive)
            {
                Body.X += Body.Velocity.X;
                Body.Y += Body.Velocity.Y;

                CurrentDistance += Body.Velocity.X;

                if (CurrentDistance <= 0) //arranjar maneira do inimigo começar na posição final do x que é = obj.width ou seja andar no sentido oposto
                {
                    FacingDirection  = 1; // go left now
                    Body.Velocity.X *= -1;
                }
                else if (CurrentDistance + Body.Bounds.Width >= TravelDistance)
                {
                    FacingDirection  = -1; // go right now
                    Body.Velocity.X *= -1;
                }

                /* Shooting */
                if (this.LastShot < gameTime.TotalGameTime.TotalMilliseconds)
                {
                    //Console.WriteLine("Shooting at " + gameTime.TotalGameTime.TotalMilliseconds);
                    this.LastShot = (float)gameTime.TotalGameTime.TotalMilliseconds + this.ShootRate;

                    // get the first dead bullet
                    Bullet b = null;
                    for (int i = 0; i < Bullets.Count; i++)
                    {
                        if (!Bullets[i].Alive)
                        {
                            b = Bullets[i];
                            break;
                        }
                    }

                    if (b != null)
                    {
                        Random rnd        = new Random();
                        int    YVariation = 4;

                        b.Reset();
                        b.Revive();

                        b.ShotAtMilliseconds = gameTime.TotalGameTime.TotalMilliseconds;

                        b.Body.X = Body.X + (FacingDirection > 0 ? 24 : -2);
                        b.Body.Y = this.Body.Y + rnd.Next(-YVariation, YVariation) + 10;  //TODO: fix 16 offset with final sprites

                        b.Body.Velocity.X = 0;
                        b.Body.Velocity.Y = ShootingVelocity;
                    }

                    b = null;
                    // get the first dead bullet
                    for (int i = 0; i < Bullets.Count; i++)
                    {
                        if (!Bullets[i].Alive)
                        {
                            b = Bullets[i];
                            break;
                        }
                    }

                    if (b != null)
                    {
                        Random rnd        = new Random();
                        int    YVariation = 4;

                        b.Reset();
                        b.Revive();

                        b.ShotAtMilliseconds = gameTime.TotalGameTime.TotalMilliseconds;

                        b.Body.X = Body.X + (FacingDirection > 0 ? 24 : -2);
                        b.Body.Y = this.Body.Y + rnd.Next(-YVariation, YVariation) + 10;  //TODO: fix 16 offset with final sprites

                        b.Body.Velocity.X = 0;
                        b.Body.Velocity.Y = ShootingVelocity * -1;
                    }
                }

                foreach (Bullet b in Bullets)
                {
                    b.Update(gameTime);
                }
            }
        }
Example #2
0
        public void UpdateProjectiles(GameTime gameTime, KeyboardState keyboardState)
        {
            if (Alive)
            {
                this.movementParticleEmitter.Update(gameTime);
                this.movementParticleEmitter.ForEachParticle(KillOutOfBoundsParticle);
                this.movementParticleEmitter.EmitterBox.X = Body.X + 11;
                this.movementParticleEmitter.EmitterBox.Y = Body.Y + 3;
                this.movementParticleEmitter.Activated    = false;

                this.anchorParticleEmitter.Update(gameTime);
                this.anchorParticleEmitter.EmitterBox.X = Body.X + 8;
                this.anchorParticleEmitter.EmitterBox.Y = Body.Y + 16;
                this.anchorParticleEmitter.Activated    = false;
            }

            if ((keyboardState.IsKeyDown(Keys.RightControl) || keyboardState.IsKeyDown(Keys.LeftControl)) && Energy >= BulletCost)
            {
                Animations.Play("shooting");
                if (this.LastShot < gameTime.TotalGameTime.TotalMilliseconds)
                {
                    this.LastShot = (float)gameTime.TotalGameTime.TotalMilliseconds + this.ShootRate;

                    // get the first dead bullet
                    Bullet b = null;
                    for (int i = 0; i < Bullets.Count; i++)
                    {
                        if (!Bullets[i].Alive)
                        {
                            b = Bullets[i];
                            break;
                        }
                    }

                    if (b != null)
                    {
                        Random rnd        = new Random();
                        int    YVariation = 4;

                        b.Reset();
                        b.Revive();

                        b.ShotAtMilliseconds = gameTime.TotalGameTime.TotalMilliseconds;

                        b.Body.X = Body.X + (FacingDirection > 0 ? 24 : -2);
                        b.Body.Y = this.Body.Y + rnd.Next(-YVariation, YVariation) + 10;                     //TODO: fix 16 offset with final sprites

                        b.Body.Velocity.X = (ShootingVelocity + (rnd.Next(-2, 2) * 0.1f)) * FacingDirection; // some variation to the speed
                        b.Body.Velocity.Y = (rnd.Next(-3, -1) * 0.01f);                                      // make it float a bit

                        // subtract bullet cost to energy
                        Energy -= BulletCost;
                        FireKnockBack(this);
                        Karma.AddShotFired();

                        float pitch = rnd.Next(-100, 10) * 0.01f;

                        SoundEffect sfx;
                        State.SFX.TryGetValue("bubble", out sfx);
                        sfx?.Play(1f, pitch, 0f);
                    }
                }
            }

            foreach (Bullet b in Bullets)
            {
                b.Update(gameTime);
            }
        }