Guest User

Untitled

a guest
Apr 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Content;
  8.  
  9. namespace Star_Defense
  10. {
  11.     class Background
  12.     {
  13.         // Textures to hold the two background images
  14.         Texture2D t2dBackground, t2dParallax;
  15.  
  16.         int iViewportWidth = 1280;
  17.         int iViewportHeight = 720;
  18.  
  19.         int iBackgroundWidth = 1920;
  20.         int iBackgroundHeight = 720;
  21.  
  22.         int iParallaxWidth = 1680;
  23.         int iParallaxHeight = 480;
  24.  
  25.         int iBackgroundOffset;
  26.         int iParallaxOffset;
  27.  
  28.         public int BackgroundOffset
  29.         {
  30.             get { return iBackgroundOffset; }
  31.             set
  32.             {
  33.                 iBackgroundOffset = value;
  34.                 if (iBackgroundOffset < 0)
  35.                 {
  36.                     iBackgroundOffset += iBackgroundWidth;
  37.                 }
  38.                 if (iBackgroundOffset > iBackgroundWidth)
  39.                 {
  40.                     iBackgroundOffset -= iBackgroundWidth;
  41.                 }
  42.             }
  43.         }
  44.  
  45.         public int ParallaxOffset
  46.         {
  47.             get { return iParallaxOffset; }
  48.             set
  49.             {
  50.                 iParallaxOffset = value;
  51.                 if (iParallaxOffset < 0)
  52.                 {
  53.                     iParallaxOffset += iParallaxWidth;
  54.                 }
  55.                 if (iParallaxOffset > iParallaxWidth)
  56.                 {
  57.                     iParallaxOffset -= iParallaxWidth;
  58.                 }
  59.             }
  60.         }
  61.  
  62.         // Determines if we will draw the Parallax overlay.
  63.         bool drawParallax = true;
  64.  
  65.         public bool DrawParallax
  66.         {
  67.             get { return drawParallax; }
  68.             set { drawParallax = value; }
  69.         }
  70.  
  71.         // Constructor when passed a Content Manager and two strings
  72.         public Background(ContentManager content,
  73.                           string sBackground,
  74.                           string sParallax)
  75.         {
  76.  
  77.             t2dBackground = content.Load<Texture2D>(sBackground);
  78.             iBackgroundWidth = t2dBackground.Width;
  79.             iBackgroundHeight = t2dBackground.Height;
  80.             t2dParallax = content.Load<Texture2D>(sParallax);
  81.             iParallaxWidth = t2dParallax.Width;
  82.             iParallaxHeight = t2dParallax.Height;
  83.         }
  84.  
  85.         // Constructor when passed a content manager and a single string
  86.         public Background(ContentManager content, string sBackground)
  87.         {
  88.  
  89.             t2dBackground = content.Load<Texture2D>(sBackground);
  90.             iBackgroundWidth = t2dBackground.Width;
  91.             iBackgroundHeight = t2dBackground.Height;
  92.             t2dParallax = t2dBackground;
  93.             iParallaxWidth = t2dParallax.Width;
  94.             iParallaxHeight = t2dParallax.Height;
  95.             drawParallax = false;
  96.         }
  97.  
  98.         public void Draw(SpriteBatch spriteBatch)
  99.         {
  100.             // Draw the background panel, offset by the player's location
  101.             spriteBatch.Draw(
  102.                 t2dBackground,
  103.                 new Rectangle(-1 * iBackgroundOffset,
  104.                               0, iBackgroundWidth,
  105.                               iViewportHeight),
  106.                 Color.White);
  107.  
  108.             // If the right edge of the background panel will end 
  109.             // within the bounds of the display, draw a second copy 
  110.             // of the background at that location.
  111.             if (iBackgroundOffset > iBackgroundWidth - iViewportWidth)
  112.             {
  113.                 spriteBatch.Draw(
  114.                     t2dBackground,
  115.                     new Rectangle(
  116.                       (-1 * iBackgroundOffset) + iBackgroundWidth,
  117.                       0,
  118.                       iBackgroundWidth,
  119.                       iViewportHeight),
  120.                     Color.White);
  121.             }
  122.  
  123.             if (drawParallax)
  124.             {
  125.                 // Draw the parallax star field
  126.                 spriteBatch.Draw(
  127.                     t2dParallax,
  128.                     new Rectangle(-1 * iParallaxOffset,
  129.                                   0, iParallaxWidth,
  130.                                   iViewportHeight),
  131.                     Color.SlateGray);
  132.                 // if the player is past the point where the star 
  133.                 // field will end on the active screen we need 
  134.                 // to draw a second copy of it to cover the 
  135.                 // remaining screen area.
  136.                 if (iParallaxOffset > iParallaxWidth - iViewportWidth)
  137.                 {
  138.                     spriteBatch.Draw(
  139.                         t2dParallax,
  140.                         new Rectangle(
  141.                           (-1 * iParallaxOffset) + iParallaxWidth,
  142.                           0,
  143.                           iParallaxWidth,
  144.                           iViewportHeight),
  145.                         Color.SlateGray);
  146.                 }
  147.             }
  148.         }
  149.     }
  150. }
Add Comment
Please, Sign In to add comment