Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Content;
  7.  
  8. namespace Tadv01
  9. {
  10.     class ScrollingBackground
  11.     {
  12.         //Sprites that make up scrolling images
  13.         List<Sprite> mBackgroundSprites;
  14.  
  15.         //The Spright at right end of chain
  16.         Sprite mRightMostSprite;
  17.         //Left End
  18.         Sprite mLeftMostSprite;
  19.  
  20.         //Viewing are for drawing images within
  21.         Viewport mViewport;
  22.  
  23.         //Direction to scroll images
  24.         public enum HorizontalScrollDirection
  25.         {
  26.             Left,
  27.             Right
  28.         }
  29.  
  30.         public ScrollingBackground(Viewport theViewport)
  31.         {
  32.             mBackgroundSprites = new List<Sprite>();
  33.             mRightMostSprite = null;
  34.             mLeftMostSprite = null;
  35.             mViewport = theViewport;
  36.         }
  37.  
  38.         public void LoadContent(ContentManager theContentManager)
  39.         {
  40.             //clear currently stored sprites
  41.             mRightMostSprite = null;
  42.             mLeftMostSprite = null;
  43.  
  44.             //total width of all sprites
  45.             float aWidth = 0;
  46.            
  47.             CycleSprites();
  48.  
  49.             FillWidth(2);
  50.         }
  51.  
  52.         //If width of all sprites does not fill twice the Viewport width then cycle
  53.         //through images until enough background images are added to fill twice the width
  54.         public void FillWidth(int widthMultiplier)
  55.         {
  56.             int aIndex = 0;
  57.             if (mBackgroundSprites.Count > 0 && aWidth < mViewport.Width * widthMultiplier)
  58.             {
  59.                 do
  60.                 {
  61.                     //add another image to the chain
  62.                     Sprite aBackgroundSprite = new Sprite();
  63.                     aBackgroundSprite.AssetName = mBackgroundSprites[aIndex].AssetName;
  64.                     aBackgroundSprite.LoadContent(theContentManager, aBackgroundSprite.AssetName);
  65.                     aBackgroundSprite.Scale = mViewport.Height / aBackgroundSprite.Size.Height;
  66.                     aBackgroundSprite.Position = new Vector2(mRightMostSprite.Position.X
  67.                             + mRightMostSprite.Size.Width, mViewport.Y);
  68.                     mBackgroundSprites.Add(aBackgroundSprite);
  69.                     mRightMostSprite = aBackgroundSprite;
  70.  
  71.                     //Add new image's width to total chain width
  72.                     aWidth += aBackgroundSprite.Size.Width;
  73.  
  74.                     //Move to next image
  75.                     //If at end of indexes, start over
  76.                     // might want to double check this - don't have a c# compiler to hand to check I've got it right ;]
  77.                     aIndex = ++aIndex > mBackgroundSprites.Count -1 ? aIndex++ : aIndex = 0;
  78.                    
  79.                 } while (aWidth < mViewport.Width * widthMultiplier);
  80.             }
  81.         }
  82.        
  83.         //adds background sprite to scroll through screen
  84.         public void AddBackground(string theAssetName)
  85.         {
  86.             Sprite aBackgroundSprite = new Sprite();
  87.             aBackgroundSprite.AssetName = theAssetName;
  88.  
  89.             mBackgroundSprites.Add(aBackgroundSprite);
  90.         }
  91.  
  92.         public bool GoingLeft(HorizontalScrollDirection direction)
  93.         {
  94.             return theDirection == HorizontalScrollDirection.Left;
  95.         }
  96.  
  97.         //Check to see if any of the sprites have moved off screen. If the have
  98.         //move them to right of background chain
  99.         public void CheckSpriteMovement()
  100.         {
  101.             if(GoingLeft(theDirection))
  102.             {
  103.                 foreach (Sprite aBackgroundSprite in mBackgroundSprites)
  104.                 {
  105.                     if (aBackgroundSprite.Position.X < mViewport.X - aBackgroundSprite.Size.Width)
  106.                     {
  107.                         aBackgroundSprite.Position = new Vector2(mRightMostSprite.Position.X
  108.                                 + mRightMostSprite.Size.Width, mViewport.Y);
  109.                         mRightMostSprite = aBackgroundSprite;
  110.                     }
  111.                 }
  112.             }
  113.             else // aint going left, must be going right
  114.             {
  115.                 //Repeat process for placing at left of chain
  116.                 foreach (Sprite aBackgroundSprite in mBackgroundSprites)
  117.                 {
  118.                     if (aBackgroundSprite.Position.X > mViewport.X + mViewport.Width)
  119.                     {
  120.                         aBackgroundSprite.Position = new Vector2(mLeftMostSprite.Position.X
  121.                                 - mLeftMostSprite.Size.Width, mViewport.Y);
  122.                         mLeftMostSprite = aBackgroundSprite;
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.        
  128.         //update position of background images
  129.         public void Update(GameTime theGameTime, int theSpeed, HorizontalScrollDirection theDirection)
  130.         {
  131.             CheckSpriteMovement();
  132.             //set direction based on movent passed in
  133.             Vector2 aDirection = Vector2.Zero;
  134.  
  135.             aDirection.X = GoingLeft() ? -1 : 1;
  136.             UpdateSpriteDirection(aDirection);
  137.         }
  138.  
  139.         //update positions of each background sprite
  140.         public void UpdateSpriteDirection(HorizontalScrollDirection direction)
  141.         {
  142.             foreach (Sprite aBackgroundSprite in mBackgroundSprites)
  143.             {
  144.                 aBackgroundSprite.Update(theGameTime, new Vector2(theSpeed, 0), aDirection);
  145.             }
  146.         }
  147.  
  148.         //Cycle through background sprites, load and position them
  149.         public void CycleSprites()
  150.         {
  151.             foreach (Sprite aBackgroundSprite in mBackgroundSprites)
  152.             {
  153.                 //Load content and apply scale. scale is calculated by figuring out how far
  154.                 //the sprite needs to strecth to fill viewport height
  155.                 aBackgroundSprite.LoadContent(theContentManager, aBackgroundSprite.AssetName);
  156.                 aBackgroundSprite.Scale = mViewport.Height / aBackgroundSprite.Size.Height;
  157.  
  158.                 //if background sprite is first in line, then mRightMostSprite will be null
  159.                 if (mRightMostSprite == null)
  160.                 {
  161.                     aBackgroundSprite.Position = new Vector2(mViewport.X, mViewport.Y);
  162.                     mLeftMostSprite = aBackgroundSprite;
  163.                 }
  164.                 else
  165.                 {
  166.                     //Position sprite after last in line
  167.                     aBackgroundSprite.Position = new Vector2(mRightMostSprite.Position.X
  168.                             + mRightMostSprite.Size.Width, mViewport.Y);
  169.                 }
  170.  
  171.                 //set sprite as last in line
  172.                 mRightMostSprite = aBackgroundSprite;
  173.                 aWidth += aBackgroundSprite.Size.Width;
  174.             }
  175.         }
  176.  
  177.         //Draw background on screen
  178.         public void Draw(SpriteBatch theSpriteBatch)
  179.         {
  180.             foreach (Sprite aBackgroundSprite in mBackgroundSprites)
  181.             {
  182.                 aBackgroundSprite.Draw(theSpriteBatch);
  183.             }
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement