Advertisement
konalisp

animate.cs

Mar 1st, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1.     class AnimationType {
  2.         public float timerDefault = 100;
  3.         public Sprite[] Assets { set; get; }
  4.         public float[,] Frames { set; get; }
  5.         public Vector2f _position;
  6.         public Vector2f Position { set { _position = value; } get { return _position; } }
  7.         public int _currentFrame = 0;
  8.         public int CurrentFrame { set { _currentFrame = value; } get { return _currentFrame; } }
  9.         public float _timer = 0;
  10.         public float Timer { set { _timer = value; } get { return _timer; } }
  11.         public bool _sorted = false;
  12.         public bool Sorted { set { _sorted = value; } get { return _sorted; } }
  13.        
  14.         public AnimationType() {
  15.            
  16.         }
  17.        
  18.         public void SetPosition(Vector2f pos) {
  19.             this.Position = pos;
  20.             foreach (Sprite s in this.Assets) {
  21.                 s.Position = this.Position;
  22.             }
  23.         }
  24.        
  25.         public void Animate() {
  26.             //set the sprite's position
  27.             SetPosition(this.Position);
  28.             //move the timer foreward.
  29.             this.Timer += this.Frames[this.CurrentFrame, 2] * Globals.DeltaTime.AsSeconds();
  30.             //if the timer moves past the limit, reset it and go to the next frame.
  31.             if (this.Timer >= timerDefault) { this.Timer = 0; this.CurrentFrame += 1; }
  32.             //if the frame counter goes past the limit, reset it. The animation is done.
  33.             if (this.CurrentFrame > this.Frames.GetUpperBound(0)) { this.CurrentFrame = 0; }
  34.         }
  35.        
  36.         public void Render(RenderWindow w) {
  37.             w.Draw(this.Assets[this.CurrentFrame]);
  38.         }
  39.     }
  40.    
  41.     class Animation {
  42.         public static AnimationType testAnimation = new AnimationType() {
  43.             Position = new Vector2f(100,100),
  44.             Assets = new Sprite[4] {
  45.                 new Sprite(new Texture(@"res\ts1.png")),
  46.                 new Sprite(new Texture(@"res\ts2.png")),
  47.                 new Sprite(new Texture(@"res\ts3.png")),
  48.                 new Sprite(new Texture(@"res\ts4.png")),
  49.             },
  50.             Frames = new float[4,3] {
  51.                 {0, 0, 200}, //order, sprite index, duration
  52.                 {1, 1, 200},
  53.                 {2, 2, 200},
  54.                 {3, 3, 200},
  55.             },
  56.            
  57.             Sorted = true,
  58.         };
  59.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement