summaryrefslogtreecommitdiff
path: root/src/graphics/animation.cpp
blob: 577324a7391304a3be7019a9fb3388489a1e5a4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "animation.h"

#include <cstdio>

namespace Graphics{

    // Basic animation, loops through the pictures
    Animation::Animation(){}

    Animation::Animation(const sf::Texture& texture, int count, int switchTime,
            int height = 0, int width = 0, int row = 0) :
        i_ (0),
        count_ (count),
        row_ (row),
        lastTime_ (0),
        switchTime_ (switchTime){

        sf::Vector2u size = texture_.getSize();
        if ( height == 0 ){
            h_ = size.y;
        } else {
            h_ = height;
        }

        if ( width == 0 ){
            if (size.x % count != 0){
                printf("Animation: size not divisible by %d\n", count);
            }
            w_ = size.x / count_;
        } else {
            w_ = width;
        }

        rect_ = sf::IntRect(0,row_ * h_,w_,h_); // set to first picture in
                                                   // animation
    }

    bool Animation::ticked(int deltaTime){
        lastTime_ += deltaTime;
        if ( switchTime_ > lastTime_ ){
            return false;
        }
        lastTime_ = 0;
        return true;
    }

    sf::IntRect& Animation::next(int deltaTime){
        if ( !ticked(deltaTime) ){
            return rect_;
        }
        if (i_ < count_ -1 ){
            i_ += 1;
        } else {
            i_ = 0;
        }
        rect_.left  = i_ * w_;

        return rect_;
    }

    void Animation::reset(){
        i_ = 0;
        rect_.left  = i_ * w_;
        rect_.top   = row_ * h_;
    }

    bool Animation::finished(){
        return false;
    }

    Animation::~Animation(){}

    // ONESHOT ANIMATION
    OneShotAnimation::OneShotAnimation(){}

    OneShotAnimation::OneShotAnimation(const sf::Texture& texture, int count,
            int switchTime, int height = 0, int width = 0, int row = 0):
        Animation(texture, count, switchTime, height, width, row) {
    }

    bool OneShotAnimation::finished(){
        return (i_ == count_ - 1);
    }

    sf::IntRect& OneShotAnimation::next(int deltaTime){
        if ( !ticked(deltaTime) ){
            return rect_;
        }
        if ( finished() ){
            return rect_;
        }
        if (i_ < count_ -1 ){
            i_ += 1;
        }
        rect_.left  = i_ * w_;

        return rect_;
    }

    // BOUNCING ANIMATION
    BouncingAnimation::BouncingAnimation(){}

    BouncingAnimation::BouncingAnimation(const sf::Texture& texture, int count,
            int switchTime, int height = 0, int width = 0, int row = 0):
        Animation(texture, count, switchTime, height, width, row),
        up_ (true) {
    }

    sf::IntRect& BouncingAnimation::next(int deltaTime){
        if( !ticked(deltaTime) ) {
            return rect_;
        }
        // Rewrite this and eliminate the boolean (use the count as
        // positive/negative)
        if ( up_ ) {
            if( i_ == count_ - 1){
                up_ = false;
            } else {
                i_++;
            }
        } else {
            i_--;
            if( i_ == 0 ){
                up_ = true;
            }
        }
        rect_.left  = i_ * w_;

        return rect_;
    }

    BouncingAnimation::~BouncingAnimation(){}
}