Advertisement
Guest User

Untitled

a guest
Mar 24th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.57 KB | None | 0 0
  1. module c2d.sprite.animatedsprite;
  2.  
  3. import std.conv;
  4. import std.stdio;
  5.  
  6. import c2d.sprite.sprite;
  7. import derelict.sdl2.sdl;
  8.  
  9. class AnimatedSprite : Sprite {
  10.     private uint[][string] animations;
  11.     private string currentAnimation;
  12.  
  13.     /**
  14.      * Create AnimatedSprite object
  15.      *
  16.      * Params:
  17.      *      texture = sprite's texture
  18.      *      framesCount = frames count of sprite
  19.      *      animations = list of animations sequences
  20.      */
  21.     this(SDL_Texture* texture, uint framesCount, uint[][string] animations) {
  22.         super(texture, framesCount);
  23.         this.animations = animations;
  24.  
  25.         foreach (key; animations.keys) {
  26.             currentAnimation = key;
  27.             break;
  28.         }
  29.     }
  30.  
  31.     /**
  32.      * Set current animation
  33.      *
  34.      * Params:
  35.      *      animation = new animation name
  36.      *
  37.      * Returns:
  38.      *      true - if animation successfully changed
  39.      *      false - otherwise
  40.      */
  41.     public bool setAnimation(string animation) {
  42.         if (animation in animations) {
  43.             currentAnimation = animation;
  44.             setCurrentFrame(animations[currentAnimation][0]);
  45.         }
  46.         return false;
  47.     }
  48.  
  49.     /**
  50.      * Move to next frame
  51.      *
  52.      * Returns:
  53.      *      true - if end reached and go to first frame
  54.      *      false - otherwise
  55.      */
  56.     override public bool nextFrame() {
  57.         writeln("debug(nextFrame()): " ~ currentAnimation ~ "; " ~ to!string(animations[currentAnimation].length));
  58.         if (currentFrame >= animations[currentAnimation].length - 1) {
  59.             currentFrame = 0;
  60.             return true;
  61.         }
  62.         currentFrame++;
  63.         return false;
  64.     }
  65.  
  66.     /**
  67.      * Return frames count of current animation
  68.      *
  69.      * Returns:
  70.      *      Frames count of current animation
  71.      */
  72.     override public uint getFramesCount() {
  73.         return to!uint(animations[currentAnimation].length);
  74.     }
  75.  
  76.     /**
  77.      * Return current frame SDL_Rect
  78.      *
  79.      * Returns:
  80.      *      Current frame SDL_Rect
  81.      */
  82.     override public SDL_Rect getFrameRect() {
  83.         SDL_Rect fr;
  84.         writeln("debug(getFrameRect()): " ~ currentAnimation ~ "; " ~ to!string(currentFrame));
  85.         fr.x = animations[currentAnimation][currentFrame] * frameWidth;
  86.         fr.y = 0;
  87.         fr.w = frameWidth;
  88.         fr.h = frameHeight;
  89.        
  90.         return fr;
  91.     }
  92.  
  93.     /**
  94.      * Set current frame
  95.      *
  96.      * Params:
  97.      *      f = new frame number
  98.      *
  99.      * Returns:
  100.      *      true - if new frame number successfully set
  101.      *      false - otherwise
  102.      */
  103.     override public bool setCurrentFrame(uint f) {
  104.         if (f >= animations[currentAnimation].length) {
  105.             return false;
  106.         }
  107.         currentFrame = f;
  108.         return true;
  109.     }
  110.    
  111.     /**
  112.      * Return current frame
  113.      *
  114.      * Returns:
  115.      *      Current frame
  116.      */
  117.     override public uint getCurrentFrame() {
  118.         return animations[currentAnimation][currentFrame];
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement