Ejemplo n.º 1
0
        public override void LoadContent()
        {
            base.LoadContent();
            MediaPlayer.Stop();

            camera      = new Camera2D(Vector2.Zero);
            camera.Zoom = (float)Graphics.PreferredBackBufferHeight * 2.45f / 600f;   // the ideal zoom is 2.45 at 600px of screen height

            font           = content.Load <SpriteFont>("Font");
            tilemapTexture = this.content.Load <Texture2D>("spritesheet-jn");

            /*
             * Player Health & Energy
             */
            texturePlayerHealth = this.content.Load <Texture2D>("HeartDraft");
            texturePlayerEnergy = this.content.Load <Texture2D>("EnergyBar");

            /*
             * A single pixel to draw lines and stuff
             */
            pixel = new Texture2D(Graphics.GraphicsDevice, 1, 1);
            DrawMe.Fill(pixel, Color.White);

            /*
             * Load sfx
             */
            SFX.Add("bubble", content.Load <SoundEffect>("sfx_bubble"));
            SFX.Add("bubble_noise_single", content.Load <SoundEffect>("BubbleNoisesSingle"));
            SFX.Add("anchor", content.Load <SoundEffect>("Anchor3"));
            SFX.Add("fall", content.Load <SoundEffect>("Falling"));
            SFX.Add("enemyDeath", content.Load <SoundEffect>("EnemyDeath"));

            /*
             * Player init
             */
            player = new Player(this, tilemapTexture, Vector2.Zero, 32, 32, true);
            player.Animations.CurrentFrame = new Frame(96, 176, 16, 32); // woman
            player.Animations.CurrentFrame = new Frame(0, 144, 32, 32);  // actual player
            //player.Animations.Add("robot-idle", new int[] { 177, 178, 179, 180, 181, 182 }, 6, false, true);
            //player.Animations.Add("woman-run", new int[] { 183, 184, 185, 186, 187, 188 }, 12, true);
            player.Body.X = 16 * 7;
            player.Body.Y = 16 * 5;
            player.Body.SetSize(16, 32, 0, 0);  // woman
            player.Body.SetSize(10, 26, 11, 3); // actual player
            Karma.maxKarma          = 0;
            Karma.playerShotsFired  = 0;
            Karma.playerTotalDamage = 0f;
            Karma.playerCollect     = 0;

            //player.Animations.Play("woman-run");

            /*
             * Level init
             */
            XMLLevelLoader XMLloader = new XMLLevelLoader();

            level = XMLloader.LoadLevel(this, @"Content\featureTestMap.tmx", tilemapTexture);
            level.SetCollisionTiles(new int[] { 1, 2, 17, 18, 33, 34 });


            /*
             * parse objects
             */
            foreach (TiledObject obj in level.Objects)
            {
                Console.WriteLine("parsing " + obj.Name);

                if (obj.Name.ToLower() == "jellyfish")
                {
                    Vector2 center = new Vector2(obj.X + obj.Width / 2, obj.Y + obj.Height / 2);
                    Vector2 radius = new Vector2(obj.Width / 2, obj.Height / 2);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    JellyFish j = new JellyFish(this, tilemapTexture, Vector2.Zero, 16, 32, center, radius, speed);
                    j.Animations.CurrentFrame = new Frame(48, 112, 16, 32);

                    enemies.Add(j);
                    Karma.maxKarma = Karma.DefineMaxKarma(Karma.maxKarma, j.Health);

                    Console.WriteLine("added jelly");
                }
                else if (obj.Name.ToLower() == "pufferfish")
                {
                    Vector2 position = new Vector2(obj.X, obj.Y);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    PufferFish p = new PufferFish(this, tilemapTexture, position, 32, 32, obj.Width, speed);
                    p.Animations.CurrentFrame = new Frame(0, 112, 32, 32);

                    enemies.Add(p);
                    Karma.maxKarma = Karma.DefineMaxKarma(Karma.maxKarma, p.Health);

                    Console.WriteLine("added puffer");
                }
                else if (obj.Name.ToLower() == "turtlex")
                {
                    Vector2 position = new Vector2(obj.X, obj.Y);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    TurtleX p = new TurtleX(this, tilemapTexture, position, 32, 32, 64, obj.Width, speed);
                    p.Animations.CurrentFrame = new Frame(96, 112, 32, 32);

                    enemies.Add(p);
                    Karma.maxKarma = Karma.DefineMaxKarma(Karma.maxKarma, p.Health);

                    Console.WriteLine("added turtlex");
                }
                else if (obj.Name.ToLower() == "goldfish")
                {
                    goldenFishs.Add(new GoldFish(this, tilemapTexture, new Vector2(obj.X, obj.Y), 16, 16));
                }
                else if (obj.Name.ToLower() == "particles")
                {
                    ParticleEmitter particleEmitter = new ParticleEmitter(this, obj.X, obj.Y, 256);
                    particleEmitter.EmitterBox.Resize(obj.Width, obj.Height);
                    particleEmitter.MakeParticles(tilemapTexture, 16, 16);
                    particleEmitter.ParticleVelocity = new Vector2(0, -0.01f);
                    particleEmitter.SetAcceleration(0, -0.005f);
                    particleEmitter.XVelocityVariationRange = new Vector2(-20f, 20f);
                    particleEmitter.YVelocityVariationRange = new Vector2(-20f, 0f);
                    particleEmitter.SetTextureCropRectangle(new Rectangle(3 * 16, 6 * 16, 16, 16));
                    particleEmitter.SpawnRate = 250f;
                    particleEmitter.ParticleLifespanMilliseconds          = 5000f;
                    particleEmitter.ParticleLifespanVariationMilliseconds = 1000f;
                    particleEmitter.InitialScale = 0.1f;
                    particleEmitter.FinalScale   = 1.5f;

                    particleEmitter.ForEachParticle(ChangeSpriteTintBlue);

                    backgroundParticles.Add(particleEmitter);



                    Console.WriteLine("added particles");
                }
                else if (obj.Name.ToLower() == "player_spawn")
                {
                    player.Body.X = obj.X;
                    player.Body.Y = obj.Y;
                }
                else if (obj.Name.ToLower() == "change_state_trigger")
                {
                    triggers.Add(new Trigger(obj.X, obj.Y, obj.Width, obj.Height, obj.GetProperty("value")));
                }
            }

            // build spikes tiles list
            spikesPointingDown = level.GetTilesListByID(new int[] { 97 });
            spikesPointingUp   = level.GetTilesListByID(new int[] { 98 });

            foreach (Tile spike in spikesPointingDown)
            {
                spike.Body.SetSize(12, 6, 2, 0);
            }
            foreach (Tile spike in spikesPointingUp)
            {
                spike.Body.SetSize(12, 6, 2, 10);
            }

            topWaterTiles = level.GetTilesListByID(new int[] { 49, 50, 51 });

            /*
             * UI Elements init
             */
            energyBar   = new EnergyBar(Graphics, new Vector2(16, 32 + 4), 64, 8, new Color(255, 0, 0));
            Karma.karma = Karma.maxKarma;

            /*
             * Build Background Gradient
             */
            backgroundWaterGradientStrip = new Texture2D(Graphics.GraphicsDevice, 1, level.Height * level.TileHeight);

            Color startColor  = new Color(57, 92, 181);
            Color finishColor = new Color(17, 43, 104);
            Color currentColor;

            for (int i = 0; i < backgroundWaterGradientStrip.Height; i++)
            {
                float ratio = Math2.Map(i, 0f, backgroundWaterGradientStrip.Height, 0f, 1.0f);

                currentColor = Color.Lerp(startColor, finishColor, ratio);
                DrawMe.Pixel(backgroundWaterGradientStrip, 0, i, currentColor);
            }

            /*
             * Build Background Gradient
             */
            backgroundSkyGradientStrip = new Texture2D(Graphics.GraphicsDevice, 1, Graphics.PreferredBackBufferHeight / 2);

            startColor  = new Color(61, 28, 111);
            finishColor = new Color(158, 98, 123);
            for (int i = 0; i < backgroundSkyGradientStrip.Height; i++)
            {
                float ratio = Math2.Map(i, 0f, backgroundSkyGradientStrip.Height, 0f, 1.0f);

                currentColor = Color.Lerp(startColor, finishColor, ratio);
                DrawMe.Pixel(backgroundSkyGradientStrip, 0, i, currentColor);
            }

            ContentLoaded = true;
        }
Ejemplo n.º 2
0
        public override void Update(GameTime gameTime)
        {
            Console.WriteLine(gameTime.ElapsedGameTime.TotalSeconds);

            /*
             * Input State refresh
             */
            #region [System Status Refresh]

            KeyboardState kState = Keyboard.GetState();
            MouseState    mState = Mouse.GetState();

            #endregion

            /*
             * First of all, we update the player
             * and other sprites velocity vectors
             */
            #region [Update Sprites Velocity and Position]

            player.UpdateMotion(gameTime, kState);

            foreach (Sprite s in enemies)
            {
                if (s.Body.Tag == "turtlex")
                {
                    TurtleX t             = (TurtleX)s;
                    float   inflictDamage = t.UpdateMov(gameTime, player);

                    if (inflictDamage > 0f)
                    {
                        TriggerPlayerHurt(gameTime, t, inflictDamage);
                        t.Kill();
                    }

                    if (inflictDamage == -1f)
                    {
                        t.Kill();
                        camera.ActivateShake(gameTime, 400f, 6, 0.01f, true, -0.02f);
                    }

                    continue;
                }

                s.Update(gameTime);

                if (s.Alive)
                {
                    if (Physics.Overlap(s, player) && !player.IsBlinking)  // when blinking, take no damage
                    {
                        TriggerPlayerHurt(gameTime, s);
                    }
                }
            }

            #endregion

            /*
             * Because the hazards may cause velocity changes,
             * we overlap the player with them, BEFORE checking
             * for collisions with the world.
             */
            #region [Hazards Overlap]

            if (!player.IsBlinking)
            {
                foreach (Tile spike in spikesPointingDown)
                {
                    if (Physics.Overlap(spike, player))
                    {
                        TriggerPlayerHurt(gameTime, spike);
                        break;  // breaking at the first overlap
                                // avoids various knockback forces
                                // being applied.
                                // this solved the issue (as far as I tested)
                                // of glitching through the solid tiles
                    }
                }
                foreach (Tile spike in spikesPointingUp)
                {
                    if (Physics.Overlap(spike, player))
                    {
                        TriggerPlayerHurt(gameTime, spike);
                        break;
                    }
                }
            }

            /*
             * Player Projectiles
             */
            player.UpdateProjectiles(gameTime, kState);

            foreach (Bullet b in player.Bullets)
            {
                if (b.Alive)
                {
                    foreach (Tile t in level.CollidableTiles)
                    {
                        if (Physics.Overlap(b, t))
                        {
                            b.Kill();
                        }
                    }
                    foreach (Sprite s in enemies)
                    {
                        if (s.Alive)
                        {
                            if (Physics.Overlap(b, s))
                            {
                                b.Kill();
                                s.ReceiveDamage(b.Damage);
                                s.StartBlinking(gameTime);

                                if (!s.Alive)
                                {
                                    camera.ActivateShake(gameTime, 150, 6, 0.015f, true, -0.01f);
                                }
                            }
                        }
                    }
                }
            }

            /*
             * Enemies Projectiles
             */
            foreach (Sprite p in enemies)
            {
                if (p.Alive)
                {
                    if (p.GetType() == typeof(PufferFish))
                    {
                        PufferFish puf = (PufferFish)p;

                        foreach (Bullet b in puf.Bullets)
                        {
                            if (b.Alive)
                            {
                                foreach (Tile t in level.CollidableTiles)
                                {
                                    if (Physics.Overlap(b, t))
                                    {
                                        b.Kill();
                                    }
                                }

                                if (player.Alive)
                                {
                                    if (Physics.Overlap(b, player) && !player.IsBlinking)
                                    {
                                        TriggerPlayerHurt(gameTime, b);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            #endregion

            /*
             * Next up, we have the world collisions
             * and resolution.
             */
            #region [World Collisions]

            bool cameraShakeResponse = player.UpdateCollisions(gameTime, level);

            RepositionOutOfBoundsPlayer(gameTime);

            #endregion

            /*
             * Particle Emitters, background assets, UI, etc..
             */
            #region [ Secondary Assets ]

            // water waves
            float count = 1f;
            foreach (Tile t in topWaterTiles)
            {
                float x          = (float)gameTime.TotalGameTime.TotalMilliseconds;
                float phaseShift = (count / topWaterTiles.Count) * (float)Math.PI * 19.7f;
                float freq       = 0.005f;
                float amp        = 1f;

                // low freq motion, sin wave 1
                t.Body.Y = Math2.SinWave((x * freq - phaseShift), amp);

                count++;
            }

            foreach (ParticleEmitter p in backgroundParticles)
            {
                p.Update(gameTime);
                p.ForEachParticle(KillOutOfBoundsParticle);
            }

            foreach (GoldFish g in goldenFishs)
            {
                g.Update(gameTime);

                if (Physics.Overlap(g, player))
                {
                    g.Kill();
                }
            }

            energyBar.SetPercent((int)(player.Energy * 100f / player.MaxEnergy));
            healthBar.SetPercent((int)(player.Health * 100f / player.MaxHealth));

            if (cameraShakeResponse && !camera.Shaking)
            {
                //camera.ActivateShake(gameTime, 250f, 4f, 0.05f, true, 0.01f); // static
                camera.ActivateShake(gameTime, 250f, Math.Abs(player.Body.DeltaY()) * 3, 0.05f, true, 0.01f); // based on delta
                //Console.WriteLine(Math.Abs(player.Body.DeltaY()));
            }

            camera.Position = new Vector2(player.Body.Position.X + player.Body.Bounds.HalfWidth, player.Body.Position.Y + player.Body.Bounds.HalfHeight);

            // clamp camera position
            float halfscreenwidth  = Graphics.PreferredBackBufferWidth / 2f;
            float halfscreenheight = Graphics.PreferredBackBufferHeight / 2f;
            camera.Position.X = MathHelper.Clamp(camera.Position.X, halfscreenwidth / camera.Zoom, (level.Width * level.TileWidth) - (halfscreenwidth / camera.Zoom));
            camera.Position.Y = MathHelper.Clamp(camera.Position.Y, -1000f, (level.Height * level.TileHeight) - (halfscreenheight / camera.Zoom));

            camera.Update(gameTime, Graphics.GraphicsDevice);

            #endregion

            /*
             * Game resolution
             */
            #region [ Game Resolution and other checks ]

            if (!player.Alive)
            {
                // show end game screen
                // now we will just restart this state
                StateManager.Instance.StartGameState("Level1State");
                return;
            }

            foreach (Trigger t in triggers)
            {
                if (Physics.Overlap(player.Body.Bounds, t))
                {
                    StateManager.Instance.StartGameState(t.Value);
                    return;
                }
            }

            #endregion
        }
Ejemplo n.º 3
0
        public override void LoadContent()
        {
            base.LoadContent();

            camera      = new Camera2D(Vector2.Zero);
            camera.Zoom = 1.5f;

            font           = content.Load <SpriteFont>("Font");
            tilemapTexture = this.content.Load <Texture2D>("SpriteSheet");

            MediaPlayer.Volume = 0.3f;
            MediaPlayer.Play(Globals.MenuSong);
            MediaPlayer.IsRepeating = true;

            diveButton = new Trigger(Graphics.PreferredBackBufferWidth / 2 - 128 / 2, Graphics.PreferredBackBufferHeight - 48 - 32, 128f, 48f, "dive");  //Graphics.PreferredBackBufferWidth / 2 - 128 / 2, Graphics.PreferredBackBufferHeight - 48 - 32, 128, 48

            /*
             * Level init
             */
            XMLLevelLoader XMLloader = new XMLLevelLoader();

            // se o load do mapa falhar, well shit. vai para o menu.
            try
            {
                level = XMLloader.LoadLevel(this, @"Content\Menu.tmx", tilemapTexture);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error Loading Map. Error message: " + e.Message);

                StateManager.Instance.StartGameState("MenuState");
            }

            level.SetCollisionTiles(new int[] { 2, 33, 34, 35, 47, 66, 15, 79 });

            /*
             * parse objects
             */
            foreach (TiledObject obj in level.Objects)
            {
                Console.WriteLine("parsing " + obj.Name);

                if (obj.Name.ToLower() == "jellyfish")
                {
                    Vector2 center = new Vector2(obj.X + obj.Width / 2, obj.Y + obj.Height / 2);
                    Vector2 radius = new Vector2(obj.Width / 2, obj.Height / 2);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    JellyFish j = new JellyFish(this, tilemapTexture, Vector2.Zero, 16, 32, center, radius, speed);

                    Karma.maxKarma += j.Health;

                    // make it start on the right side of its path
                    if (obj.GetProperty("start_rotation") == "right")
                    {
                        j.FacingDirection = 1;
                    }
                    else
                    {
                        j.FacingDirection = -1;
                    }

                    Console.WriteLine(obj.GetProperty("start_rotation"));

                    enemies.Add(j);
                }
                else if (obj.Name.ToLower() == "pufferfish")
                {
                    Vector2 position = new Vector2(obj.X, obj.Y);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    PufferFish p = new PufferFish(this, tilemapTexture, position, 32, 32, obj.Width, speed);

                    for (int i = 0; i < p.Bullets.Count; i++)
                    {
                        p.Bullets[i].Alive   = true;
                        p.Bullets[i].Visible = false;
                    }

                    Karma.maxKarma += p.Health;

                    // make it start on the right side of its path
                    if (obj.GetProperty("start_side") == "right")
                    {
                        p.Body.X          = obj.X + obj.Width;
                        p.CurrentDistance = obj.Width - 33;
                    }

                    enemies.Add(p);
                }
                else if (obj.Name.ToLower() == "turtlex")
                {
                    Vector2 position = new Vector2(obj.X, obj.Y);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    TurtleX p = new TurtleX(this, tilemapTexture, position, 32, 32, 64, obj.Width, speed);
                    p.Animations.CurrentFrame = new Frame(96, 112, 32, 32);

                    Karma.maxKarma += p.Health;

                    // make it start on the right side of its path
                    if (obj.GetProperty("start_side") == "right")
                    {
                        p.Body.X          = obj.X + obj.Width;
                        p.CurrentDistance = obj.Width - 33;
                    }

                    enemies.Add(p);
                }
                else if (obj.Name.ToLower() == "goldfish")
                {
                    goldenFishs.Add(new GoldFish(this, tilemapTexture, new Vector2(obj.X, obj.Y), 16, 16));
                }
                else if (obj.Name.ToLower() == "particles")
                {
                    if (obj.GetProperty("type") == "dark_ambient")
                    {
                        ParticleEmitter particleEmitter = new ParticleEmitter(this, obj.X, obj.Y, 256);
                        particleEmitter.EmitterBox.Resize(obj.Width, obj.Height);

                        particleEmitter.MakeRandomParticles(tilemapTexture, new Rectangle[]
                        {
                            new Rectangle(128, 257, 3, 3),
                            new Rectangle(132, 257, 3, 3),
                            new Rectangle(136, 257, 3, 3)
                        });

                        particleEmitter.ParticleVelocity = new Vector2(0, 0);
                        particleEmitter.SetAcceleration(0, 0);
                        particleEmitter.XVelocityVariationRange = new Vector2(-20f, 20f);
                        particleEmitter.YVelocityVariationRange = new Vector2(-20f, 20f);
                        particleEmitter.SpawnRate = 100f;
                        particleEmitter.ParticleLifespanMilliseconds          = 5000f;
                        particleEmitter.ParticleLifespanVariationMilliseconds = 1000f;
                        particleEmitter.InitialScale = 1.5f;
                        particleEmitter.FinalScale   = 0.5f;

                        backgroundParticles.Add(particleEmitter);
                    }
                    else
                    {
                        ParticleEmitter particleEmitter = new ParticleEmitter(this, obj.X, obj.Y, 256);
                        particleEmitter.EmitterBox.Resize(obj.Width, obj.Height);
                        particleEmitter.MakeParticles(tilemapTexture, 16, 16);
                        particleEmitter.ParticleVelocity = new Vector2(0, -0.01f);
                        particleEmitter.SetAcceleration(0, -0.005f);
                        particleEmitter.XVelocityVariationRange = new Vector2(-20f, 20f);
                        particleEmitter.YVelocityVariationRange = new Vector2(-20f, 0f);
                        particleEmitter.SetTextureCropRectangle(new Rectangle(0, 78, 16, 16));
                        particleEmitter.SpawnRate = 250f;
                        particleEmitter.ParticleLifespanMilliseconds          = 5000f;
                        particleEmitter.ParticleLifespanVariationMilliseconds = 1000f;
                        particleEmitter.InitialScale = 0.1f;
                        particleEmitter.FinalScale   = 1.5f;

                        particleEmitter.ForEachParticle(ChangeSpriteTintBlue);

                        backgroundParticles.Add(particleEmitter);
                    }
                }
                else if (obj.Name.ToLower() == "player_spawn")
                {
                    //player.Body.X = obj.X;
                    //player.Body.Y = obj.Y;
                }
                else if (obj.Name.ToLower() == "change_state_trigger")
                {
                    triggers.Add(new Trigger(obj.X, obj.Y, obj.Width, obj.Height, obj.GetProperty("value")));
                }
                else if (obj.Name.ToLower() == "speedbox")
                {
                    SpeedBox s = new SpeedBox(obj.X, obj.Y, obj.Width, obj.Height, float.Parse(obj.GetProperty("speedX")), float.Parse(obj.GetProperty("speedY")));

                    ParticleEmitter particleEmitter = new ParticleEmitter(this, obj.X, obj.Y, 512);
                    particleEmitter.EmitterBox.Resize(obj.Width, obj.Height);
                    particleEmitter.MakeRandomParticles(tilemapTexture, new Rectangle[]
                    {
                        new Rectangle(128, 257, 3, 3),
                        new Rectangle(132, 257, 3, 3),
                        new Rectangle(136, 257, 3, 3),
                        new Rectangle(128 - 16, 257, 3, 3),
                        new Rectangle(132 - 16, 257, 3, 3),
                        new Rectangle(136 - 16, 257, 3, 3)
                    });
                    particleEmitter.ParticleVelocity        = new Vector2(s.SpeedIncrease.X * 10, s.SpeedIncrease.Y * 10);
                    particleEmitter.XVelocityVariationRange = new Vector2(-20f, 20f);
                    particleEmitter.YVelocityVariationRange = new Vector2(-20f, 20f);

                    particleEmitter.SpawnRate = 60f;
                    particleEmitter.ParticleLifespanMilliseconds          = 5000f;
                    particleEmitter.ParticleLifespanVariationMilliseconds = 1000f;
                    particleEmitter.InitialScale = 1.0f;
                    particleEmitter.FinalScale   = 0.5f;


                    backgroundParticles.Add(particleEmitter);
                    speedBoxes.Add(s);
                }

                Console.WriteLine("added " + obj.Name);
            }

            topWaterTiles = level.GetTilesListByID(new int[] { 97, 98, 99 });

            /*
             * Build Background Gradient
             */
            backgroundWaterGradientStrip = new Texture2D(Graphics.GraphicsDevice, 1, level.Height * level.TileHeight);

            Color startColor  = new Color(57, 92, 181);
            Color finishColor = new Color(17, 43, 104);
            Color currentColor;

            for (int i = 0; i < backgroundWaterGradientStrip.Height; i++)
            {
                float ratio = Math2.Map(i, 0f, backgroundWaterGradientStrip.Height, 0f, 1.0f);

                currentColor = Color.Lerp(startColor, finishColor, ratio);
                DrawMe.Pixel(backgroundWaterGradientStrip, 0, i, currentColor);
            }

            /*
             * Build Background Gradient
             */
            backgroundSkyGradientStrip = new Texture2D(Graphics.GraphicsDevice, 1, Graphics.PreferredBackBufferHeight / 2);

            startColor  = new Color(61, 28, 111);
            finishColor = new Color(158, 98, 123);
            for (int i = 0; i < backgroundSkyGradientStrip.Height; i++)
            {
                float ratio = Math2.Map(i, 0f, backgroundSkyGradientStrip.Height, 0f, 1.0f);

                currentColor = Color.Lerp(startColor, finishColor, ratio);
                DrawMe.Pixel(backgroundSkyGradientStrip, 0, i, currentColor);
            }

            ContentLoaded = true;
        }
Ejemplo n.º 4
0
        public override void Update(GameTime gameTime)
        {
            /*
             * Input State refresh
             */
            KeyboardState kState = Keyboard.GetState();
            MouseState    mState = Mouse.GetState();

            if (kState.IsKeyDown(Keys.Q))
            {
                player.StartBlinking(gameTime);
            }

            /*
             * Player Update
             */
            player.UpdateMotion(gameTime, kState);

            /*
             * AI (lol) Update
             */
            foreach (Sprite s in enemies)
            {
                if (s.Alive)
                {
                    if (s.GetType() == typeof(TurtleX))
                    {
                        TurtleX t = (TurtleX)s;
                        t.UpdateMov(gameTime, player);
                    }
                    else
                    {
                        s.Update(gameTime);
                    }

                    if (Physics.Overlap(s, player) && !player.IsBlinking)      // when blinking, take no damage
                    {
                        player.ReceiveDamage(s.Damage);
                        player.StartBlinking(gameTime);
                    }
                }
            }
            foreach (Sprite s in pufferFish)
            {
                if (s.Alive)
                {
                    s.Update(gameTime);

                    if (Physics.Overlap(s, player) && !player.IsBlinking)  // when blinking, take no damage
                    {
                        player.ReceiveDamage(s.Damage);
                        player.StartBlinking(gameTime);
                    }
                }
            }

            foreach (Tile spike in spikesPointingDown)
            {
                if (Physics.Overlap(spike, player) && !player.IsBlinking)  // when blinking, take no damage
                {
                    player.ReceiveDamage(10);
                    player.StartBlinking(gameTime);
                }
            }
            foreach (Tile spike in spikesPointingUp)
            {
                if (Physics.Overlap(spike, player) && !player.IsBlinking)  // when blinking, take no damage
                {
                    player.ReceiveDamage(10);
                    player.StartBlinking(gameTime);
                }
            }


            /*
             * Projectiles
             */
            foreach (Bullet b in player.Bullets)
            {
                if (b.Alive)
                {
                    foreach (Tile t in level.CollidableTiles)
                    {
                        if (Physics.Overlap(b, t))
                        {
                            b.Kill();
                        }
                    }
                    foreach (Sprite s in pufferFish)
                    {
                        if (s.Alive)
                        {
                            if (Physics.Overlap(b, s))
                            {
                                b.Kill();
                                s.ReceiveDamage(b.Damage);
                                s.StartBlinking(gameTime);

                                // we should remove dead actors from the list
                            }
                        }
                    }

                    foreach (Sprite s in enemies)
                    {
                        if (s.Alive)
                        {
                            if (Physics.Overlap(b, s))
                            {
                                b.Kill();
                                s.ReceiveDamage(b.Damage);
                                s.StartBlinking(gameTime);

                                // we should remove dead actors from the list
                            }
                        }
                    }
                }
            }

            /*
             * Projectiles
             */
            foreach (PufferFish p in pufferFish)
            {
                foreach (Bullet b in p.Bullets)
                {
                    if (b.Alive)
                    {
                        foreach (Tile t in level.CollidableTiles)
                        {
                            if (Physics.Overlap(b, t))
                            {
                                b.Kill();
                            }
                        }

                        if (player.Alive)
                        {
                            if (Physics.Overlap(b, player) && !player.IsBlinking)
                            {
                                player.ReceiveDamage(10);
                                player.StartBlinking(gameTime);
                            }
                        }
                    }
                }
            }

            /*
             * UI Update
             */
            energyBar.SetPercent((int)(player.Energy * 100f / player.MaxEnergy));
            healthBar.SetPercent((int)(player.Health * 100f / player.MaxHealth));

            camera.Position = new Vector2(player.Body.Position.X, 7 * 16);
            camera.GetTransform(Graphics.GraphicsDevice);

            /*
             * End Condition
             */
            if (!player.Alive)
            {
                // show end game screen
                // now we will just restart this state
                StateManager.Instance.StartGameState("PlayState");
            }
        }
Ejemplo n.º 5
0
        public override void LoadContent()
        {
            base.LoadContent();

            camera      = new Camera2D(Vector2.Zero);
            camera.Zoom = (float)Graphics.PreferredBackBufferHeight * 2.45f / 600f;  // the ideal zoom is 2.45 at 600px of screen height

            font           = content.Load <SpriteFont>("Font");
            tilemapTexture = this.content.Load <Texture2D>("SpriteSheet");

            MediaPlayer.Volume = 0.3f;
            MediaPlayer.Play(Globals.LevelSong);
            MediaPlayer.IsRepeating = true;

            /*
             * A single pixel to draw lines and stuff
             */
            pixel = new Texture2D(Graphics.GraphicsDevice, 1, 1);
            DrawMe.Fill(pixel, Color.White);

            /*
             * Load sfx
             */
            SFX.Add("bubble", content.Load <SoundEffect>("sfx_bubble"));
            SFX.Add("bubble_noise_single", content.Load <SoundEffect>("BubbleNoisesSingle"));
            SFX.Add("anchor", content.Load <SoundEffect>("Anchor3"));
            SFX.Add("fall", content.Load <SoundEffect>("Falling"));
            SFX.Add("enemyDeath", content.Load <SoundEffect>("EnemyDeath"));
            SFX.Add("goldenFish", content.Load <SoundEffect>("GoldenFish2"));
            SFX.Add("energyWarning", content.Load <SoundEffect>("EnergyEmpty"));       //done
            SFX.Add("turtleExplosion", content.Load <SoundEffect>("TurtleExplosion")); //done
            SFX.Add("playerHurt", content.Load <SoundEffect>("Player hurt"));          //done
            SFX.Add("playerDeath", content.Load <SoundEffect>("Player Death3"));       //done faltar esperar uns segundos antes de fazer reload de state

            /*
             * Player init
             */
            player = new Player(this, tilemapTexture, Vector2.Zero, 32, 32, true);

            /*
             * Level init
             */
            XMLLevelLoader XMLloader = new XMLLevelLoader();

            // se o load do mapa falhar, well shit. vai para o menu.
            try
            {
                level = XMLloader.LoadLevel(this, @"Content\" + Globals.CurrentLevel + ".tmx", tilemapTexture);
            } catch (Exception e)
            {
                Console.WriteLine("Error Loading Map. Error message: " + e.Message);

                StateManager.Instance.StartGameState("MenuState");
            }

            level.SetCollisionTiles(new int[] { 2, 33, 34, 35, 47, 66, 15, 79 });


            /*
             * parse objects
             */
            foreach (TiledObject obj in level.Objects)
            {
                Console.WriteLine("parsing " + obj.Name);

                if (obj.Name.ToLower() == "jellyfish")
                {
                    Vector2 center = new Vector2(obj.X + obj.Width / 2, obj.Y + obj.Height / 2);
                    Vector2 radius = new Vector2(obj.Width / 2, obj.Height / 2);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    JellyFish j = new JellyFish(this, tilemapTexture, Vector2.Zero, 16, 32, center, radius, speed);

                    Karma.maxKarma += j.Health;

                    // make it start on the right side of its path
                    if (obj.GetProperty("start_rotation") == "right")
                    {
                        j.FacingDirection = 1;
                    }
                    else
                    {
                        j.FacingDirection = -1;
                    }

                    Console.WriteLine(obj.GetProperty("start_rotation"));

                    enemies.Add(j);
                }
                else if (obj.Name.ToLower() == "pufferfish")
                {
                    Vector2 position = new Vector2(obj.X, obj.Y);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    PufferFish p = new PufferFish(this, tilemapTexture, position, 32, 32, obj.Width, speed);

                    Karma.maxKarma += p.Health;

                    // make it start on the right side of its path
                    if (obj.GetProperty("start_side") == "right")
                    {
                        p.Body.X          = obj.X + obj.Width;
                        p.CurrentDistance = obj.Width - 33;
                    }

                    enemies.Add(p);
                }
                else if (obj.Name.ToLower() == "turtlex")
                {
                    Vector2 position = new Vector2(obj.X, obj.Y);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    TurtleX p = new TurtleX(this, tilemapTexture, position, 32, 32, 64, obj.Width, speed);
                    p.Animations.CurrentFrame = new Frame(96, 112, 32, 32);

                    Karma.maxKarma += p.Health;

                    // make it start on the right side of its path
                    if (obj.GetProperty("start_side") == "right")
                    {
                        p.Body.X          = obj.X + obj.Width;
                        p.CurrentDistance = obj.Width - 33;
                    }

                    enemies.Add(p);
                }
                else if (obj.Name.ToLower() == "goldfish")
                {
                    goldenFishs.Add(new GoldFish(this, tilemapTexture, new Vector2(obj.X, obj.Y), 16, 16));
                }
                else if (obj.Name.ToLower() == "particles")
                {
                    if (obj.GetProperty("type") == "dark_ambient")
                    {
                        ParticleEmitter particleEmitter = new ParticleEmitter(this, obj.X, obj.Y, 256);
                        particleEmitter.EmitterBox.Resize(obj.Width, obj.Height);

                        particleEmitter.MakeRandomParticles(tilemapTexture, new Rectangle[]
                        {
                            new Rectangle(128, 257, 3, 3),
                            new Rectangle(132, 257, 3, 3),
                            new Rectangle(136, 257, 3, 3)
                        });

                        particleEmitter.ParticleVelocity = new Vector2(0, 0);
                        particleEmitter.SetAcceleration(0, 0);
                        particleEmitter.XVelocityVariationRange = new Vector2(-20f, 20f);
                        particleEmitter.YVelocityVariationRange = new Vector2(-20f, 20f);
                        particleEmitter.SpawnRate = 100f;
                        particleEmitter.ParticleLifespanMilliseconds          = 5000f;
                        particleEmitter.ParticleLifespanVariationMilliseconds = 1000f;
                        particleEmitter.InitialScale = 1.5f;
                        particleEmitter.FinalScale   = 0.5f;

                        backgroundParticles.Add(particleEmitter);
                    }
                    else
                    {
                        ParticleEmitter particleEmitter = new ParticleEmitter(this, obj.X, obj.Y, 256);
                        particleEmitter.EmitterBox.Resize(obj.Width, obj.Height);
                        particleEmitter.MakeParticles(tilemapTexture, 16, 16);
                        particleEmitter.ParticleVelocity = new Vector2(0, -0.01f);
                        particleEmitter.SetAcceleration(0, -0.005f);
                        particleEmitter.XVelocityVariationRange = new Vector2(-20f, 20f);
                        particleEmitter.YVelocityVariationRange = new Vector2(-20f, 0f);
                        particleEmitter.SetTextureCropRectangle(new Rectangle(0, 78, 16, 16));
                        particleEmitter.SpawnRate = 250f;
                        particleEmitter.ParticleLifespanMilliseconds          = 5000f;
                        particleEmitter.ParticleLifespanVariationMilliseconds = 1000f;
                        particleEmitter.InitialScale = 0.1f;
                        particleEmitter.FinalScale   = 1.5f;

                        particleEmitter.ForEachParticle(ChangeSpriteTintBlue);

                        backgroundParticles.Add(particleEmitter);
                    }
                }
                else if (obj.Name.ToLower() == "player_spawn")
                {
                    player.Body.X = obj.X;
                    player.Body.Y = obj.Y;
                }
                else if (obj.Name.ToLower() == "change_state_trigger")
                {
                    triggers.Add(new Trigger(obj.X, obj.Y, obj.Width, obj.Height, obj.GetProperty("value")));
                }
                else if (obj.Name.ToLower() == "speedbox")
                {
                    SpeedBox s = new SpeedBox(obj.X, obj.Y, obj.Width, obj.Height, float.Parse(obj.GetProperty("speedX")), float.Parse(obj.GetProperty("speedY")));

                    ParticleEmitter particleEmitter = new ParticleEmitter(this, obj.X, obj.Y, 512);
                    particleEmitter.EmitterBox.Resize(obj.Width, obj.Height);
                    particleEmitter.MakeRandomParticles(tilemapTexture, new Rectangle[]
                    {
                        new Rectangle(128, 257, 3, 3),
                        new Rectangle(132, 257, 3, 3),
                        new Rectangle(136, 257, 3, 3),
                        new Rectangle(128 - 16, 257, 3, 3),
                        new Rectangle(132 - 16, 257, 3, 3),
                        new Rectangle(136 - 16, 257, 3, 3)
                    });
                    particleEmitter.ParticleVelocity        = new Vector2(s.SpeedIncrease.X * 10, s.SpeedIncrease.Y * 10);
                    particleEmitter.XVelocityVariationRange = new Vector2(-20f, 20f);
                    particleEmitter.YVelocityVariationRange = new Vector2(-20f, 20f);

                    particleEmitter.SpawnRate = 60f;
                    particleEmitter.ParticleLifespanMilliseconds          = 5000f;
                    particleEmitter.ParticleLifespanVariationMilliseconds = 1000f;
                    particleEmitter.InitialScale = 1.0f;
                    particleEmitter.FinalScale   = 0.5f;


                    backgroundParticles.Add(particleEmitter);
                    speedBoxes.Add(s);
                }

                Console.WriteLine("added " + obj.Name);
            }

            // build spikes tiles list
            spikesPointingDown  = level.GetTilesListByID(new int[] { 514 });
            spikesPointingUp    = level.GetTilesListByID(new int[] { 515 });
            spikesPointingLeft  = level.GetTilesListByID(new int[] { 516 });
            spikesPointingRight = level.GetTilesListByID(new int[] { 517 });

            foreach (Tile spike in spikesPointingDown)
            {
                spike.Body.SetSize(12, 6, 1, 0);
            }
            foreach (Tile spike in spikesPointingUp)
            {
                spike.Body.SetSize(12, 6, 2, 10);
            }

            foreach (Tile spike in spikesPointingLeft)
            {
                spike.Body.SetSize(6, 12, 10, 2);
            }

            foreach (Tile spike in spikesPointingRight)
            {
                spike.Body.SetSize(6, 12, 0, 2);
            }

            topWaterTiles = level.GetTilesListByID(new int[] { 97, 98, 99 });

            /*
             * UI Elements init
             */
            //healthBar = new EnergyBar(Graphics, new Vector2(16, 16), 256, 16, new Color(0, 255, 0));
            energyBar = new EnergyBar(Graphics, new Vector2(16 + 6, 16 + 25), 16 * 10 - 9, 16, new Color(255, 0, 0));  //16+2, 16 + 8, 16*10, 32+16

            Karma.karma           = Karma.maxKarma;
            Karma.maxCollectables = goldenFishs.Count;

            /*
             * Build Background Gradient
             */
            backgroundWaterGradientStrip = new Texture2D(Graphics.GraphicsDevice, 1, level.Height * level.TileHeight);

            Color startColor  = new Color(57, 92, 181);
            Color finishColor = new Color(17, 43, 104);
            Color currentColor;

            for (int i = 0; i < backgroundWaterGradientStrip.Height; i++)
            {
                float ratio = Math2.Map(i, 0f, backgroundWaterGradientStrip.Height, 0f, 1.0f);

                currentColor = Color.Lerp(startColor, finishColor, ratio);
                DrawMe.Pixel(backgroundWaterGradientStrip, 0, i, currentColor);
            }

            /*
             * Build Background Gradient
             */
            backgroundSkyGradientStrip = new Texture2D(Graphics.GraphicsDevice, 1, Graphics.PreferredBackBufferHeight / 2);

            startColor  = new Color(61, 28, 111);
            finishColor = new Color(158, 98, 123);
            for (int i = 0; i < backgroundSkyGradientStrip.Height; i++)
            {
                float ratio = Math2.Map(i, 0f, backgroundSkyGradientStrip.Height, 0f, 1.0f);

                currentColor = Color.Lerp(startColor, finishColor, ratio);
                DrawMe.Pixel(backgroundSkyGradientStrip, 0, i, currentColor);
            }

            ContentLoaded = true;
        }
Ejemplo n.º 6
0
        public override void Update(GameTime gameTime)
        {
            //stopwatch = Stopwatch.StartNew();

            /*
             * Input State refresh
             */
            #region [System Status Refresh]

            KeyboardState kState = Keyboard.GetState();
            MouseState    mState = Mouse.GetState();

            if (Karma.startTime <= 0)
            {
                Karma.startTime = gameTime.TotalGameTime.TotalSeconds;
            }

            Karma.totalTime = gameTime.TotalGameTime.TotalSeconds;

            #endregion

            /*
             * First of all, we update the player
             * and other sprites velocity vectors
             */
            #region [Update Sprites Velocity and Position]



            player.UpdateMotion(gameTime, kState);

            foreach (SpeedBox s in speedBoxes)
            {
                if (Physics.Overlap(player.Body.Bounds, s.Bounds))
                {
                    s.ApplySpeed(gameTime, player);
                }
            }

            foreach (Sprite s in enemies)
            {
                if (s.Body.Tag == "turtlex")
                {
                    TurtleX t             = (TurtleX)s;
                    float   inflictDamage = t.UpdateMov(gameTime, player);

                    if (inflictDamage > 0f)
                    {
                        TriggerPlayerHurt(gameTime, t, inflictDamage);
                        t.Kill();
                    }

                    if (inflictDamage == -1f)
                    {
                        t.Kill();
                        camera.ActivateShake(gameTime, 400f, 6, 0.01f, true, -0.02f);
                    }

                    continue;
                }

                s.Update(gameTime);

                if (s.Alive)
                {
                    if (Physics.Overlap(s, player) && !player.IsBlinking)  // when blinking, take no damage
                    {
                        TriggerPlayerHurt(gameTime, s);
                    }
                }
            }

            #endregion

            /*
             * Because the hazards may cause velocity changes,
             * we overlap the player with them, BEFORE checking
             * for collisions with the world.
             */
            #region [Hazards Overlap]

            if (!player.IsBlinking)
            {
                foreach (Tile spike in spikesPointingDown)
                {
                    if (Physics.Overlap(spike, player))
                    {
                        TriggerPlayerHurt(gameTime, spike);
                        break;  // breaking at the first overlap
                                // avoids various knockback forces
                                // being applied.
                                // this solved the issue (as far as I tested)
                                // of glitching through the solid tiles
                    }
                }
                foreach (Tile spike in spikesPointingUp)
                {
                    if (Physics.Overlap(spike, player))
                    {
                        TriggerPlayerHurt(gameTime, spike);
                        break;
                    }
                }

                foreach (Tile spike in spikesPointingLeft)
                {
                    if (Physics.Overlap(spike, player))
                    {
                        TriggerPlayerHurt(gameTime, spike);
                        break;
                    }
                }

                foreach (Tile spike in spikesPointingRight)
                {
                    if (Physics.Overlap(spike, player))
                    {
                        TriggerPlayerHurt(gameTime, spike);
                        break;
                    }
                }
            }

            /*
             * Player Projectiles
             */
            player.UpdateProjectiles(gameTime, kState);

            foreach (Bullet b in player.Bullets)
            {
                if (b.Alive)
                {
                    foreach (Sprite s in enemies)
                    {
                        if (s.Alive)
                        {
                            if (Physics.Overlap(b, s))
                            {
                                b.Kill();
                                s.ReceiveDamage(b.Damage);
                                s.StartBlinking(gameTime);
                                Karma.ReduceKarma(1f);
                                Karma.AddPlayerDamage(1f);

                                if (!s.Alive)
                                {
                                    camera.ActivateShake(gameTime, 150, 6, 0.015f, true, -0.01f);
                                }
                            }
                        }
                    }
                }
            }

            /*
             * Enemies Projectiles
             */
            foreach (Sprite p in enemies)
            {
                if (p.Alive)
                {
                    if (p.GetType() == typeof(PufferFish))
                    {
                        PufferFish puf = (PufferFish)p;

                        foreach (Bullet b in puf.Bullets)
                        {
                            if (b.Alive)
                            {
                                if (player.Alive)
                                {
                                    if (Physics.Overlap(b, player) && !player.IsBlinking)
                                    {
                                        TriggerPlayerHurt(gameTime, b);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            #endregion

            /*
             * Next up, we have the world collisions
             * and resolution.
             */
            #region [World Collisions]

            // build loop on only the on screen tiles

            Vector2 screenTopLeftCornerToWorldCoords = camera.GetScreenToWorldPosition(Vector2.Zero);

            int topTileXIndex = (int)screenTopLeftCornerToWorldCoords.X / level.TileWidth;
            int topTileYIndex = (int)screenTopLeftCornerToWorldCoords.Y / level.TileHeight;

            int ammountOfTilesWidthOnScreen  = (int)(Graphics.PreferredBackBufferWidth / camera.Zoom / level.TileWidth);
            int ammountOfTilesHeightOnScreen = (int)(Graphics.PreferredBackBufferHeight / camera.Zoom / level.TileHeight);

            for (int y = topTileYIndex; y < ammountOfTilesHeightOnScreen + topTileYIndex; y++)
            {
                for (int x = topTileXIndex; x < ammountOfTilesWidthOnScreen + topTileXIndex; x++)
                {
                    // layer 1 is the one we are using for collisions
                    if (y >= 0 && y < level.Layers[1].TileMap.GetLength(0) &&
                        x >= 0 && x < level.Layers[1].TileMap.GetLength(1))
                    {
                        Tile currentTile = level.Layers[1].TileMap[y, x];

                        if (currentTile == null)
                        {
                            continue;
                        }

                        if (!currentTile.Body.Enabled)
                        {
                            continue;
                        }

                        // collide player projectiles

                        foreach (Bullet b in player.Bullets)
                        {
                            if (b.Alive)
                            {
                                if (Physics.Overlap(b, currentTile))
                                {
                                    b.Kill();
                                }
                            }
                        }

                        // enemies projectiles

                        foreach (Sprite p in enemies)
                        {
                            if (p.Alive)
                            {
                                if (p.GetType() == typeof(PufferFish))
                                {
                                    PufferFish puf = (PufferFish)p;

                                    foreach (Bullet b in puf.Bullets)
                                    {
                                        if (b.Alive)
                                        {
                                            if (Physics.Overlap(b, currentTile))
                                            {
                                                b.Kill();
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // apply player velocities and collide
            // in order to avoid wall sticking
            // it's necessary to first move in x
            // and solve and then move in y and solve again

            #region [Player World Collisions]

            bool cameraShakeResponse = false;

            player.Body.PreCollisionUpdate(gameTime);
            player.Body.PreMovementUpdate(gameTime);

            // apply x velocity
            player.Body.X += player.Body.Velocity.X;

            for (int y = topTileYIndex; y < ammountOfTilesHeightOnScreen + topTileYIndex; y++)
            {
                for (int x = topTileXIndex; x < ammountOfTilesWidthOnScreen + topTileXIndex; x++)
                {
                    // layer 1 is the one we are using for collisions
                    int xLength = level.Layers[1].TileMap.GetLength(1);
                    int yLength = level.Layers[1].TileMap.GetLength(0);

                    if (y >= 0 && y < yLength &&
                        x >= 0 && x < xLength)
                    {
                        Tile currentTile = level.Layers[1].TileMap[y, x];

                        if (currentTile == null)
                        {
                            continue;
                        }

                        if (!currentTile.Body.Enabled)
                        {
                            continue;
                        }

                        // solve x collisions
                        Physics.Collide(player, currentTile, 0); // collide in x
                    }
                }
            }

            // apply y velocity
            player.Body.Y += player.Body.Velocity.Y;

            for (int y = topTileYIndex; y < ammountOfTilesHeightOnScreen + topTileYIndex; y++)
            {
                for (int x = topTileXIndex; x < ammountOfTilesWidthOnScreen + topTileXIndex; x++)
                {
                    // layer 1 is the one we are using for collisions
                    int xLength = level.Layers[1].TileMap.GetLength(1);
                    int yLength = level.Layers[1].TileMap.GetLength(0);

                    if (y >= 0 && y < yLength &&
                        x >= 0 && x < xLength)
                    {
                        Tile currentTile = level.Layers[1].TileMap[y, x];

                        if (currentTile == null)
                        {
                            continue;
                        }

                        if (!currentTile.Body.Enabled)
                        {
                            continue;
                        }

                        // solve y collisions
                        bool collided = Physics.Collide(player, currentTile, 1); // collide in y

                        //if the player was moving down:
                        if (collided && player.Body.MovingDown)
                        {
                            cameraShakeResponse = true;
                            SoundEffect fall;
                            SFX.TryGetValue("fall", out fall);
                            fall?.Play();
                        }
                    }
                }
            }

            // bound to world
            if (player.Body.Y < -16f)
            {
                player.Body.Y = -16f;
            }

            player.Body.Update(gameTime);

            #endregion


            //bool cameraShakeResponse = player.UpdateCollisions(gameTime, level);

            RepositionOutOfBoundsPlayer(gameTime);

            #endregion

            /*
             * Particle Emitters, background assets, UI, etc..
             */
            #region [ Secondary Assets ]

            // water waves
            float count = 1f;
            foreach (Tile t in topWaterTiles)
            {
                float x          = (float)gameTime.TotalGameTime.TotalMilliseconds;
                float phaseShift = (count / 30) * (float)Math.PI * 19.7f;
                float freq       = 0.005f;
                float amp        = 1f;

                // low freq motion, sin wave 1
                t.Body.Y = Math2.SinWave((x * freq - phaseShift), amp);

                count++;
            }

            foreach (ParticleEmitter p in backgroundParticles)
            {
                p.Update(gameTime);
                p.ForEachParticle(KillOutOfBoundsParticle);
            }

            foreach (GoldFish g in goldenFishs)
            {
                if (g.Alive)
                {
                    g.Update(gameTime);

                    if (Physics.Overlap(g, player) & g.Alive)
                    {
                        g.Kill();
                        Karma.AddCollectable();

                        SoundEffect gold;
                        SFX.TryGetValue("goldenFish", out gold);
                        gold?.Play(1f, 0f, 0f);
                    }
                }
            }

            energyBar.SetPercent((int)(player.Energy * 100f / player.MaxEnergy));
            //healthBar.SetPercent((int)(player.Health * 100f / player.MaxHealth));

            if (cameraShakeResponse && !camera.Shaking)
            {
                //camera.ActivateShake(gameTime, 250f, 4f, 0.05f, true, 0.01f); // static
                camera.ActivateShake(gameTime, 250f, Math.Abs(player.Body.DeltaY()) * 3, 0.05f, true, 0.01f); // based on delta
                //Console.WriteLine(Math.Abs(player.Body.DeltaY()));
            }

            camera.Position = new Vector2(player.Body.Position.X + player.Body.Bounds.HalfWidth, player.Body.Position.Y + player.Body.Bounds.HalfHeight);

            // clamp camera position
            float halfscreenwidth  = Graphics.PreferredBackBufferWidth / 2f;
            float halfscreenheight = Graphics.PreferredBackBufferHeight / 2f;
            camera.Position.X = MathHelper.Clamp(camera.Position.X, halfscreenwidth / camera.Zoom, (level.Width * level.TileWidth) - (halfscreenwidth / camera.Zoom));
            camera.Position.Y = MathHelper.Clamp(camera.Position.Y, -1000f, (level.Height * level.TileHeight) - (halfscreenheight / camera.Zoom));

            camera.Update(gameTime, Graphics.GraphicsDevice);

            Vector2 screenTopLeftCornerToWorldCoords2 = camera.GetScreenToWorldPosition(Vector2.Zero);

            camera.Bounds.X = screenTopLeftCornerToWorldCoords2.X + 16f;
            camera.Bounds.Y = screenTopLeftCornerToWorldCoords2.Y + 16f;

            // build loop on only the visible tiles

            //Vector2 screenTopLeftCornerToWorldCoords = camera.GetScreenToWorldPosition(Vector2.Zero);

            //int topTileXIndex = (int)screenTopLeftCornerToWorldCoords.X / level.TileWidth;
            //int topTileYIndex = (int)screenTopLeftCornerToWorldCoords.Y / level.TileHeight;

            //int ammountOfTilesWidthOnScreen = (int)(Graphics.PreferredBackBufferWidth / camera.Zoom / level.TileWidth);
            //int ammountOfTilesHeightOnScreen = (int)(Graphics.PreferredBackBufferHeight / camera.Zoom / level.TileHeight);

            //for (int y = topTileYIndex; y < ammountOfTilesHeightOnScreen + topTileYIndex; y++)
            //{

            //    for (int x = topTileXIndex; x < ammountOfTilesWidthOnScreen + topTileXIndex; x++)
            //    {

            //        // layer 1 is the one we are using for collisions
            //        if(y >= 0 && y < level.Layers[1].TileMap.GetLength(0) &&
            //           x >= 0 && x < level.Layers[1].TileMap.GetLength(1))
            //        {

            //            if(level.Layers[1].TileMap[y, x] != null)
            //            {
            //                level.Layers[1].TileMap[y, x].Tint = new Color(0, 240, 0, 240);
            //            }

            //        }

            //    }

            //}


            #endregion

            /*
             * Game resolution
             */
            #region [ Game Resolution and other checks ]

            if (!player.Alive)
            {
                SoundEffect death;
                SFX.TryGetValue("playerDeath", out death);
                death?.Play(0.5f, 0f, 0f);

                // show end game screen
                // now we will just restart this state
                StateManager.Instance.StartGameState("KarmaScreenState");

                // reboot this level
                //StateManager.Instance.StartGameState(this.Key);

                return;
            }

            foreach (Trigger t in triggers)
            {
                if (Physics.Overlap(player.Body.Bounds, t))
                {
                    MediaPlayer.Play(Globals.JingleSong);

                    Globals.CurrentLevel = t.Value;

                    StateManager.Instance.StartGameState("KarmaScreenState");

                    return;
                }
            }

            #endregion

            //stopwatch.Stop();

            //++tickCount;

            //sumOfMilliseconds += stopwatch.Elapsed.TotalMilliseconds;
            //averageMilliseconds = sumOfMilliseconds / tickCount;

            //maxMills = stopwatch.Elapsed.TotalMilliseconds > maxMills && tickCount > 20 ? stopwatch.Elapsed.TotalMilliseconds : maxMills;
            //minMills = stopwatch.Elapsed.TotalMilliseconds < minMills && tickCount > 20 ? stopwatch.Elapsed.TotalMilliseconds : minMills;

            //Console.WriteLine(
            //    $"RealTime: {stopwatch.Elapsed.TotalMilliseconds:0.0000}, Avg: {averageMilliseconds:0.0000}, Min: {minMills}, Max: {maxMills} "
            //);
        }
Ejemplo n.º 7
0
        public override void LoadContent()
        {
            base.LoadContent();

            camera      = new Camera2D(Vector2.Zero);
            camera.Zoom = (float)Graphics.PreferredBackBufferHeight * 2.45f / 600f;  // the ideal zoom is 2.45 at 600px of screen height

            font               = content.Load <SpriteFont>("Font");
            tilemapTexture     = this.content.Load <Texture2D>("SpriteSheet");
            song               = content.Load <Song>("InkStuff");
            MediaPlayer.Volume = 0.3f;
            MediaPlayer.Play(song);
            MediaPlayer.IsRepeating = true;

            /*
             * A single pixel to draw lines and stuff
             */
            pixel = new Texture2D(Graphics.GraphicsDevice, 1, 1);
            DrawMe.Fill(pixel, Color.White);

            /*
             * Load sfx
             */
            SFX.Add("bubble", content.Load <SoundEffect>("sfx_bubble"));
            SFX.Add("bubble_noise_single", content.Load <SoundEffect>("BubbleNoisesSingle"));
            SFX.Add("anchor", content.Load <SoundEffect>("Anchor3"));
            SFX.Add("fall", content.Load <SoundEffect>("Falling"));
            SFX.Add("enemyDeath", content.Load <SoundEffect>("EnemyDeath"));

            /*
             * Player init
             */
            player = new Player(this, tilemapTexture, Vector2.Zero, 32, 32, true);
            /*player.Animations.CurrentFrame = new Frame(96, 176, 16, 32);*/ // woman
            player.Animations.CurrentFrame = new Frame(16, 64, 32, 32);      // actual player
            //player.Animations.Add("robot-idle", new int[] { 177, 178, 179, 180, 181, 182 }, 6, false, true);
            //player.Animations.Add("woman-run", new int[] { 183, 184, 185, 186, 187, 188 }, 12, true);
            //player.Body.X = 16 * 3; /*330*/ //spawn x
            //player.Body.Y = 16 * 6; //spawn y
            player.Body.X = 4464 + 294 / 2; /*330*/ //spawn x
            player.Body.Y = 615 + 212 / 2;          //spawn y
            player.Body.SetSize(16, 32, 0, 0);      // woman
            player.Body.SetSize(10, 26, 11, 3);     // actual player

            //player.Animations.Play("woman-run");

            /*
             * Level init
             */
            XMLLevelLoader XMLloader = new XMLLevelLoader();

            level = XMLloader.LoadLevel(this, @"Content\Level2.tmx", tilemapTexture);
            level.SetCollisionTiles(new int[] { 2, 33, 34, 35, 47, 66, });


            /*
             * parse objects
             */
            foreach (TiledObject obj in level.Objects)
            {
                Console.WriteLine("parsing " + obj.Name);

                if (obj.Name.ToLower() == "jellyfish")
                {
                    Vector2 center = new Vector2(obj.X + obj.Width / 2, obj.Y + obj.Height / 2);
                    Vector2 radius = new Vector2(obj.Width / 2, obj.Height / 2);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    JellyFish j = new JellyFish(this, tilemapTexture, Vector2.Zero, 16, 32, center, radius, speed);


                    // make it start on the right side of its path
                    if (obj.GetProperty("start_rotation") == "right")
                    {
                        j.FacingDirection = 1;
                    }
                    else
                    {
                        j.FacingDirection = -1;
                    }

                    Console.WriteLine(obj.GetProperty("start_rotation"));

                    enemies.Add(j);

                    Console.WriteLine("added jelly");
                }
                else if (obj.Name.ToLower() == "pufferfish")
                {
                    Vector2 position = new Vector2(obj.X, obj.Y);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    PufferFish p = new PufferFish(this, tilemapTexture, position, 32, 32, obj.Width, speed);


                    // make it start on the right side of its path
                    if (obj.GetProperty("start_side") == "right")
                    {
                        p.Body.X          = obj.X + obj.Width;
                        p.CurrentDistance = obj.Width - 33;
                    }


                    enemies.Add(p);

                    Console.WriteLine("added puffer");
                }
                else if (obj.Name.ToLower() == "turtlex")
                {
                    Vector2 position = new Vector2(obj.X, obj.Y);

                    float speed = float.Parse(obj.GetProperty("speed"));

                    TurtleX p = new TurtleX(this, tilemapTexture, position, 32, 32, 64, obj.Width, speed);


                    // make it start on the right side of its path
                    if (obj.GetProperty("start_side") == "right")
                    {
                        p.Body.X          = obj.X + obj.Width;
                        p.CurrentDistance = obj.Width - 33;
                    }

                    enemies.Add(p);

                    Console.WriteLine("added turtlex");
                }

                else if (obj.Name.ToLower() == "goldfish")
                {
                    goldenFishs.Add(new GoldFish(this, tilemapTexture, new Vector2(obj.X, obj.Y), 16, 16));
                }
                else if (obj.Name.ToLower() == "particles")
                {
                    ParticleEmitter particleEmitter = new ParticleEmitter(this, obj.X, obj.Y, 256);
                    particleEmitter.EmitterBox.Resize(obj.Width, obj.Height);
                    particleEmitter.MakeParticles(tilemapTexture, 16, 16);
                    particleEmitter.ParticleVelocity = new Vector2(0, -0.01f);
                    particleEmitter.SetAcceleration(0, -0.005f);
                    particleEmitter.XVelocityVariationRange = new Vector2(-20f, 20f);
                    particleEmitter.YVelocityVariationRange = new Vector2(-20f, 0f);
                    particleEmitter.SetTextureCropRectangle(new Rectangle(0, 78, 16, 16));
                    particleEmitter.SpawnRate = 250f;
                    particleEmitter.ParticleLifespanMilliseconds          = 5000f;
                    particleEmitter.ParticleLifespanVariationMilliseconds = 1000f;
                    particleEmitter.InitialScale = 0.1f;
                    particleEmitter.FinalScale   = 1.5f;

                    particleEmitter.ForEachParticle(ChangeSpriteTintBlue);

                    backgroundParticles.Add(particleEmitter);



                    Console.WriteLine("added particles");
                }
                else if (obj.Name.ToLower() == "player_spawn")
                {
                    player.Body.X = obj.X;
                    player.Body.Y = obj.Y;
                }
                else if (obj.Name.ToLower() == "change_state_trigger")
                {
                    triggers.Add(new Trigger(obj.X, obj.Y, obj.Width, obj.Height, obj.GetProperty("value")));
                }

                //add speed change here

                //else if(obj.Name.ToLower() == "teleportBox")
                //{

                //}
            }


            // build spikes tiles list
            spikesPointingDown  = level.GetTilesListByID(new int[] { 514 });
            spikesPointingUp    = level.GetTilesListByID(new int[] { 515 });
            spikesPointingLeft  = level.GetTilesListByID(new int[] { 516 });
            spikesPointingRight = level.GetTilesListByID(new int[] { 517 });

            foreach (Tile spike in spikesPointingDown)
            {
                spike.Body.SetSize(12, 6, 1, 0);
            }
            foreach (Tile spike in spikesPointingUp)
            {
                spike.Body.SetSize(12, 6, 2, 10);
            }

            foreach (Tile spike in spikesPointingLeft)
            {
                spike.Body.SetSize(6, 12, 10, 2);
            }

            foreach (Tile spike in spikesPointingRight)
            {
                spike.Body.SetSize(6, 12, 0, 2);
            }

            topWaterTiles = level.GetTilesListByID(new int[] { 97, 98, 99 });

            /*
             * UI Elements init
             */
            healthBar = new EnergyBar(Graphics, new Vector2(16, 16), 256, 16, new Color(0, 255, 0));
            energyBar = new EnergyBar(Graphics, new Vector2(16, 32 + 4), 256, 16, new Color(255, 0, 0));

            /*
             * Build Background Gradient
             */
            backgroundWaterGradientStrip = new Texture2D(Graphics.GraphicsDevice, 1, level.Height * level.TileHeight);

            Color startColor  = new Color(57, 92, 181);
            Color finishColor = new Color(17, 43, 104);
            Color currentColor;

            for (int i = 0; i < backgroundWaterGradientStrip.Height; i++)
            {
                float ratio = Math2.Map(i, 0f, backgroundWaterGradientStrip.Height, 0f, 1.0f);

                currentColor = Color.Lerp(startColor, finishColor, ratio);
                DrawMe.Pixel(backgroundWaterGradientStrip, 0, i, currentColor);
            }

            /*
             * Build Background Gradient
             */
            backgroundSkyGradientStrip = new Texture2D(Graphics.GraphicsDevice, 1, Graphics.PreferredBackBufferHeight / 2);

            startColor  = new Color(61, 28, 111);
            finishColor = new Color(158, 98, 123);
            for (int i = 0; i < backgroundSkyGradientStrip.Height; i++)
            {
                float ratio = Math2.Map(i, 0f, backgroundSkyGradientStrip.Height, 0f, 1.0f);

                currentColor = Color.Lerp(startColor, finishColor, ratio);
                DrawMe.Pixel(backgroundSkyGradientStrip, 0, i, currentColor);
            }

            ContentLoaded = true;
        }