// public methods
        public void Update()
        {
            if (random.NextDouble() > runningGenerationRate)
            {
                runningGenerationRate += 0.0002;
                return;
            }

            this.runningGenerationRate = this.GenerationRate;

            float w = this.scene.Width;
            float h = this.scene.Height;
            int   x = random.Next((int)(w * 0.1), (int)(w * 0.9));
            int   y = random.Next((int)(h * 0.1), (int)(h * 0.9));

            BombEntity bomb = new BombEntity(null, x, y);

            this.scene.AddEntity(bomb);
        }
示例#2
0
        private void Update_Play()
        {
            float halfAlienSize = alienSize / 2;

            // update alien
            var alien = NamedEntities["alien"];

            alien.ConstrainInRect(0, 0, this.Width, this.Height);

            // update bullet entities
            foreach (Entity e in this.Entities)
            {
                // update speed for homing bullet
                if (e.ObjectType == Program.GameObjectType.HomingBullet &&
                    (long)e.Tag > Program.CurrentTime)
                {
                    float  sp = e.Motion.Speed.Norm();
                    Vector u  = Alien.Position - e.Position;
                    u = u.SafeNormalize();
                    Vector v = e.Motion.Speed.SafeNormalize();
                    e.Motion.Speed = (u * attractorFactor + v * (1 - attractorFactor)).SafeNormalize() * sp;
                }

                // update bullets
                if (e.ObjectType == Program.GameObjectType.Bullet ||
                    e.ObjectType == Program.GameObjectType.HomingBullet)
                {
                    SpriteEntity se = e as SpriteEntity;

                    // remove off screen bullets
                    if (!se.IsInRect(-50, -50, this.Width + 100, this.Height + 100))
                    {
                        se.CanRemove = true;
                        continue;
                    }

                    float dis = se.Position.DistanceFrom(this.Alien.Position);
                    if (dis < halfAlienSize)
                    {
                        // goto Failed state
                        state = State.Failed;
                    }
                    else if (se.State == (int)Program.BulletState.Normal && dis < halfAlienSize + this.closeDistance)
                    {
                        // increase score if bullet is close enough
                        score++;
                        se.State = (int)Program.BulletState.Approached;

                        /*
                         *                      if (e.ObjectType == Program.GameObjectType.Bullet)
                         *                              se.Bitmaps = this.NormalBulletGenerator.ApproachedSprites;
                         *                      if (e.ObjectType == Program.GameObjectType.HomingBullet)
                         *                              se.Bitmaps = this.HomingBulletGenerator.ApproachedSprites;
                         */
                    }
                }

                if (e.ObjectType == Program.GameObjectType.Laser)
                {
                    LaserEntity laser = e as LaserEntity;
                    float       dis   = laser.DistanceTo(this.Alien.Position);

                    if (dis < halfAlienSize)
                    {
                        // goto Failed state
                        state = State.Failed;
                    }
                    else if (dis < halfAlienSize + this.closeDistance)
                    {
                        // increase score if bullet is close enough
                        score++;
                        var p1 = new System.Windows.Media.MediaPlayer();
                        p1.Open(new System.Uri(AppDomain.CurrentDomain.BaseDirectory + "wav/click.wav"));
                        p1.Play();
                    }
                }

                if (e.ObjectType == Program.GameObjectType.Bomb)
                {
                    BombEntity laser = e as BombEntity;
                    float      dis   = laser.DistanceTo(this.Alien.Position);

                    if (dis < halfAlienSize)
                    {
                        // goto Failed state
                        state = State.Failed;
                    }
                    else if (dis < halfAlienSize + this.closeDistance)
                    {
                        // increase score if bullet is close enough
                        score++;
                    }
                }
            }

            // make new bullet randomly
            this.NormalBulletGenerator.Update();
            this.HomingBulletGenerator.Update();
            this.LaserGenerator.Update();
            this.BombGenerator.Update();

            // update time and score text entitye
            int        remainingTime = this.levelCountdown - (int)(Program.CurrentTime - this.PlayStartTime) / 1000;
            TextEntity cdt           = this.NamedEntities["countdown_text"] as TextEntity;

            cdt.Text = remainingTime.ToString();

            TextEntity st = this.NamedEntities["score_text"] as TextEntity;

            st.Text = this.score.ToString("0000");

            // if time up, goto intermission scene
            if (remainingTime == 0)
            {
                // goto Completed state
                state = State.Completed;
            }
        }