2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program 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.
9 // This program 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.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef HEADER_SUPERTUX_BADGUY_BADGUY_HPP
18 #define HEADER_SUPERTUX_BADGUY_BADGUY_HPP
20 #include "object/moving_sprite.hpp"
21 #include "supertux/direction.hpp"
22 #include "supertux/physic.hpp"
23 #include "supertux/timer.hpp"
28 /** Base class for moving sprites that can hurt the Player. */
29 class BadGuy : public MovingSprite
32 BadGuy(const Vector& pos, const std::string& sprite_name, int layer = LAYER_OBJECTS);
33 BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer = LAYER_OBJECTS);
34 BadGuy(const Reader& reader, const std::string& sprite_name, int layer = LAYER_OBJECTS);
36 /** Called when the badguy is drawn. The default implementation
37 simply draws the badguy sprite on screen */
38 virtual void draw(DrawingContext& context);
40 /** Called each frame. The default implementation checks badguy
41 state and calls active_update and inactive_update */
42 virtual void update(float elapsed_time);
44 /** Called when a collision with another object occurred. The
45 default implementation calls collision_player, collision_solid,
46 collision_badguy and collision_squished */
47 virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
49 /** Called when a collision with tile with special attributes
51 virtual void collision_tile(uint32_t tile_attributes);
53 /** Set the badguy to kill/falling state, which makes him falling of
54 the screen (his sprite is turned upside-down) */
55 virtual void kill_fall();
57 /** Call this, if you use custom kill_fall() or kill_squashed(GameObject& object) */
58 virtual void run_dead_script();
60 /** True if this badguy can break bricks or open bonusblocks in his
62 virtual bool can_break()
67 Vector get_start_position() const
69 return start_position;
71 void set_start_position(const Vector& vec)
76 /** Count this badguy to the statistics? This value should not be
77 changed during runtime. */
80 /** Called when hit by a fire bullet, and is_flammable() returns true */
81 virtual void ignite();
83 /** Called to revert a badguy when is_ignited() returns true */
84 virtual void extinguish();
86 /** Returns whether to call ignite() when a badguy gets hit by a fire bullet */
87 virtual bool is_flammable() const;
89 /** Returns whether this badguys is currently on fire */
90 bool is_ignited() const;
92 /** Called when hit by an ice bullet, and is_freezable() returns true. */
93 virtual void freeze();
95 /** Called to unfreeze the badguy. */
96 virtual void unfreeze();
98 virtual bool is_freezable() const;
100 bool is_frozen() const;
112 /** Called when the badguy collided with a player */
113 virtual HitResponse collision_player(Player& player, const CollisionHit& hit);
115 /** Called when the badguy collided with solid ground */
116 virtual void collision_solid(const CollisionHit& hit);
118 /** Called when the badguy collided with another badguy */
119 virtual HitResponse collision_badguy(BadGuy& other, const CollisionHit& hit);
121 /** Called when the player hit the badguy from above. You should
122 return true if the badguy was squished, false if squishing
124 virtual bool collision_squished(GameObject& object);
126 /** Called when the badguy collided with a bullet */
127 virtual HitResponse collision_bullet(Bullet& bullet, const CollisionHit& hit);
129 /** called each frame when the badguy is activated. */
130 virtual void active_update(float elapsed_time);
132 /** called each frame when the badguy is not activated. */
133 virtual void inactive_update(float elapsed_time);
135 /** true if initialize() has already been called */
138 /** called immediately before the first call to initialize */
139 virtual void initialize();
141 /** called when the badguy has been activated. (As a side effect the
142 dir variable might have been changed so that it faces towards
144 virtual void activate();
146 /** called when the badguy has been deactivated */
147 virtual void deactivate();
149 void kill_squished(GameObject& object);
151 void set_state(State state);
152 State get_state() const
155 /** returns a pointer to the nearest player or 0 if no player is available */
156 Player* get_nearest_player();
158 /** initial position of the enemy. Also the position where enemy
159 respawns when after being deactivated. */
162 /** Returns true if we might soon fall at least @c height
163 pixels. Minimum value for height is 1 pixel */
164 bool might_fall(int height = 1);
166 Vector start_position;
168 /** The direction we currently face in */
171 /** The direction we initially faced in */
174 /** Get Direction from String. */
175 Direction str2dir( std::string dir_str );
177 /** Update on_ground_flag judging by solid collision @c hit. This
178 gets called from the base implementation of collision_solid, so
179 call this when overriding collision_solid's default
181 void update_on_ground_flag(const CollisionHit& hit);
183 /** Returns true if we touched ground in the past frame This only
184 works if update_on_ground_flag() gets called in
188 /** Returns floor normal stored the last time when
189 update_on_ground_flag was called and we touched something solid
191 Vector get_floor_normal();
194 bool ignited; /**< true if this badguy is currently on fire */
196 std::string dead_script; /**< script to execute when badguy is killed */
198 /** Returns true if we were in STATE_ACTIVE at the beginning of the
199 last call to update() */
202 /** changes colgroup_active. Also calls set_group when badguy is in STATE_ACTIVE */
203 void set_colgroup_active(CollisionGroup group);
214 /** true if state was STATE_ACTIVE at the beginning of the last call
220 /** true if we touched something solid from above and
221 update_on_ground_flag was called last frame */
224 /** floor normal stored the last time when update_on_ground_flag was
225 called and we touched something solid from above */
228 /** CollisionGroup the badguy should be in while active */
229 CollisionGroup colgroup_active;
232 BadGuy(const BadGuy&);
233 BadGuy& operator=(const BadGuy&);