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_ENEMY_H 18 #define SOLARUS_ENEMY_H 19 20 #include "solarus/core/Common.h" 21 #include "solarus/core/Treasure.h" 22 #include "solarus/entities/EnemyAttack.h" 23 #include "solarus/entities/EnemyReaction.h" 24 #include "solarus/entities/Entity.h" 25 #include "solarus/entities/EntityPtr.h" 26 #include "solarus/entities/Explosion.h" 27 #include <map> 28 #include <string> 29 30 namespace Solarus { 31 32 /** 33 * \brief Represents an enemy. 34 * 35 * This class stores the attack and defense properties of the enemy. 36 * Every enemy sprite must have at least the following animations 37 * and with the four main directions: 38 * - "walking": used when the enemy is in its normal state 39 * - "hurt": used when it was just hurt 40 * If the enemy can be immobilized (i.e. it is sensible to attacks that immobilize it), 41 * the following animations are also required: 42 * - "immobilized": used while the enemy is immobilized 43 * - "shaking": used when the immobilized enemy is about to get back to its normal state. 44 * The animation of its sprites automatically switches between those animations 45 * depending on its current movement and the attacks it is subject to. 46 * Additional animations may be defined. 47 */ 48 class Enemy: public Entity { 49 50 public: 51 52 static constexpr EntityType ThisType = EntityType::ENEMY; 53 54 /** 55 * \brief Defines the style of sounds and animations played when an enemy 56 * is hurt or killed. 57 */ 58 enum class HurtStyle { 59 NORMAL, /**< "enemy_hurt" (and if necessary "enemy_killed") is played */ 60 MONSTER, /**< "monster_hurt" (and if necessary "enemy_killed") is played */ 61 BOSS, /**< "boss_hurt" or "boss_killed" is played and explosions are created */ 62 }; 63 64 /** 65 * \brief Defines how an enemy behaves with obstacles. 66 */ 67 enum class ObstacleBehavior { 68 NORMAL, /**< the enemy is on the ground and stops on normal obstacles */ 69 FLYING, /**< the enemy ignores holes, lava, water, etc. */ 70 SWIMMING /**< the enemy can move in water */ 71 }; 72 73 public: 74 75 // creation 76 Enemy( 77 Game& game, 78 const std::string& name, 79 int layer, 80 const Point& xy, 81 const std::string& breed, 82 const Treasure& treasure 83 ); 84 85 static EntityPtr create( 86 Game& game, 87 const std::string& breed, 88 const std::string& savegame_variable, 89 const std::string& name, 90 int layer, 91 const Point& xy, 92 int direction, 93 const Treasure& treasure 94 ); 95 96 virtual EntityType get_type() const override; 97 virtual bool is_ground_observer() const override; 98 99 virtual void notify_creating() override; 100 virtual void notify_created() override; 101 102 // Enemy properties. 103 const std::string& get_breed() const; 104 int get_damage() const; 105 void set_damage(int damage_on_hero); 106 int get_life() const; 107 void set_life(int life); 108 HurtStyle get_hurt_style() const; 109 void set_hurt_style(HurtStyle hurt_style); 110 std::string get_dying_sprite_id() const; 111 void set_dying_sprite_id(const std::string& dying_sprite_id); 112 bool get_can_attack() const; 113 void set_can_attack(bool can_attack); 114 bool is_traversable() const; 115 void set_traversable(bool traversable); 116 CollisionMode get_attacking_collision_mode() const; 117 void set_attacking_collision_mode(CollisionMode attacking_collision_mode); 118 ObstacleBehavior get_obstacle_behavior() const; 119 void set_obstacle_behavior(ObstacleBehavior obstacle_behavior); 120 bool get_pushed_back_when_hurt() const; 121 void set_pushed_back_when_hurt(bool pushed_back_when_hurt); 122 bool get_push_hero_on_sword() const; 123 void set_push_hero_on_sword(bool push_hero_on_sword); 124 bool get_can_hurt_hero_running() const; 125 void set_can_hurt_hero_running(bool can_hurt_hero_running); 126 int get_minimum_shield_needed() const; 127 void set_minimum_shield_needed(int minimum_shield_needed); 128 EnemyReaction::Reaction get_attack_consequence( 129 EnemyAttack attack, 130 const Sprite* this_sprite) const; 131 void set_attack_consequence( 132 EnemyAttack attack, 133 EnemyReaction::ReactionType reaction, 134 int life_lost = 0, 135 const ScopedLuaRef& callback = ScopedLuaRef()); 136 void set_attack_consequence_sprite( 137 const Sprite& sprite, 138 EnemyAttack attack, 139 EnemyReaction::ReactionType reaction, 140 int life_lost = 0, 141 const ScopedLuaRef& callback = ScopedLuaRef()); 142 void set_no_attack_consequences(); 143 void set_no_attack_consequences_sprite(const Sprite& sprite); 144 void set_default_attack_consequences(); 145 void set_default_attack_consequences_sprite(const Sprite& sprite); 146 147 // sprites 148 std::string get_animation() const; 149 void set_animation(const std::string& animation); 150 151 // obstacles 152 bool is_obstacle_for(Entity& other) override; 153 bool is_destructible_obstacle(Destructible& destructible) override; 154 bool is_block_obstacle(Block& block) override; 155 bool is_teletransporter_obstacle(Teletransporter& teletransporter) override; 156 bool is_raised_block_obstacle(CrystalBlock& raised_block) override; 157 bool is_stream_obstacle(Stream& stream) override; 158 159 bool is_low_wall_obstacle() const override; 160 bool is_deep_water_obstacle() const override; 161 bool is_shallow_water_obstacle() const override; 162 bool is_hole_obstacle() const override; 163 bool is_prickle_obstacle() const override; 164 bool is_lava_obstacle() const override; 165 166 // enemy state 167 void update() override; 168 void set_suspended(bool suspended) override; 169 170 void notify_enabled(bool enabled) override; 171 void notify_ground_below_changed() override; 172 void notify_collision(Entity& entity_overlapping, CollisionMode collision_mode) override; 173 void notify_collision(Entity& other_entity, Sprite& this_sprite, Sprite& other_sprite) override; 174 void notify_collision_with_explosion(Explosion& explosion, Sprite& sprite_overlapping) override; 175 void notify_collision_with_fire(Fire& fire, Sprite& sprite_overlapping) override; 176 void notify_collision_with_enemy(Enemy& other, Sprite& this_sprite, Sprite& other_sprite) override; 177 178 // attack the hero 179 void attack_hero(Hero& hero, Sprite* this_sprite); 180 void attack_stopped_by_hero_shield(); 181 182 // receive an attack 183 void restart(); 184 bool is_in_normal_state() const; 185 bool is_invulnerable() const; 186 bool is_being_hurt() const; 187 bool is_immobilized() const; 188 bool is_killed() const; 189 bool is_dying_animation_finished() const; 190 void try_hurt(EnemyAttack attack, Entity& source, Sprite* this_sprite); 191 void hurt(Entity& source, Sprite* this_sprite); 192 void kill(); 193 bool is_dying() const; 194 const Treasure& get_treasure() const; 195 void set_treasure(const Treasure& treasure); 196 void clear_treasure(); 197 198 static const std::map<EnemyAttack, std::string> attack_names; /**< Lua names of the EnemyAttack enum. */ 199 static const std::map<HurtStyle, std::string> hurt_style_names; /**< Lua names of the HurtStyle enum. */ 200 static const std::map<ObstacleBehavior, std::string> obstacle_behavior_names; /**< Lua names of the ObstacleBehavior enum. */ 201 202 private: 203 204 // hurt the enemy 205 void play_hurt_sound(); 206 bool is_sprite_finished_or_looping() const; 207 void immobilize(); 208 void stop_immobilized(); 209 void custom_attack(EnemyAttack attack, Sprite* this_sprite); 210 void notify_hurt(Entity& source, EnemyAttack attack); 211 void notify_dead(); 212 void notify_immobilized(); 213 bool is_saved() const; 214 215 // enemy characteristics 216 std::string breed; /**< breed of the enemy (determines its sprites and behavior) */ 217 218 int damage_on_hero; /**< number of heart quarters the player loses when he gets hurt by this enemy; 219 * this number is divided depending on the hero's tunic number (default: 1) */ 220 int life; /**< number of health points of the enemy (default: 1) */ 221 HurtStyle hurt_style; /**< style of sounds and animations when this enemy gets hurt 222 * (default: HURT_NORMAL) */ 223 std::string dying_sprite_id; /**< Sprite to show during the dying animation if any. */ 224 bool pushed_back_when_hurt; /**< indicates whether the enemy is pushed back when it gets hurt by the hero 225 * (default: true) */ 226 bool push_hero_on_sword; /**< indicates whether the hero is pushed back when he hurts the enemy with his 227 * sword (default: false) */ 228 bool can_hurt_hero_running; /**< indicates that the enemy can attack the hero even when the hero is running */ 229 int minimum_shield_needed; /**< shield number needed by the hero to avoid the attack of this enemy, 230 * or 0 to make the attack unavoidable (default: 0) */ 231 std::map<EnemyAttack, EnemyReaction> 232 attack_reactions; /**< how the enemy reacts to each attack 233 * (by default, it depends on the attacks) */ 234 std::string savegame_variable; /**< name of the boolean variable indicating whether this enemy is killed, 235 * or an empty string if it is not saved */ 236 bool traversable; /**< Whether this enemy can be traversed by other entities. */ 237 CollisionMode 238 attacking_collision_mode; /**< How the enemy tries to attack the hero. */ 239 ObstacleBehavior 240 obstacle_behavior; /**< Whether this enemy can fly or swim. */ 241 242 // enemy state 243 bool being_hurt; /**< indicates that the enemy is being hurt */ 244 uint32_t stop_hurt_date; /**< date when the enemy stops being hurt */ 245 bool invulnerable; /**< indicates that the enemy cannot be hurt for now */ 246 uint32_t vulnerable_again_date; /**< date when the enemy can be hurt again */ 247 bool can_attack; /**< indicates that the enemy can currently attack the hero */ 248 uint32_t can_attack_again_date; /**< date when the enemy can attack again (0 means never) */ 249 bool immobilized; /**< indicates that the enemy is currently immobilized */ 250 uint32_t start_shaking_date; /**< date when the enemy shakes */ 251 uint32_t end_shaking_date; /**< date when the enemy stops shaking and walks again */ 252 bool dying_animation_started; /**< whether the dying animation was started */ 253 254 Treasure treasure; /**< pickable item that appears when this enemy gets killed */ 255 256 // boss or mini-boss 257 bool exploding; /**< indicates that the boss is dying and some explosions are triggered on him */ 258 int nb_explosions; /**< number of explosions already played */ 259 uint32_t next_explosion_date; /**< date of the next explosion */ 260 261 }; 262 263 } 264 265 #endif 266 267