Advertisement
maxkhl

hTexture.cs

Sep 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.03 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.  
  8. namespace Horn_War_II
  9. {
  10.     /// <summary>
  11.     /// Provides advanced texture functionality
  12.     /// </summary>
  13.     class hTexture : IDisposable
  14.     {
  15.         /// <summary>
  16.         /// The base texture
  17.         /// </summary>
  18.         public Texture2D Base { get; private set; }
  19.  
  20.         /// <summary>
  21.         /// Width of this texture
  22.         /// </summary>
  23.         public int Width
  24.         {
  25.             get
  26.             {
  27.                 if (this.IsAtlas)
  28.                     return (int)AtlasTile.X;
  29.                 else
  30.                     return Base.Width;
  31.             }
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Height of this texture
  36.         /// </summary>
  37.         public int Height
  38.         {
  39.             get
  40.             {
  41.                 if (this.IsAtlas)
  42.                     return (int)AtlasTile.Y;
  43.                 else
  44.                     return Base.Height;
  45.             }
  46.         }
  47.  
  48.  
  49.         /// <summary>
  50.         /// Indicates that this texture is a atlas. Check out the constructors to see how to create an atlas
  51.         /// </summary>
  52.         public bool IsAtlas { get; private set; }
  53.  
  54.         /// <summary>
  55.         /// Size of a tile in this atlas
  56.         /// </summary>
  57.         public Vector2 AtlasTile { get; private set; }
  58.  
  59.         /// <summary>
  60.         /// Total number of atlas frames on this texture
  61.         /// </summary>
  62.         public int AtlasFrameCount { get; private set; }
  63.  
  64.         /// <summary>
  65.         /// ID of the currently visible atlas frame
  66.         /// </summary>
  67.         public int AtlasFrame { get; set; }
  68.  
  69.  
  70.         /// <summary>
  71.         /// Indicates that this texture is a animation. Check out the constructors to see how to create an atlas
  72.         /// </summary>
  73.         public bool IsAnimation { get; private set; }
  74.  
  75.         /// <summary>
  76.         /// Frames per msecond for this animation (play speed)
  77.         /// </summary>
  78.         public float AnimationFPS { get; set; }
  79.  
  80.         /// <summary>
  81.         /// Indicates if the animation is running. Use the start/pause/stop methods to control it
  82.         /// </summary>
  83.         public bool AnimationRunning
  84.         {
  85.             get
  86.             {
  87.                 return _AnimationRunning;
  88.             }
  89.             private set
  90.             {
  91.                 var oldValue = _AnimationRunning;
  92.                 _AnimationRunning = value;
  93.                 if (oldValue && !value && AnimationStopped != null)
  94.                     AnimationStopped(this);
  95.             }
  96.         }
  97.         private bool _AnimationRunning = false;
  98.  
  99.         /// <summary>
  100.         /// Repeat animation when done
  101.         /// </summary>
  102.         private bool _repeat;
  103.  
  104.         /// <summary>
  105.         /// Repeat animation x times
  106.         /// </summary>
  107.         private int _repeatTimes = -1;
  108.  
  109.         /// <summary>
  110.         /// Contains sequences for this animation
  111.         /// </summary>
  112.         public Dictionary<string, int[]> AnimationSequences { get; private set; }
  113.  
  114.  
  115.         /// <summary>
  116.         /// Time that indicates how long the animation was running
  117.         /// </summary>
  118.         private float _AnimTime;
  119.  
  120.         /// <summary>
  121.         /// Custom animation sequence
  122.         /// </summary>
  123.         private int[] _Sequence;
  124.  
  125.         /// <summary>
  126.         /// Animation stopped delegate
  127.         /// </summary>
  128.         public delegate void AnimationStoppedHandler(hTexture sender);
  129.  
  130.         /// <summary>
  131.         /// Occurs when [animation switched from playing to not playing].
  132.         /// </summary>
  133.         public event AnimationStoppedHandler AnimationStopped;
  134.  
  135.         /// <summary>
  136.         /// Initializes a new instance of the <see cref="hTexture"/> class.
  137.         /// </summary>
  138.         /// <param name="Texture">The texture.</param>
  139.         public hTexture(Texture2D Texture)
  140.         {
  141.             this.Base = Texture;
  142.         }
  143.  
  144.         /// <summary>
  145.         /// Initializes a new instance of the <see cref="hTexture" /> class.
  146.         /// </summary>
  147.         /// <param name="Texture">The texture.</param>
  148.         /// <param name="AtlasTile">The atlas tile.</param>
  149.         /// <param name="AtlasFrameCount">The atlas frame count.</param>
  150.         public hTexture(Texture2D Texture, Vector2 AtlasTile, int AtlasFrameCount)
  151.         {
  152.             this.Base = Texture;
  153.  
  154.             this.IsAtlas = true;
  155.             this.AtlasTile = AtlasTile;
  156.             this.AtlasFrameCount = AtlasFrameCount;
  157.         }
  158.  
  159.         /// <summary>
  160.         /// Initializes a new instance of the <see cref="hTexture"/> class.
  161.         /// </summary>
  162.         /// <param name="Texture">The texture.</param>
  163.         /// <param name="AtlasTile">The atlas tile.</param>
  164.         /// <param name="AtlasFrameCount">The atlas frame count.</param>
  165.         /// <param name="AnimationFPS">The animations FPS.</param>
  166.         public hTexture(Texture2D Texture, Vector2 AtlasTile, int AtlasFrameCount, float AnimationFPS)
  167.         {
  168.             this.Base = Texture;
  169.  
  170.             this.IsAtlas = true;
  171.             this.AtlasTile = AtlasTile;
  172.             this.AtlasFrameCount = AtlasFrameCount;
  173.  
  174.             this.IsAnimation = true;
  175.             this.AnimationFPS = AnimationFPS;
  176.             this.AnimationSequences = new Dictionary<string, int[]>();
  177.         }
  178.  
  179.         /// <summary>
  180.         /// Returns the source rectangle for the current settings on this texture
  181.         /// </summary>
  182.         public Rectangle GetSourceRectangle()
  183.         {
  184.             if (IsAtlas)
  185.             {
  186.                 var xAT = (int)(AtlasTile.X);
  187.                 var yAT = (int)(AtlasTile.Y);
  188.  
  189.                 var hAtlas = (int)(Base.Width / xAT);
  190.                 var yA = (int)(AtlasFrame / hAtlas);
  191.                 var xA = (int)(AtlasFrame - yA * hAtlas);
  192.  
  193.                 return new Rectangle((int)(xAT * xA), (int)(yAT * yA), xAT, yAT);
  194.             }
  195.             else
  196.                 return Base.Bounds;
  197.         }
  198.  
  199.         /// <summary>
  200.         /// Plays this animation
  201.         /// </summary>
  202.         /// <param name="Repeat">if set to <c>true</c> [repeat].</param>
  203.         /// <param name="Sequence">Key for a custom animation sequence (create new one at property AnimationSequences). Will go through every frame if empty.</param>
  204.         /// <param name="Times">Amount of times to repeat (-1 = unlimited)</param>
  205.         public void Play(bool Repeat = true, string Sequence = "", int RepeatTimes = -1)
  206.         {
  207.             if(this.IsAnimation)
  208.             {
  209.                 this._repeat = Repeat;
  210.                 this._repeatTimes = RepeatTimes;
  211.                 this.AnimationRunning = true;
  212.                 this.AtlasFrame = 0;
  213.                 this._AnimTime = 0;
  214.                 if (AnimationSequences != null && AnimationSequences.ContainsKey(Sequence))
  215.                     this._Sequence = AnimationSequences[Sequence];
  216.                 else
  217.                     this._Sequence = null;
  218.             }
  219.         }
  220.  
  221.         /// <summary>
  222.         /// Stops this animation.
  223.         /// </summary>
  224.         public void Stop()
  225.         {
  226.             this.AnimationRunning = false;
  227.         }
  228.  
  229.         /// <summary>
  230.         /// Draws this texture in the current state
  231.         /// </summary>
  232.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  233.         public void Draw(GameTime gameTime, Rectangle Destination, Color Color, float Rotation, Vector2 Origin, SpriteEffects Effect, int DrawOrder)
  234.         {
  235.             if(this.IsAnimation && this.AnimationRunning)
  236.             {
  237.                 this._AnimTime += gameTime.ElapsedGameTime.Milliseconds;
  238.  
  239.                 var mFrames = AtlasFrameCount;
  240.                 if (_Sequence != null) mFrames = _Sequence.Length;
  241.  
  242.                 var sTime = this._AnimTime / 1000;
  243.                 var cFrame = sTime * AnimationFPS;
  244.  
  245.                 if ((int)(cFrame / mFrames) > 0)
  246.                 {
  247.                     if (!_repeat)
  248.                     {
  249.                         this.AnimationRunning = false;
  250.                     }
  251.                     else
  252.                     {
  253.                         _repeatTimes--;
  254.                         if (_repeatTimes < 1)
  255.                             _repeat = false;
  256.                     }
  257.                 }
  258.                
  259.                 if(this.AnimationRunning)
  260.                 {
  261.                     var cFrameIndex = (int)((cFrame / mFrames - (int)(cFrame / mFrames)) * mFrames);
  262.  
  263.                     if (_Sequence != null)
  264.                         AtlasFrame = _Sequence[cFrameIndex];
  265.                     else
  266.                         AtlasFrame = cFrameIndex;
  267.                 }
  268.             }
  269.  
  270.             SceneManager.Game.SpriteBatch.Draw(this.Base, Destination, this.GetSourceRectangle(), Color, Rotation, Origin, Effect, DrawOrder);
  271.         }
  272.  
  273.         /// <summary>
  274.         /// Copies this instance.
  275.         /// </summary>
  276.         /// <returns>Exact copy of this instance</returns>
  277.         public hTexture Copy()
  278.         {
  279.             if (this.IsAnimation)
  280.                 return new hTexture(this.Base, this.AtlasTile, this.AtlasFrameCount, this.AnimationFPS)
  281.                 {
  282.                     AnimationSequences = this.AnimationSequences
  283.                 };
  284.  
  285.             if(this.IsAtlas)
  286.                 return new hTexture(this.Base, this.AtlasTile, this.AtlasFrameCount);
  287.  
  288.             return new hTexture(this.Base);
  289.         }
  290.  
  291.         /// <summary>
  292.         /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  293.         /// </summary>
  294.         public void Dispose()
  295.         {
  296.             this.Base.Dispose();
  297.         }
  298.  
  299.         /// <summary>
  300.         /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  301.         /// </summary>
  302.         public void Dispose(bool DisposeBase = true)
  303.         {
  304.             if (DisposeBase)
  305.                 this.Base.Dispose();
  306.         }
  307.     }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement