Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module c2d.sprite.animatedsprite;
- import std.conv;
- import std.stdio;
- import c2d.sprite.sprite;
- import derelict.sdl2.sdl;
- class AnimatedSprite : Sprite {
- private uint[][string] animations;
- private string currentAnimation;
- /**
- * Create AnimatedSprite object
- *
- * Params:
- * texture = sprite's texture
- * framesCount = frames count of sprite
- * animations = list of animations sequences
- */
- this(SDL_Texture* texture, uint framesCount, uint[][string] animations) {
- super(texture, framesCount);
- this.animations = animations;
- foreach (key; animations.keys) {
- currentAnimation = key;
- break;
- }
- }
- /**
- * Set current animation
- *
- * Params:
- * animation = new animation name
- *
- * Returns:
- * true - if animation successfully changed
- * false - otherwise
- */
- public bool setAnimation(string animation) {
- if (animation in animations) {
- currentAnimation = animation;
- setCurrentFrame(animations[currentAnimation][0]);
- }
- return false;
- }
- /**
- * Move to next frame
- *
- * Returns:
- * true - if end reached and go to first frame
- * false - otherwise
- */
- override public bool nextFrame() {
- writeln("debug(nextFrame()): " ~ currentAnimation ~ "; " ~ to!string(animations[currentAnimation].length));
- if (currentFrame >= animations[currentAnimation].length - 1) {
- currentFrame = 0;
- return true;
- }
- currentFrame++;
- return false;
- }
- /**
- * Return frames count of current animation
- *
- * Returns:
- * Frames count of current animation
- */
- override public uint getFramesCount() {
- return to!uint(animations[currentAnimation].length);
- }
- /**
- * Return current frame SDL_Rect
- *
- * Returns:
- * Current frame SDL_Rect
- */
- override public SDL_Rect getFrameRect() {
- SDL_Rect fr;
- writeln("debug(getFrameRect()): " ~ currentAnimation ~ "; " ~ to!string(currentFrame));
- fr.x = animations[currentAnimation][currentFrame] * frameWidth;
- fr.y = 0;
- fr.w = frameWidth;
- fr.h = frameHeight;
- return fr;
- }
- /**
- * Set current frame
- *
- * Params:
- * f = new frame number
- *
- * Returns:
- * true - if new frame number successfully set
- * false - otherwise
- */
- override public bool setCurrentFrame(uint f) {
- if (f >= animations[currentAnimation].length) {
- return false;
- }
- currentFrame = f;
- return true;
- }
- /**
- * Return current frame
- *
- * Returns:
- * Current frame
- */
- override public uint getCurrentFrame() {
- return animations[currentAnimation][currentFrame];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement