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_H 18 #define SOLARUS_HERO_H 19 20 #include "solarus/core/Point.h" 21 #include "solarus/entities/EnemyAttack.h" 22 #include "solarus/entities/Entity.h" 23 #include "solarus/entities/Ground.h" 24 #include "solarus/hero/HeroSprites.h" 25 #include <memory> 26 #include <string> 27 28 namespace Solarus { 29 30 class CustomState; 31 class CarriedObject; 32 class Equipment; 33 class EquipmentItem; 34 class EquipmentItemUsage; 35 class HeroSprites; 36 class HeroState; 37 class InputEvent; 38 class Map; 39 class Treasure; 40 41 /** 42 * \brief The hero entity. 43 * 44 * This class represents the hero. It coordinates his position on the map and his state. 45 * The hero is animated by several sprites that are handled by the HeroSprites class. 46 */ 47 class Hero: public Entity { 48 49 public: 50 51 static constexpr EntityType ThisType = EntityType::HERO; 52 53 /** 54 * \name Creation and destruction. 55 */ 56 explicit Hero(Equipment& equipment); 57 58 /** 59 * \name Features. 60 * 61 * These functions, required by Entity, indicate 62 * the main properties of this type of entity. 63 */ 64 EntityType get_type() const override; 65 66 /** 67 * \name Game loop. 68 * 69 * Functions called by the game loop. 70 */ 71 void update() override; 72 void built_in_draw(Camera& camera) override; 73 void set_suspended(bool suspended) override; 74 bool notify_input(const InputEvent& event); 75 void notify_command_pressed(GameCommand command) override; 76 void notify_command_released(GameCommand command) override; 77 78 /** 79 * \name Sprites. 80 * 81 * Functions relative to the sprites. 82 * The sprites are managed and drawn by the class HeroSprites. 83 */ 84 const HeroSprites& get_hero_sprites() const; 85 HeroSprites& get_hero_sprites(); 86 int get_animation_direction() const; 87 void set_animation_direction(int direction); 88 bool is_animation_finished() const; 89 void rebuild_equipment(); 90 bool is_shadow_visible() const; 91 92 /** 93 * \name Changing map. 94 * 95 * Functions called when the player goes to another map. 96 */ 97 void notify_creating() override; 98 void notify_map_starting(Map& map, const std::shared_ptr<Destination>& destination) override; 99 void notify_map_started(Map& map, const std::shared_ptr<Destination>& destination) override; 100 void notify_map_opening_transition_finishing(Map& map, const std::shared_ptr<Destination>& destination) override; 101 void notify_map_opening_transition_finished(Map& map, const std::shared_ptr<Destination>& destination) override; 102 void notify_map_finished() override; 103 void notify_tileset_changed() override; 104 void place_on_destination(Map& map, const Rectangle& previous_map_location); 105 106 /** 107 * \name Position. 108 * 109 * These function provide information about the position of 110 * the hero relative to other entities, and about 111 * what is in front of him (we call this the "facing point"). 112 */ 113 Point get_facing_point() const override; 114 void notify_facing_entity_changed(Entity* facing_entity) override; 115 bool is_facing_obstacle(); 116 bool is_facing_point_on_obstacle(); 117 bool is_facing_direction4(int direction4) const; 118 bool is_facing_direction8(int direction8) const; 119 bool is_on_raised_blocks() const; 120 std::shared_ptr<const Stairs> get_stairs_overlapping() const; 121 122 /** 123 * \name Movement. 124 * 125 * Information about the movement of the hero. 126 * The hero is controlled by the player most of the time, 127 * and sometimes he is controlled automatically or he 128 * cannot move. 129 */ 130 bool can_control_movement() const; 131 bool can_control_direction() const; 132 int get_normal_walking_speed() const; 133 void set_normal_walking_speed(int normal_walking_speed); 134 int get_walking_speed() const; 135 void set_walking_speed(int walking_speed); 136 int get_wanted_movement_direction8() const; 137 int get_real_movement_direction8(); 138 bool is_moving_towards(int direction4) const; 139 bool is_direction_locked() const; 140 void notify_position_changed() override; 141 void notify_layer_changed() override; 142 void notify_obstacle_reached() override; 143 void notify_movement_started() override; 144 void notify_movement_changed() override; 145 void notify_movement_finished() override; 146 void reset_movement(); 147 148 /** 149 * \name Ground under the hero. 150 * 151 * Functions to handle the ground of the tiles under the hero. 152 * Depending on the kind of ground, a special sprite may be displayed under him (grass, shallow water) 153 * or something bad can happen (deep water, holes). 154 */ 155 bool is_ground_visible() const; 156 bool is_ground_observer() const override; 157 void notify_ground_below_changed() override; 158 const Point& get_last_solid_ground_coords() const; 159 int get_last_solid_ground_layer() const; 160 ScopedLuaRef make_solid_ground_callback( 161 const Point& xy, int layer) const; 162 const ScopedLuaRef& get_target_solid_ground_callback() const; 163 void set_target_solid_ground_callback(const ScopedLuaRef& callback); 164 void reset_target_solid_ground_callback(); 165 166 /** 167 * \name Obstacles. 168 * 169 * Information about what is considered as an obstacle for the hero. 170 */ 171 bool is_obstacle_for(Entity& other) override; 172 bool is_traversable_obstacle() const override; 173 bool is_wall_obstacle() const override; 174 bool is_low_wall_obstacle() const override; 175 bool is_grass_obstacle() const override; 176 bool is_shallow_water_obstacle() const override; 177 bool is_deep_water_obstacle() const override; 178 bool is_hole_obstacle() const override; 179 bool is_ice_obstacle() const override; 180 bool is_lava_obstacle() const override; 181 bool is_prickle_obstacle() const override; 182 bool is_ladder_obstacle() const override; 183 bool is_block_obstacle(Block& block) override; 184 bool is_teletransporter_obstacle(Teletransporter& teletransporter) override; 185 bool is_hero_obstacle(Hero& hero) override; 186 bool is_stream_obstacle(Stream& stream) override; 187 bool is_stairs_obstacle(Stairs& stairs) override; 188 bool is_sensor_obstacle(Sensor& sensor) override; 189 bool is_switch_obstacle(Switch& sw) override; 190 bool is_raised_block_obstacle(CrystalBlock& raised_block) override; 191 bool is_crystal_obstacle(Crystal& crystal) override; 192 bool is_npc_obstacle(Npc& npc) override; 193 bool is_door_obstacle(Door& block) override; 194 bool is_enemy_obstacle(Enemy& enemy) override; 195 bool is_jumper_obstacle(Jumper& jumper, const Rectangle& candidate_position) override; 196 bool is_destructible_obstacle(Destructible& destructible) override; 197 bool is_separator_obstacle(Separator& separator, const Rectangle& candidate_position) override; 198 199 /** 200 * \name Collisions. 201 * 202 * Handle collisions between the hero and other entities. 203 */ 204 void check_position() override; 205 void notify_collision_with_destructible(Destructible& destructible, CollisionMode collision_mode) override; 206 void notify_collision_with_enemy(Enemy& enemy, CollisionMode) override; 207 void notify_collision_with_enemy(Enemy& enemy, Sprite& this_sprite, Sprite& enemy_sprite) override; 208 void notify_collision_with_teletransporter(Teletransporter& teletransporter, CollisionMode collision_mode) override; 209 void notify_collision_with_stream(Stream& stream, int dx, int dy) override; 210 void notify_collision_with_stairs(Stairs& stairs, CollisionMode collision_mode) override; 211 void notify_collision_with_jumper(Jumper& jumper, CollisionMode collision_mode) override; 212 void notify_collision_with_sensor(Sensor& sensor, CollisionMode collision_mode) override; 213 void notify_collision_with_switch(Switch& sw, CollisionMode collision_mode) override; 214 void notify_collision_with_switch(Switch& sw, Sprite &sprite_overlapping) override; 215 void notify_collision_with_crystal(Crystal& crystal, CollisionMode collision_mode) override; 216 void notify_collision_with_crystal(Crystal& crystal, Sprite &sprite_overlapping) override; 217 void notify_collision_with_chest(Chest& chest) override; 218 void notify_collision_with_block(Block& block) override; 219 void notify_collision_with_separator(Separator& separator, CollisionMode collision_mode) override; 220 void notify_collision_with_explosion(Explosion& explosion, Sprite& sprite_overlapping) override; 221 void avoid_collision(Entity& entity, int direction); 222 bool is_striking_with_sword(Entity& entity) const; 223 224 /** 225 * \name Enemies. 226 * 227 * Attacking enemies or getting hurt by them. 228 */ 229 void notify_attacked_enemy( 230 EnemyAttack attack, 231 Enemy& victim, 232 Sprite* victim_sprite, 233 const EnemyReaction::Reaction& result, 234 bool killed 235 ) override; 236 int get_sword_damage_factor() const; 237 bool is_invincible() const; 238 void set_invincible(bool invincible, uint32_t duration); 239 bool can_be_hurt(Entity* attacker) const; 240 void hurt( 241 Entity& source, 242 Sprite* source_sprite, 243 int life_points 244 ); 245 void hurt(const Point& source_xy, int life_points); 246 void hurt(int life_points); 247 void notify_game_over_finished(); 248 249 /** 250 * \name State. 251 * 252 * These functions provide information about the hero's internal state 253 * and allow to start actions which may modify this state. 254 * Actions can be triggered by equipment items, entities or scripts. 255 */ 256 bool is_free() const; 257 bool is_using_item() const; 258 EquipmentItemUsage& get_item_being_used(); 259 std::shared_ptr<CarriedObject> get_carried_object(); 260 bool is_brandishing_treasure() const; 261 bool is_grabbing_or_pulling() const; 262 bool is_moving_grabbed_entity() const; 263 void notify_grabbed_entity_collision(); 264 265 void start_grass(); 266 void start_shallow_water(); 267 void start_deep_water(); 268 void start_hole(); 269 void start_ice(); 270 void start_lava(); 271 void start_prickle(uint32_t delay); 272 273 void start_next_state(); 274 void start_free(); 275 void start_free_carrying_loading_or_running(); 276 void start_treasure( 277 const Treasure& treasure, 278 const ScopedLuaRef& callback_ref 279 ); 280 void start_forced_walking(const std::string& path, bool loop, bool ignore_obstacles); 281 void start_jumping(int direction8, int distance, bool ignore_obstacles, 282 bool with_sound); 283 void start_frozen(); 284 void start_victory(const ScopedLuaRef& callback_ref); 285 void start_lifting(const std::shared_ptr<CarriedObject>& item_to_lift); 286 void start_running(); 287 void start_pushing(); 288 void start_grabbing(); 289 void start_pulling(); 290 bool can_pick_treasure(EquipmentItem& item); 291 bool can_avoid_teletransporter(const Teletransporter& teletransporter) const; 292 bool can_run() const; 293 bool can_push() const; 294 bool can_grab() const; 295 bool can_pull() const; 296 bool can_interact_with_npc(Npc& npc); 297 bool can_use_shield() const; 298 bool can_start_sword() const; 299 void start_sword(); 300 void start_sword_loading(int spin_attack_delay); 301 bool can_start_item(EquipmentItem& item); 302 void start_item(EquipmentItem& item); 303 void start_boomerang(int max_distance, int speed, 304 const std::string& tunic_preparing_animation, 305 const std::string& sprite_name); 306 void start_bow(); 307 void start_hookshot(); 308 void start_back_to_solid_ground(bool use_specified_position, 309 uint32_t end_delay = 0, bool with_sound = true); 310 void start_state_from_ground(); 311 void start_custom_state(const std::shared_ptr<CustomState>& custom_state); 312 313 private: 314 315 // state 316 class PlayerMovementState; /**< base class for states whose movement is controlled by the player */ 317 class FreeState; /**< the hero is free to move (stopped or walking) and can interact with entities */ 318 class CarryingState; /**< the hero can walk but he is carrying a pot or a bush */ 319 class SwordLoadingState; /**< the hero can walk but his sword is loading for a spin attack */ 320 class SwimmingState; /**< the hero is swimming in deep water */ 321 class PushingState; /**< the hero is trying to push an obstacle */ 322 class SwordTappingState; /**< the hero is tapping his sword on a wall */ 323 class PullingState; /**< the hero is pulling an object */ 324 class GrabbingState; /**< the hero is grabbing an object and can pull it */ 325 class SwordSwingingState; /**< the hero is swinging his sword */ 326 class SpinAttackState; /**< the hero is releasing a spin attack */ 327 class LiftingState; /**< the hero is lifting an destroyable item (a pot, a bush, etc.) */ 328 class TreasureState; /**< the hero is brandishing a treasure */ 329 class RunningState; /**< the hero is running */ 330 class ForcedWalkingState; /**< the hero is walking with a predetermined path */ 331 class JumpingState; /**< the hero is jumping */ 332 class HurtState; /**< the hero is hurt */ 333 class PlungingState; /**< the hero is plunging into water */ 334 class FallingState; /**< the hero is falling into a hole */ 335 class BackToSolidGroundState; /**< the hero is getting back to solid ground (e.g. after he drowned 336 * in deep water or falled into a hole) */ 337 class StairsState; /**< the hero is being moved by stairs */ 338 class VictoryState; /**< the hero is make a victory sequence with his sword */ 339 class UsingItemState; /**< the hero is currently using an equipment item */ 340 class BoomerangState; /**< the hero is shooting a boomerang */ 341 class HookshotState; /**< the hero is throwing his hookshot */ 342 class BowState; /**< the hero is shooting an arrow with a bow */ 343 class FrozenState; /**< the hero cannot move for various possible reasons, 344 * including an instruction from the script */ 345 346 // position 347 void place_on_map(Map& map); 348 void update_direction(); 349 void update_movement(); 350 void try_snap_to_facing_entity(); 351 void apply_additional_ground_movement(); 352 std::shared_ptr<Teletransporter> get_delayed_teletransporter(); 353 354 // ground 355 void update_ground_effects(); 356 void update_ice(); 357 void stop_ice_movement(); 358 359 // life 360 void check_gameover(); 361 void update_invincibility(); 362 363 // state 364 bool invincible; /**< Whether the hero is temporarily invincible. */ 365 uint32_t end_invincible_date; /**< When stopping the invincibility (0 means infinite). */ 366 367 // sprites 368 std::unique_ptr<HeroSprites> 369 sprites; /**< the hero's sprites (note that we don't use the sprites structure from Entity) */ 370 371 // position 372 int normal_walking_speed; /**< speed when normally walking */ 373 int walking_speed; /**< current walking speed (possibly changed by the ground) */ 374 375 // state specific 376 std::shared_ptr<Teletransporter> 377 delayed_teletransporter; /**< a teletransporter that will be activated when the hero finishes 378 * a special behavior, such as falling into a hole or walking on stairs */ 379 bool on_raised_blocks; /**< indicates that the hero is currently on 380 * raised crystal blocks */ 381 382 // ground 383 Point last_solid_ground_coords; /**< coordinates of the last hero position on a ground 384 * where he can walk (e.g. before jumping or falling into a hole) */ 385 int last_solid_ground_layer; /**< layer of the last hero position on a solid ground */ 386 ScopedLuaRef 387 target_solid_ground_callback; /**< Function that gives the position where the hero will go back if he falls 388 * into a hole (or some other bad ground), or an empty ref to indicate 389 * that the hero will just return to the last solid ground coordinates. */ 390 uint32_t next_ground_date; /**< when something will happen with the ground next time (a sound or a movement) */ 391 uint32_t next_ice_date; /**< when recomputing the additional movement on ice */ 392 int ice_movement_direction8; /**< wanted movement direction a while ago */ 393 Point ground_dxy; /**< additional movement with special ground (hole or ice) */ 394 395 }; 396 397 } 398 399 #endif 400 401