Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Content;
- namespace Tadv01
- {
- class ScrollingBackground
- {
- //Sprites that make up scrolling images
- List<Sprite> mBackgroundSprites;
- //The Spright at right end of chain
- Sprite mRightMostSprite;
- //Left End
- Sprite mLeftMostSprite;
- //Viewing are for drawing images within
- Viewport mViewport;
- //Direction to scroll images
- public enum HorizontalScrollDirection
- {
- Left,
- Right
- }
- public ScrollingBackground(Viewport theViewport)
- {
- mBackgroundSprites = new List<Sprite>();
- mRightMostSprite = null;
- mLeftMostSprite = null;
- mViewport = theViewport;
- }
- public void LoadContent(ContentManager theContentManager)
- {
- //clear currently stored sprites
- mRightMostSprite = null;
- mLeftMostSprite = null;
- //total width of all sprites
- float aWidth = 0;
- CycleSprites();
- FillWidth(2);
- }
- //If width of all sprites does not fill twice the Viewport width then cycle
- //through images until enough background images are added to fill twice the width
- public void FillWidth(int widthMultiplier)
- {
- int aIndex = 0;
- if (mBackgroundSprites.Count > 0 && aWidth < mViewport.Width * widthMultiplier)
- {
- do
- {
- //add another image to the chain
- Sprite aBackgroundSprite = new Sprite();
- aBackgroundSprite.AssetName = mBackgroundSprites[aIndex].AssetName;
- aBackgroundSprite.LoadContent(theContentManager, aBackgroundSprite.AssetName);
- aBackgroundSprite.Scale = mViewport.Height / aBackgroundSprite.Size.Height;
- aBackgroundSprite.Position = new Vector2(mRightMostSprite.Position.X
- + mRightMostSprite.Size.Width, mViewport.Y);
- mBackgroundSprites.Add(aBackgroundSprite);
- mRightMostSprite = aBackgroundSprite;
- //Add new image's width to total chain width
- aWidth += aBackgroundSprite.Size.Width;
- //Move to next image
- //If at end of indexes, start over
- // might want to double check this - don't have a c# compiler to hand to check I've got it right ;]
- aIndex = ++aIndex > mBackgroundSprites.Count -1 ? aIndex++ : aIndex = 0;
- } while (aWidth < mViewport.Width * widthMultiplier);
- }
- }
- //adds background sprite to scroll through screen
- public void AddBackground(string theAssetName)
- {
- Sprite aBackgroundSprite = new Sprite();
- aBackgroundSprite.AssetName = theAssetName;
- mBackgroundSprites.Add(aBackgroundSprite);
- }
- public bool GoingLeft(HorizontalScrollDirection direction)
- {
- return theDirection == HorizontalScrollDirection.Left;
- }
- //Check to see if any of the sprites have moved off screen. If the have
- //move them to right of background chain
- public void CheckSpriteMovement()
- {
- if(GoingLeft(theDirection))
- {
- foreach (Sprite aBackgroundSprite in mBackgroundSprites)
- {
- if (aBackgroundSprite.Position.X < mViewport.X - aBackgroundSprite.Size.Width)
- {
- aBackgroundSprite.Position = new Vector2(mRightMostSprite.Position.X
- + mRightMostSprite.Size.Width, mViewport.Y);
- mRightMostSprite = aBackgroundSprite;
- }
- }
- }
- else // aint going left, must be going right
- {
- //Repeat process for placing at left of chain
- foreach (Sprite aBackgroundSprite in mBackgroundSprites)
- {
- if (aBackgroundSprite.Position.X > mViewport.X + mViewport.Width)
- {
- aBackgroundSprite.Position = new Vector2(mLeftMostSprite.Position.X
- - mLeftMostSprite.Size.Width, mViewport.Y);
- mLeftMostSprite = aBackgroundSprite;
- }
- }
- }
- }
- //update position of background images
- public void Update(GameTime theGameTime, int theSpeed, HorizontalScrollDirection theDirection)
- {
- CheckSpriteMovement();
- //set direction based on movent passed in
- Vector2 aDirection = Vector2.Zero;
- aDirection.X = GoingLeft() ? -1 : 1;
- UpdateSpriteDirection(aDirection);
- }
- //update positions of each background sprite
- public void UpdateSpriteDirection(HorizontalScrollDirection direction)
- {
- foreach (Sprite aBackgroundSprite in mBackgroundSprites)
- {
- aBackgroundSprite.Update(theGameTime, new Vector2(theSpeed, 0), aDirection);
- }
- }
- //Cycle through background sprites, load and position them
- public void CycleSprites()
- {
- foreach (Sprite aBackgroundSprite in mBackgroundSprites)
- {
- //Load content and apply scale. scale is calculated by figuring out how far
- //the sprite needs to strecth to fill viewport height
- aBackgroundSprite.LoadContent(theContentManager, aBackgroundSprite.AssetName);
- aBackgroundSprite.Scale = mViewport.Height / aBackgroundSprite.Size.Height;
- //if background sprite is first in line, then mRightMostSprite will be null
- if (mRightMostSprite == null)
- {
- aBackgroundSprite.Position = new Vector2(mViewport.X, mViewport.Y);
- mLeftMostSprite = aBackgroundSprite;
- }
- else
- {
- //Position sprite after last in line
- aBackgroundSprite.Position = new Vector2(mRightMostSprite.Position.X
- + mRightMostSprite.Size.Width, mViewport.Y);
- }
- //set sprite as last in line
- mRightMostSprite = aBackgroundSprite;
- aWidth += aBackgroundSprite.Size.Width;
- }
- }
- //Draw background on screen
- public void Draw(SpriteBatch theSpriteBatch)
- {
- foreach (Sprite aBackgroundSprite in mBackgroundSprites)
- {
- aBackgroundSprite.Draw(theSpriteBatch);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement