// 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   side = random.Next(4);
            int   pos1 = random.Next(10, 90);
            int   pos2 = random.Next(10, 90);

            LaserEntity laser = null;

            switch (side)
            {
            case 0:                     // top
                laser = new LaserEntity(null, pos1 * w / 100.0f, 0, pos2 * w / 100.0f, h);
                break;

            case 1:                     // bottom
                laser = new LaserEntity(null, pos1 * w / 100.0f, h, pos2 * w / 100.0f, 0);
                break;

            case 2:                     // left
                laser = new LaserEntity(null, 0, pos1 * h / 100.0f, w, pos2 * h / 100.0f);
                break;

            case 3:                     // right
                laser = new LaserEntity(null, w, pos1 * h / 100.0f, 0, pos2 * h / 100.0f);
                break;
            }

            this.scene.AddEntity(laser);
        }
Esempio n. 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;
            }
        }