1
/*
2
 *  The Mana World
3
 *  Copyright (C) 2004  The Mana World Development Team
4
 *
5
 *  This file is part of The Mana World.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21
22
#ifndef ANIMATEDSPRITE_H
23
#define ANIMATEDSPRITE_H
24
25
#include "resources/spritedef.h"
26
27
#include <map>
28
#include <string>
29
30
class Animation;
31
class Graphics;
32
struct Frame;
33
34
/**
35
 * Animates a sprite by adding playback state.
36
 */
37
class AnimatedSprite
38
{
39
    public:
40
        /**
41
         * Constructor.
42
         * @param sprite the sprite to animate
43
         */
44
        AnimatedSprite(SpriteDef *sprite);
45
46
        /**
47
         * An helper function, which will request the sprite to animate
48
         * from the resource manager.
49
         *
50
         * @param filename the file of the sprite to animate
51
         * @param variant  the sprite variant
52
         */
53
        static AnimatedSprite *load(const std::string &filename,
54
                                    int variant = 0);
55
56
        /**
57
         * Destructor.
58
         */
59
        ~AnimatedSprite();
60
61
        /**
62
         * Resets the animated sprite.
63
         */
64
        void reset();
65
66
        /**
67
         * Plays an action using the current direction
68
         */
69
        void play(SpriteAction action);
70
71
        /**
72
         * Inform the animation of the passed time so that it can output the
73
         * correct animation frame.
74
         */
75
        void update(int time);
76
77
        /**
78
         * Draw the current animation frame at the coordinates given in screen
79
         * pixels.
80
         */
81
        bool draw(Graphics* graphics, int posX, int posY) const;
82
83
        /**
84
         * gets the width in pixels of the image of the current frame
85
         */
86
        int getWidth() const;
87
88
        /**
89
         * gets the height in pixels of the image of the current frame
90
         */
91
        int getHeight() const;
92
93
        /**
94
         * Sets the direction.
95
         */
96
        void setDirection(SpriteDirection direction);
97
98
        /**
99
         * Sets the alpha value of the animated sprite
100
         */
101
        void setAlpha(float alpha)
102
        { mAlpha = alpha; }
103
104
        /**
105
         * Returns the current alpha opacity of the animated sprite.
106
         */
107
        virtual float getAlpha() const
108
        { return mAlpha; }
109
110
    private:
111
        bool updateCurrentAnimation(unsigned int dt);
112
113
        SpriteDirection mDirection;    /**< The sprite direction. */
114
        int mLastTime;                 /**< The last time update was called. */
115
116
        int mFrameIndex;               /**< The index of the current frame. */
117
        int mFrameTime;                /**< The time since start of frame. */
118
119
        SpriteDef *mSprite;            /**< The sprite definition. */
120
        Action *mAction;               /**< The currently active action. */
121
        Animation *mAnimation;         /**< The currently active animation. */
122
        Frame *mFrame;                 /**< The currently active frame. */
123
        float mAlpha;                  /**< The alpha opacity used to draw */
124
};
125
126
#endif