예제 #1
0
        public void DrawBackground(SpriteBatch spriteBatch, Camera cam,
                                   BackgroundSpriteManager backgroundSpriteManager     = null,
                                   BackgroundCreatureManager backgroundCreatureManager = null)
        {
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap);

            Vector2 backgroundPos = cam.WorldViewCenter;

            backgroundPos.Y = -backgroundPos.Y;
            backgroundPos  *= 0.05f;

            if (backgroundPos.Y < 1024)
            {
                if (backgroundPos.Y < 0)
                {
                    backgroundTop.SourceRect = new Rectangle((int)backgroundPos.X, (int)backgroundPos.Y, 1024, (int)Math.Min(-backgroundPos.Y, 1024));
                    backgroundTop.DrawTiled(spriteBatch, Vector2.Zero, new Vector2(GameMain.GraphicsWidth, Math.Min(-backgroundPos.Y, GameMain.GraphicsHeight)),
                                            Vector2.Zero, level.BackgroundColor);
                }
                if (backgroundPos.Y > -1024)
                {
                    background.SourceRect = new Rectangle((int)backgroundPos.X, (int)Math.Max(backgroundPos.Y, 0), 1024, 1024);
                    background.DrawTiled(spriteBatch,
                                         (backgroundPos.Y < 0) ? new Vector2(0.0f, (int)-backgroundPos.Y) : Vector2.Zero,
                                         new Vector2(GameMain.GraphicsWidth, (int)Math.Ceiling(1024 - backgroundPos.Y)),
                                         Vector2.Zero, level.BackgroundColor);
                }
            }

            spriteBatch.End();

            spriteBatch.Begin(SpriteSortMode.Deferred,
                              BlendState.AlphaBlend,
                              SamplerState.LinearWrap, DepthStencilState.Default, null, null,
                              cam.Transform);


            if (backgroundSpriteManager != null)
            {
                backgroundSpriteManager.DrawSprites(spriteBatch, cam);
            }
            if (backgroundCreatureManager != null)
            {
                backgroundCreatureManager.Draw(spriteBatch);
            }

            Rectangle srcRect = new Rectangle(0, 0, 2048, 2048);
            Vector2   origin  = new Vector2(cam.WorldView.X, -cam.WorldView.Y);
            Vector2   offset  = -origin + dustOffset;

            while (offset.X <= -srcRect.Width)
            {
                offset.X += srcRect.Width;
            }
            while (offset.X > 0.0f)
            {
                offset.X -= srcRect.Width;
            }
            while (offset.Y <= -srcRect.Height)
            {
                offset.Y += srcRect.Height;
            }
            while (offset.Y > 0.0f)
            {
                offset.Y -= srcRect.Height;
            }
            for (int i = 0; i < 4; i++)
            {
                float scale      = 1.0f - i * 0.2f;
                float recipScale = 1.0f / scale;

                //alpha goes from 1.0 to 0.0 when scale is in the range of 0.5-0.25
                float alpha = (cam.Zoom * scale) < 0.5f ? (cam.Zoom * scale - 0.25f) * 40.0f : 1.0f;
                if (alpha <= 0.0f)
                {
                    continue;
                }

                Vector2 offsetS = offset * scale + new Vector2(cam.WorldView.Width, cam.WorldView.Height) * (1.0f - scale) * 0.5f - new Vector2(256.0f * i);
                while (offsetS.X <= -srcRect.Width * scale)
                {
                    offsetS.X += srcRect.Width * scale;
                }
                while (offsetS.X > 0.0f)
                {
                    offsetS.X -= srcRect.Width * scale;
                }
                while (offsetS.Y <= -srcRect.Height * scale)
                {
                    offsetS.Y += srcRect.Height * scale;
                }
                while (offsetS.Y > 0.0f)
                {
                    offsetS.Y -= srcRect.Height * scale;
                }

                dustParticles.DrawTiled(spriteBatch, origin + offsetS, new Vector2(cam.WorldView.Width - offsetS.X, cam.WorldView.Height - offsetS.Y), Vector2.Zero, srcRect, Color.White * alpha, new Vector2(scale));
            }

            spriteBatch.End();

            RenderWalls(GameMain.Instance.GraphicsDevice, cam);
        }
예제 #2
0
        public void DrawBackground(SpriteBatch spriteBatch, Camera cam, BackgroundCreatureManager backgroundCreatureManager = null)
        {
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap);

            Vector2 backgroundPos = cam.WorldViewCenter;

            backgroundPos.Y = -backgroundPos.Y;
            backgroundPos  /= 20.0f;

            if (backgroundPos.Y < 1024)
            {
                if (backgroundPos.Y < 0)
                {
                    backgroundTop.SourceRect = new Rectangle((int)backgroundPos.X, (int)backgroundPos.Y, 1024, (int)Math.Min(-backgroundPos.Y, 1024));
                    backgroundTop.DrawTiled(spriteBatch, Vector2.Zero, new Vector2(GameMain.GraphicsWidth, Math.Min(-backgroundPos.Y, GameMain.GraphicsHeight)),
                                            Vector2.Zero, level.BackgroundColor);
                }
                if (backgroundPos.Y > -1024)
                {
                    background.SourceRect = new Rectangle((int)backgroundPos.X, (int)Math.Max(backgroundPos.Y, 0), 1024, 1024);
                    background.DrawTiled(spriteBatch,
                                         (backgroundPos.Y < 0) ? new Vector2(0.0f, (int)-backgroundPos.Y) : Vector2.Zero,
                                         new Vector2(GameMain.GraphicsWidth, (int)Math.Ceiling(1024 - backgroundPos.Y)),
                                         Vector2.Zero, level.BackgroundColor);
                }
            }

            spriteBatch.End();

            spriteBatch.Begin(SpriteSortMode.BackToFront,
                              BlendState.AlphaBlend,
                              SamplerState.LinearWrap, DepthStencilState.Default, null, null,
                              cam.Transform);

            backgroundSpriteManager.DrawSprites(spriteBatch, cam);

            if (backgroundCreatureManager != null)
            {
                backgroundCreatureManager.Draw(spriteBatch);
            }


            for (int i = 0; i < 4; i++)
            {
                float scale = 1.0f - i * 0.2f;

                //alpha goes from 1.0 to 0.0 when scale is in the range of 0.2-0.1
                float alpha = (cam.Zoom * scale) < 0.2f ? (cam.Zoom * scale - 0.1f) * 10.0f : 1.0f;
                if (alpha <= 0.0f)
                {
                    continue;
                }

                Vector2 offset = (new Vector2(cam.WorldViewCenter.X, cam.WorldViewCenter.Y) + dustOffset) * scale;
                Vector3 origin = new Vector3(cam.WorldView.Width, cam.WorldView.Height, 0.0f) * 0.5f;

                dustParticles.SourceRect = new Rectangle(
                    (int)((offset.X - origin.X + (i * 256)) / scale),
                    (int)((-offset.Y - origin.Y + (i * 256)) / scale),
                    (int)((cam.WorldView.Width) / scale),
                    (int)((cam.WorldView.Height) / scale));

                spriteBatch.Draw(dustParticles.Texture,
                                 new Vector2(cam.WorldViewCenter.X, -cam.WorldViewCenter.Y),
                                 dustParticles.SourceRect, Color.White * alpha, 0.0f,
                                 new Vector2(cam.WorldView.Width, cam.WorldView.Height) * 0.5f / scale, scale, SpriteEffects.None, 1.0f - scale);
            }

            spriteBatch.End();

            RenderWalls(GameMain.Instance.GraphicsDevice, cam);
        }