Example #1
0
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            map.Update(gameTime);
            player.Update(gameTime);

            if (((Player)player).Primary)
            {
                PlayerShot ps = new PlayerShot(Game, GraphicsDeviceManager);
                ps.Initialize();
                ps.LoadContent(SpriteBatch);
                ps.Position = player.Position + new Vector2(
                    6, -8
                    );
                playershots.AddLast(ps);
                bgmtoken = 0;
            }


            if (bgmtoken > 500)
            {
                float bgm1vol = bgm1.Volume;
                float bgm2vol = bgm2.Volume;

                bgm1vol += 0.01f;
                bgm2vol -= 0.01f;

                if (bgm1vol > 1f || bgm2vol < 0f)
                {
                    bgm1vol = 1f;
                    bgm2vol = 0f;

                    bgmtoken = -1;
                }

                bgm1.Volume = bgm1vol;
                bgm2.Volume = bgm2vol;
            }
            else if (bgmtoken >= 0)
            {
                bgmtoken += gameTime.ElapsedGameTime.Milliseconds;
                float bgm1vol = bgm1.Volume;
                float bgm2vol = bgm2.Volume;

                bgm1vol -= 0.01f;
                bgm2vol += 0.01f;

                if (bgm1vol < 0f || bgm2vol > 1f)
                {
                    bgm1vol = 0f;
                    bgm2vol = 1f;
                }

                bgm1.Volume = bgm1vol;
                bgm2.Volume = bgm2vol;
            }

            foreach (GameObject go in enemies)
            {
                go.Update(gameTime);
            }

            foreach (GameObject go in playershots)
            {
                go.Update(gameTime);
            }

            /**
             * COLLISION
             */
            // OUT OF BORDER
            Vector2 pos = player.Position;

            if (pos.X < 0)
            {
                pos.X = 0;
            }
            else if (pos.X + player.Size.X >= Size.WIDTH)
            {
                pos.X = Size.WIDTH - player.Size.X;
            }

            if (pos.Y < 0)
            {
                pos.Y = 0;
            }
            else if (pos.Y + player.Size.Y >= Size.HEIGHT)
            {
                pos.Y = Size.HEIGHT - player.Size.Y;
            }

            // SHOTS
            if (enemies.Count > 0)
            {
                LinkedList <GameObject> shots = new LinkedList <GameObject>();
                LinkedList <GameObject> mobs  = new LinkedList <GameObject>();
                foreach (GameObject shot in playershots)
                {
                    if (shot.Position.Y < 0)
                    {
                        shots.AddLast(shot);
                        continue;
                    }
                    foreach (GameObject enemy in enemies)
                    {
                        if (shot.collide(enemy) ||
                            enemy.collide(shot))
                        {
                            --((Mob)enemy).Hp;
                            if (((Mob)enemy).Hp <= 0)
                            {
                                shots.AddLast(shot);
                            }
                        }
                    }
                }
                foreach (GameObject enemy in enemies)
                {
                    if (enemy.Position.Y > Settings.Size.HEIGHT)
                    {
                        mobs.AddLast(enemy);
                        continue;
                    }
                }
                foreach (GameObject shot in shots)
                {
                    playershots.Remove(shot);
                }
                foreach (GameObject mob in mobs)
                {
                    enemies.Remove(mob);
                }
                shots = null;
                mobs  = null;
            }

            foreach (GameObject shot in enemiesshots)
            {
                if (shot.collide(player) ||
                    player.collide(shot))
                {
                    // APPLY DAMAGE
                }
            }

            /**
             * UPDATE
             */
            player.Position = pos;
        }