ToySpriteAnimator.h 6.58 KB
//
//  ToySpriteAnimator.h
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 01.06.2017.
//
//

#ifndef ToySpriteAnimator_h
#define ToySpriteAnimator_h

#include "json/document.h"
#include "cocos2d.h"
#include <vector>

class ToySpriteAnimator : public cocos2d::Ref {
  
    public:
    
        // tags for RepeatForever animations
        static const int RF_ANIMATION_TAG_BASE = 10;
    
        static const int RF_ANIMATION_TAG_BOUNCE_ANIMATION = 1;
        static const int RF_ANIMATION_TAG_FLOAT_ANIMATION = 2;
        static const int RF_ANIMATION_TAG_WIND_TUG_ANIMATION = 3;
        static const int RF_ANIMATION_TAG_FRAME_ANIMATION = 4;
        static const int RF_ANIMATION_TAG_JUMP_ANIMATION = 5;
        static const int RF_ANIMATION_TAG_SCALE_UP_DOWN_ANIMATION = 6;
        static const int RF_ANIMATION_TAG_ROTATE_FOREVER_ANIMATION = 7;
        static const int RF_ANIMATION_TAG_TRAIN_RUN_FOREVER_ANIMATION = 8;
    
        static const int RF_ANIMATION_TAG_RANGE_MIN = 11;
        static const int RF_ANIMATION_TAG_RANGE_MAX = 17;
    
        static void runAnimationForName(std::string animationName, std::vector<cocos2d::Node*> sprites, const rapidjson::Value* params);
    
        // standard animations
    
        // only supported for one sprite
        static void frameAnimation(cocos2d::Node* sprite, std::vector<std::string> imageFileArray, std::string folder = "");
    
        // only supported for sprites
        static void clipToAnimation(std::vector<cocos2d::Node*> sprites, std::vector<float> durations, std::vector<cocos2d::Rect> textureRects);
    
        static void fadeInAnimation(std::vector<cocos2d::Node*> sprites);
        static void fadeOutAnimation(std::vector<cocos2d::Node*> sprites);
        static void moveToAnimation(std::vector<cocos2d::Node*> sprites, float duration, std::vector<cocos2d::Point> newPositions, std::string easeOption = "");
        static void moveByAnimation(std::vector<cocos2d::Node*> sprites, float duration, cocos2d::Vec2 deltaPosition, std::string easeOption = "");
        static void rotateByAnimation(std::vector<cocos2d::Node*> sprites, float duration, float rotationAngle, std::string easeOption = "");
        static void bezierByAnimation(std::vector<cocos2d::Node*> sprites, std::vector<std::vector<float>> bezierPathsDeltaXs, std::vector<std::vector<float>> bezierPathsDeltaYs, std::vector<float> durations);
        static void simpleRotateAnimation(std::vector<cocos2d::Node*> sprites, std::vector<float> delays, std::vector<cocos2d::Point> animationAnchorPoints, std::vector<float> rotationAngles, std::vector<float> rotationDurations, std::vector<int> repeatTimes);
    
    
        // fancy animations
        static void trainRunForeverAnimation(std::vector<cocos2d::Node*> sprites, float lapDuration);
        static void crazyRotateAnimation(std::vector<cocos2d::Node*> sprites);
        static void fullRotateAnimation(std::vector<cocos2d::Node*> sprites, bool crazy, std::vector<float> durations);
        static void rotateForeverAnimation(std::vector<cocos2d::Node*> sprites, std::vector<float> oneLapDurations);
        static void scaleUpDownAnimation(std::vector<cocos2d::Node*> sprites, float upperScale = 1.1, float scaleUpDownDuration = 0.4, int times = 0);
        static void jumpAnimation(std::vector<cocos2d::Node*> sprites, std::vector<float> durations, std::vector<cocos2d::Point> deltas, std::vector<float> heights);
        static void jumpAnimation(std::vector<cocos2d::Node*> sprites, std::vector<float> durations, std::vector<cocos2d::Point> deltas, std::vector<float> heights, std::vector<int> jumps);
        static void floatAnimation(std::vector<cocos2d::Node*> sprites, std::vector<float> delays, std::vector<std::vector<float>> dxRanges, std::vector<std::vector<float>> dyRanges, float totalBezierTime = 2.4);
        static void windTugAnimation(std::vector<cocos2d::Node*> sprites, std::vector<float> skewAnglesX, std::vector<float> skewAnglesY);
        static void windTugAnimationWithRotation(std::vector<cocos2d::Node*> sprites, std::vector<float> skewAnglesX, std::vector<float> skewAnglesY, std::vector<float> delays = std::vector<float>());
        static void bounceAnimation(std::vector<cocos2d::Node*> sprites, std::vector<float> delays = std::vector<float>(), std::vector<float> rotationDurations = std::vector<float>(), std::vector<float> rotationAngles = std::vector<float>(),std::vector<float> wholeDurations = std::vector<float>(), bool roundUp = false);
        static void swipeAnimation(std::vector<cocos2d::Node*> sprites, float delayConst = 0.05, cocos2d::Point animationAnchorPoint = cocos2d::Point(0.5, 1), int rotationDir = 1, cocos2d::Vec2 moveDir = cocos2d::Vec2(-1,-1));
        static void sinusoidalMoveAnimation(std::vector<cocos2d::Node*> sprites, float duration, std::vector<cocos2d::Point> moveDestinations, std::vector<float> amplitudes, std::string easeOption = "");
    
        // rf actions handling
        static std::string mapTagToRFAction(int tag);
        static void restoreRFAction(cocos2d::Node* sprite, int actionTag);
    
        static void cleanUpAnimationData(cocos2d::Node* node);
    
        class FloatAnimationData : public cocos2d::Ref {
            public:
                float delay;
                std::vector<float> dxRange;
                std::vector<float> dyRange;
                float bezierTime;
        };
    
        class BounceAnimationData: public cocos2d::Ref {
            public:
                float delay;
                float rotationAngle;
                float rotationDuration;
                float wholeDuration;
                bool roundUp;
        };
    
        class AnimationUserData : public cocos2d::Ref {
            public:
                FloatAnimationData* floatAnimationData;
                BounceAnimationData* bounceAnimationData;
                AnimationUserData(){
                    floatAnimationData = NULL;
                    bounceAnimationData = NULL;
                }
                ~AnimationUserData(){
                    if(floatAnimationData){
                        delete floatAnimationData;
                    }
                    if(bounceAnimationData){
                        delete bounceAnimationData;
                    }
                }
                //can add mapping tag->animationdata
        };
    
    protected:
    
        static cocos2d::Action* actionWithEaseOption(cocos2d::ActionInterval* action, std::string easeOption = "");
        static void singleSpriteFloatAnimation(cocos2d::Node* sprite, float delay, std::vector<float> dxRange, std::vector<float> dyRange, float totalBezierTime = 2.4);
};

#endif /* ToySpriteAnimator_h */