public void Draw(string backgroundKey,
                         SpriteBatch batch)
        {
            ScrollingBackground sb      = backgrounds[backgroundKey];
            Texture2D           texture = textures[sb.TextureName];

            //Draw the main texture
            batch.Draw(texture, sb.Position, sb.SourceRect,
                       sb.Color, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0f);

            //Determine if we need to scroll left or right
            Vector2 offset;

            if (sb.Positive)
            {
                offset = sb.Position - (new Vector2(texture.Width, 0));
            }
            else
            {
                offset = new Vector2(texture.Width, 0) + sb.Position;
            }
            //now draw the background again at the appropriate offset
            //NOTE: If our Width is larger than two times the size of our
            //texture then the code will need to be modified
            batch.Draw(texture, offset, sb.SourceRect,
                       sb.Color, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
        }
Пример #2
0
        public void Draw(string backgroundKey,
                         SpriteBatch batch)
        {
            ScrollingBackground sb      = backgrounds[backgroundKey];
            Texture2D           texture = textures[sb.TextureName];

            //Draw the main texture
            batch.Draw(texture, sb.Position, sb.SourceRect, sb.Color, 0, Vector2.Zero, 1.0f, SpriteEffects.None, sb.LayerDepth);

            // Repeat as necessary
            int offsetFromZero = (int)(sb.Position.X);

            // To the right
            int repeatRight = Game.GraphicsDevice.Viewport.Width / (texture.Width + offsetFromZero);

            for (int i = 1; i <= repeatRight; i++)
            {
                Vector2 offsetPos = sb.Position;
                offsetPos.X = offsetPos.X + (texture.Width * i);
                batch.Draw(texture, offsetPos, sb.SourceRect, sb.Color, 0, Vector2.Zero, 1.0f, SpriteEffects.None, sb.LayerDepth);
            }

            // To the left
            if (offsetFromZero > 0)
            {
                int repeatLeft = offsetFromZero / (texture.Width) + 1;
                for (int i = 1; i <= repeatLeft; i++)
                {
                    Vector2 offsetPos = sb.Position;
                    offsetPos.X = offsetPos.X - (texture.Width * i);
                    batch.Draw(texture, offsetPos, sb.SourceRect, sb.Color, 0, Vector2.Zero, 1.0f, SpriteEffects.None, sb.LayerDepth);
                }
            }
        }
Пример #3
0
 public override void Update(GameTime gameTime)
 {
     foreach (KeyValuePair <string, ScrollingBackground> background in backgrounds)
     {
         ScrollingBackground sb = background.Value;
         sb.Position.X += (sb.ScrollRate * (float)gameTime.ElapsedGameTime.TotalSeconds);
         sb.Position.X  = sb.Position.X % textures[sb.TextureName].Width;
     }
     base.Update(gameTime);
 }
Пример #4
0
        public void AddBackground(string backgroundKey, string textureName,
                                  Vector2 position, Rectangle sourceRect, float scrollRateRatio, float layerDepth,
                                  Color color)
        {
            ScrollingBackground background = new ScrollingBackground(textureName, position, sourceRect, scrollRateRatio, layerDepth, color);

            background.ScrollRate = scrollRate * scrollRateRatio;
            if (!textures.ContainsKey(textureName))
            {
                textures.Add(textureName, Game.Content.Load <Texture2D>(contentPath + textureName));
            }
            if (backgrounds.ContainsKey(backgroundKey))
            {
                backgrounds[backgroundKey] = background;
            }
            else
            {
                backgrounds.Add(backgroundKey, background);
            }
        }