1 /**
2  * Copyright (C) 2006-2019 Christopho, Solarus - http://www.solarus-games.org
3  *
4  * Solarus is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Solarus is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef SOLARUS_HERO_SPRITES_H
18 #define SOLARUS_HERO_SPRITES_H
19 
20 #include "solarus/core/Common.h"
21 #include "solarus/core/Rectangle.h"
22 #include "solarus/entities/Ground.h"
23 #include "solarus/graphics/SpritePtr.h"
24 #include "solarus/lua/ScopedLuaRef.h"
25 #include <memory>
26 #include <string>
27 
28 namespace Solarus {
29 
30 class CarriedObject;
31 class Equipment;
32 class Hero;
33 class LuaContext;
34 class Sprite;
35 
36 /**
37  * \brief Manages the animations of the hero's main sprites.
38  *
39  * This class handles the animations of the hero's main sprites: the tunic,
40  * the sword, the sword stars, the shield, the shadow and the ground.
41  * It also handles the sprite-dependent sounds such as the sword sound.
42  *
43  * This class does not know anything about the hero's internal state:
44  * it is the role of the state to call this class to display the appropriate animation.
45  */
46 class HeroSprites {
47 
48   public:
49 
50     HeroSprites(Hero& hero, Equipment& equipment);
51 
52     void update();
53     void draw_on_map();
54     void set_suspended(bool suspended);
55     void rebuild_equipment();
56     void notify_creating();
57     void notify_tileset_changed();
58 
59     const std::string& get_tunic_sprite_id() const;
60     void set_tunic_sprite_id(const std::string& sprite_id);
61     const std::string& get_sword_sprite_id() const;
62     void set_sword_sprite_id(const std::string& sprite_id);
63     const std::string& get_sword_stars_sprite_id() const;
64     void set_sword_stars_sprite_id(const std::string& sprite_id);
65     const std::string& get_sword_sound_id() const;
66     void set_sword_sound_id(const std::string& sound_id);
67     const std::string& get_shield_sprite_id() const;
68     void set_shield_sprite_id(const std::string& sprite_id);
69 
70     void blink(uint32_t duration);
71     void stop_blinking();
72     bool is_blinking() const;
73     bool is_walking() const;
74     void set_clipping_rectangle(
75         const Rectangle& clipping_rectangle = Rectangle()
76     );
77 
78     int get_animation_direction(
79         int keys_direction, int real_movement_direction) const;
80     int get_animation_direction() const;
81     int get_animation_direction8() const;
82     void set_animation_direction(int direction);
83     void set_animation_direction8(int direction);
84     void restart_animation();
85     bool is_animation_finished() const;
86     int get_current_frame() const;
87     void save_animation_direction();
88     void restore_animation_direction();
89     void set_ignore_suspend(bool ignore_suspend);
90 
91     bool has_tunic_animation(const std::string& animation) const;
92     const std::string& get_tunic_animation() const;
93 
94     void set_animation_stopped_common();
95     void set_animation_stopped_normal();
96     void set_animation_stopped_sword_loading();
97     void set_animation_stopped_carrying();
98     void set_animation_stopped_swimming();
99     void set_animation_walking_common();
100     void set_animation_walking_normal();
101     void set_animation_walking_sword_loading();
102     void set_animation_walking_carrying();
103     void set_animation_swimming_slow();
104     void set_animation_swimming_fast();
105     void set_animation_walking_diagonal(int direction8);
106     void set_animation_sword();
107     void play_sword_sound();
108     void set_animation_sword_tapping();
109     void set_animation_spin_attack();
110     void set_animation_super_spin_attack();
111     void set_animation_grabbing();
112     void set_animation_pulling();
113     void set_animation_pushing();
114     void set_animation_lifting();
115     void set_animation_jumping();
116     void set_animation_hurt();
117     void set_animation_falling();
118     void set_animation_brandish();
119     void set_animation_victory();
120     void set_animation_prepare_running();
121     void set_animation_running();
122     void set_animation_boomerang(const std::string& tunic_preparing_animation);
123     void set_animation(const std::string& animation);
124     void set_animation(
125         const std::string& animation,
126         const ScopedLuaRef& callback_ref
127     );
128 
129     void create_ground(Ground grond);
130     void destroy_ground();
131     void play_ground_sound();
132 
133     void set_lifted_item(const std::shared_ptr<CarriedObject>& lifted_item);
134 
135   private:
136 
137     bool is_visible() const;
138     bool is_sword_visible() const;
139     bool is_sword_stars_visible() const;
140     bool is_shield_visible() const;
141     bool is_trail_visible() const;
142     bool is_ground_visible() const;
143 
144     std::string get_default_tunic_sprite_id() const;
145     std::string get_default_sword_sprite_id() const;
146     std::string get_default_sword_sound_id() const;
147     std::string get_default_shield_sprite_id() const;
148 
149     void stop_displaying_sword();
150     void stop_displaying_sword_stars();
151     void stop_displaying_shield();
152     void stop_displaying_trail();
153 
154     void set_tunic_animation(const std::string& animation);
155     void set_tunic_animation(const std::string& animation, const ScopedLuaRef& callback_ref);
156 
157     LuaContext& get_lua_context();
158 
159     Hero& hero;                             /**< The hero. */
160     Equipment& equipment;                   /**< Equipment of the player. */
161 
162     // Tunic.
163     std::string tunic_sprite_id;            /**< Animation set used for the tunic.
164                                              * By default, "hero/tunicX" where X is the tunic level. */
165     bool has_default_tunic_sprite;          /**< Whether tunic_sprite_id has the defaut value. */
166     SpritePtr tunic_sprite;                 /**< sprite of the current tunic */
167 
168     // Sword.
169     std::string sword_sprite_id;            /**< Animation set used for the sword.
170                                              * An empty string means no sword sprite.
171                                              * By default, "hero/swordX" where X is the sword level. */
172     bool has_default_sword_sprite;          /**< Whether sword_sprite_id has the defaut value. */
173     SpritePtr sword_sprite;                 /**< Current sword sprite. */
174     std::string sword_stars_sprite_id;      /**< Animation set used for the sword stars.
175                                              * An empty string means no sword stars sprite. */
176     SpritePtr sword_stars_sprite;           /**< Stars running along the sword when the sword is loading. */
177 
178     std::string sword_sound_id;             /**< Sound played when using the sword.
179                                              * By default, "swordX" where X is the sword level. */
180     bool has_default_sword_sound;           /**< Whether sword_sound_id has the defaut value. */
181 
182     // Shield.
183     std::string shield_sprite_id;           /**< Animation set used for the shield.
184                                              * An empty string means no shield sprite.
185                                              * By default, "hero/shieldX" where X is the shield level. */
186     bool has_default_shield_sprite;         /**< Whether shield_sprite_id has the defaut value. */
187     SpritePtr shield_sprite;                /**< Current shield sprite. */
188 
189     // Other sprites.
190     SpritePtr shadow_sprite;                /**< shadow of the hero, only in specific states (most of the time
191                                              * the shadow is with the tunic sprite) */
192     SpritePtr ground_sprite;                /**< ground displayed under the hero (e.g. grass or shallow water) */
193     SpritePtr trail_sprite;                 /**< trail of dust that the hero lets behind him (e.g. when running) */
194 
195     std::string ground_sound_id;            /**< sound id of the current ground displayed under the hero */
196 
197     // State.
198     static const int
199         animation_directions[8][2];         /**< possible directions of the animation for each movement direction */
200 
201     int animation_direction_saved;          /**< direction of the hero's sprites, saved before
202                                               * showing a sprite animation having only one direction */
203     uint32_t when_suspended;                /**< date when the game was suspended */
204 
205     bool blinking;                          /**< Whether the hero's sprites are blinking. */
206     uint32_t end_blink_date;                /**< When the hero's sprites stop blinking.
207                                              * 0 means infinite. */
208     bool walking;                           /**< stopped or walking? */
209     Rectangle clipping_rectangle;           /**< when drawing the sprites onto a map, indicates an area of the map to be restricted to
210                                              * (usually, the whole map is considered and this rectangle's values are all 0) */
211     std::shared_ptr<CarriedObject>
212         lifted_item;                        /**< if not nullptr, an item to display above the hero */
213 
214     ScopedLuaRef animation_callback_ref;    /**< Lua ref of a function to call when a custom animation ends. */
215 };
216 
217 }
218 
219 #endif
220 
221