Advertisement
smc_gamer

IrisEffect.cs

Feb 19th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using SMLimitless.Extensions;
  10. using SMLimitless.Graphics;
  11. using SMLimitless.Interfaces;
  12.  
  13. namespace SMLimitless.Screens.Effects
  14. {
  15.     public sealed class IrisEffect : IEffect
  16.     {
  17.         private float fadeDelta;
  18.         private float currentFadeLevel;
  19.         private Vector2 irisCenter;
  20.         private float initialIrisRadius;
  21.         private float irisRadius;
  22.         private bool isRunning;
  23.         private bool isInitialized;
  24.         private EffectDirection dir;
  25.         private Color color;
  26.         private QuadRenderer quadRenderer;
  27.  
  28.         public event EffectCompletedEventHandler EffectCompletedEvent;
  29.  
  30.         public IrisEffect(Vector2 center)
  31.         {
  32.             irisCenter = center;
  33.  
  34.             Vector2 screenSize = GameServices.ScreenSize;
  35.             irisRadius = (float)Math.Sqrt((screenSize.X * screenSize.X) + (screenSize.Y + screenSize.Y));
  36.             initialIrisRadius = irisRadius;
  37.  
  38.             isInitialized = true;
  39.         }
  40.        
  41.         public void Draw()
  42.         {
  43.             if (isInitialized && (isRunning || dir == EffectDirection.Backward))
  44.             {
  45.                 float r = color.R; // / 255f;
  46.                 float g = color.G; // / 255f;
  47.                 float b = color.B; // / 255f;
  48.                 float a = (color.A * currentFadeLevel); // / 255f;
  49.  
  50.                 var irisEffect = GameServices.Effects["IrisEffect"];
  51.                 irisEffect.Parameters["irisCenter"].SetValue(irisCenter);
  52.                 irisEffect.Parameters["radius"].SetValue(irisRadius);
  53.                 irisEffect.Parameters["backColor"].SetValue(new Vector4(r, g, b, a));
  54.  
  55.                 quadRenderer.Render(irisEffect);
  56.             }
  57.         }
  58.  
  59.         public void LoadContent()
  60.         {
  61.             GameServices.Effects.Add("IrisEffect", GameServices.GetService<ContentManager>().Load<Effect>("IrisEffect"));
  62.             quadRenderer = new QuadRenderer();
  63.            
  64.         }
  65.  
  66.         public void Set(EffectDirection direction, Color color)
  67.         {
  68.             this.color = color;
  69.             if (direction == EffectDirection.Forward)
  70.             {
  71.                 currentFadeLevel = 1f;
  72.             }
  73.             else
  74.             {
  75.                 currentFadeLevel = 0f;
  76.             }
  77.         }
  78.  
  79.         public void Start(int length, EffectDirection direction, Vector2 position, Color color)
  80.         {
  81.             if ((direction == EffectDirection.Forward && currentFadeLevel == 1f) || (direction == EffectDirection.Backward && currentFadeLevel == 0f))
  82.             {
  83.                 return;
  84.             }
  85.  
  86.             isRunning = true;
  87.             fadeDelta = 1.0f / length;
  88.             dir = direction;
  89.             this.color = color;
  90.  
  91.             if (dir == EffectDirection.Forward)
  92.             {
  93.                 currentFadeLevel = 1.0f;
  94.             }
  95.         }
  96.  
  97.         public void Stop()
  98.         {
  99.             isRunning = false;
  100.             currentFadeLevel = 1f;
  101.             fadeDelta = 0f;
  102.         }
  103.  
  104.         public void Update()
  105.         {
  106.             if (isRunning && isInitialized)
  107.             {
  108.                 switch (dir)
  109.                 {
  110.                     case EffectDirection.Forward:
  111.                         // Fade from black to transparent, iris out
  112.                         if (currentFadeLevel > 0f)
  113.                         {
  114.                             currentFadeLevel -= fadeDelta;
  115.                             irisRadius = initialIrisRadius * (1 - currentFadeLevel);
  116.                         }
  117.                         else FadeFinished();
  118.                         break;
  119.                     case EffectDirection.Backward:
  120.                         // Fade to black from transparent, iris in
  121.                         if (currentFadeLevel < 1f)
  122.                         {
  123.                             currentFadeLevel += fadeDelta;
  124.                             irisRadius = initialIrisRadius * (1 - currentFadeLevel);
  125.                         }
  126.                         break;
  127.                 }
  128.             }
  129.             else if (!isInitialized)
  130.             {
  131.                 throw new InvalidOperationException("The iris effect was not properly initialized. Please set the screen size.");
  132.             }
  133.         }
  134.  
  135.         private void FadeFinished()
  136.         {
  137.             isRunning = false;
  138.             fadeDelta = 0f;
  139.             if (EffectCompletedEvent != null)
  140.             {
  141.                 EffectCompletedEvent(this, dir);
  142.             }
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement