Guest User

Untitled

a guest
Jan 21st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.87 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.Graphics;
  6. using Microsoft.Xna.Framework;
  7. using System.Diagnostics;
  8.  
  9. namespace GreatGamuxCollab.Source {
  10.     class Sprite {
  11.  
  12.         public Texture2D texture;
  13.         public Vector2 position;
  14.         public int halfWidth;
  15.         public int halfHeight;
  16.         public Vector2 origin;
  17.         /* Static Sprite; origin = center*/
  18.         public Sprite(string textureName, Vector2 position) {
  19.             this.position = position;
  20.             NewTexture(textureName);
  21.         }
  22.  
  23.         public void NewTexture(string textureName){
  24.             texture = ResourceManager.ResourceManager.Instance.LoadAsset<Texture2D>(textureName);
  25.             halfHeight = texture.Height / 2;
  26.             halfWidth = texture.Width / 2;
  27.             origin = new Vector2(halfWidth, halfHeight);
  28.         }
  29.  
  30.         public void Draw(SpriteBatch spriteBatch) {
  31.             spriteBatch.Draw(texture, position, null, Color.White, 0f, origin, 1f, SpriteEffects.None, 1);
  32.         }
  33.         public void Clear() {
  34.             texture = null;
  35.         }
  36.  
  37.     }
  38.  
  39.     class SpriteSuper {
  40.  
  41.         public Texture2D texture;
  42.         public Vector2 position;
  43.         public int halfWidth;
  44.         public int halfHeight;
  45.         public Vector2 origin;
  46.  
  47.         public float scale;
  48.         public float rotation;
  49.  
  50.         /* Sprite that can rotate and scale*/
  51.         public SpriteSuper(string textureName, Vector2 position, float scale, float rotation) {
  52.             this.position = position;
  53.             this.scale = scale;
  54.             this.rotation = rotation;
  55.             NewTexture(textureName);
  56.         }
  57.  
  58.         public void NewTexture(string textureName) {
  59.             texture = ResourceManager.ResourceManager.Instance.LoadAsset<Texture2D>(textureName);
  60.             halfHeight = (int)(scale * texture.Height / 2);
  61.             halfWidth = (int)(scale * texture.Width / 2);
  62.             origin = new Vector2(texture.Height / 2, texture.Width / 2);
  63.         }
  64.         public void Draw(SpriteBatch spriteBatch) {
  65.             spriteBatch.Draw(texture, position, null, Color.White, rotation, origin, scale, SpriteEffects.None, 1);
  66.         }
  67.         public void Clear() {
  68.             texture = null;
  69.         }
  70.  
  71.     }
  72.  
  73.     class SpriteAnimated {
  74.  
  75.         private Texture2D texture;
  76.         public Vector2 position;
  77.         private int frame;
  78.         private float frameTime;
  79.  
  80.         public int frameWidth;
  81.         public int frameHeight;
  82.         public int halfWidth;
  83.         public int halfHeight;
  84.         public Vector2 origin;
  85.  
  86.         public float scale;
  87.         public float rotation;
  88.  
  89.         private int nLines;
  90.         private int nCollumns;
  91.         private Rectangle sourceRect;
  92.  
  93.         private Animation  currentAnimation;
  94.         private Action callback;
  95.  
  96.         public struct Animation {
  97.             public int firstFrame;
  98.             public int lastFrame;
  99.             public int fps;
  100.             public bool loop;
  101.             public string name;
  102.         }
  103.         //TODO: tentar usar enum para otimizar procura de Animation
  104.         private List<Animation> animations;
  105.  
  106.         public SpriteAnimated(string textureName, Vector2 position, int frameWidth, int frameHeight) {
  107.             this.position = position;
  108.             this.frameWidth = frameWidth;
  109.             this.frameHeight = frameHeight;
  110.             scale = 1f;
  111.             texture = ResourceManager.ResourceManager.Instance.LoadAsset<Texture2D>(textureName);
  112.             nCollumns = texture.Width / frameWidth;
  113.             nLines = texture.Height / frameHeight;
  114.             halfHeight = (int)(scale * frameHeight / 2);
  115.             halfWidth = (int)(scale * frameWidth / 2);
  116.             origin = new Vector2(frameWidth / 2, frameHeight / 2);
  117.            
  118.             rotation = 0f;
  119.             SetFrame(1);
  120.             frameTime = 0f;
  121.            
  122.             sourceRect = new Rectangle(0, 0, frameWidth, frameHeight);
  123.             animations = new List<Animation>();
  124.         }
  125.  
  126.         public void Draw(SpriteBatch spriteBatch, float secondsElapsed){
  127.             if(currentAnimation.name != null){
  128.                 frameTime += secondsElapsed;
  129.                 if (frameTime >= 1 / currentAnimation.fps) {
  130.                     frameTime -= 1 / currentAnimation.fps;
  131.                     if (frame == currentAnimation.lastFrame) {
  132.                         if (currentAnimation.loop)
  133.                             frame = currentAnimation.firstFrame;
  134.                         else
  135.                             currentAnimation.name = null;
  136.                         if (callback != null) callback();
  137.                     } else if (currentAnimation.lastFrame > frame) {
  138.                         SetFrame(frame + 1);
  139.                     } else if (currentAnimation.lastFrame < frame) {
  140.                         SetFrame(frame - 1);
  141.                     }
  142.                 }
  143.             }
  144.             spriteBatch.Draw(texture, position, sourceRect, Color.White, rotation, origin, scale, SpriteEffects.None, 1);
  145.         }
  146.         /* Registra a animacao para usar depois*/
  147.         public void RegisterAnimation(string name, int firstFrame, int lastFrame, int fps, bool loop) {
  148.             Animation newAnimation = new Animation();
  149.             newAnimation.name = name;
  150.             newAnimation.firstFrame = firstFrame;
  151.             newAnimation.lastFrame = lastFrame;
  152.             newAnimation.fps = fps;
  153.             newAnimation.loop = loop;
  154.             animations.Add(newAnimation);
  155.         }
  156.         /* Reproduz uma animacao previamente registrada. Coloque callback como null caso não queira um.*/
  157.         public void Play(string name, bool forceRestart, Action callback) {
  158.             Animation animation = GetByName(name);
  159.             this.callback = callback;
  160.             if(currentAnimation.name != animation.name || forceRestart  || frame == animation.lastFrame){
  161.                 currentAnimation = animation;
  162.                 frame = currentAnimation.firstFrame;                
  163.             }
  164.         }
  165.         /* Vai e para num frame desejado */
  166.         public void GotoAndStop(int frame) {
  167.             SetFrame(frame);
  168.             currentAnimation.name = null;
  169.             frameTime = 0f;
  170.         }
  171.  
  172.         public void SetFrame(int value) {            
  173.             frame = value;
  174.             sourceRect.X = frameWidth * ((frame - 1) % nCollumns);
  175.             sourceRect.Y = frameHeight * ((frame - 1) / nCollumns);
  176.            
  177.         }
  178.         public Animation GetByName(string name) {
  179.             foreach (Animation anim in animations) {
  180.                 if (anim.name == name)
  181.                     return anim;
  182.             }
  183.             return animations[0];
  184.         }
  185.         /* Desfaz as referências */
  186.         public void Clear() {
  187.             texture = null;
  188.             callback = null;
  189.         }
  190.     }
  191. }
Add Comment
Please, Sign In to add comment