Make tux and yeti dying state not reveal secret tilemaps
[supertux.git] / src / badguy / yeti.cpp
1 //  SuperTux - Boss "Yeti"
2 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
3 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "badguy/yeti.hpp"
19
20 #include "audio/sound_manager.hpp"
21 #include "badguy/bouncing_snowball.hpp"
22 #include "badguy/yeti_stalactite.hpp"
23 #include "object/camera.hpp"
24 #include "object/player.hpp"
25 #include "sprite/sprite.hpp"
26 #include "supertux/object_factory.hpp"
27 #include "supertux/sector.hpp"
28
29 #include <float.h>
30 #include <math.h>
31
32 namespace {
33 const float JUMP_DOWN_VX = 250; /**< horizontal speed while jumping off the dais */
34 const float JUMP_DOWN_VY = -250; /**< vertical speed while jumping off the dais */
35
36 const float RUN_VX = 350; /**< horizontal speed while running */
37
38 const float JUMP_UP_VX = 350; /**< horizontal speed while jumping on the dais */
39 const float JUMP_UP_VY = -700; /**< vertical speed while jumping on the dais */
40
41 const float STOMP_VY = -300; /** vertical speed while stomping on the dais */
42
43 const float LEFT_STAND_X = 80; /**< x-coordinate of left dais' end position */
44 const float RIGHT_STAND_X = 1280-LEFT_STAND_X-60; /**< x-coordinate of right dais' end position */
45 const float LEFT_JUMP_X = LEFT_STAND_X+448; /**< x-coordinate of from where to jump on the left dais */
46 const float RIGHT_JUMP_X = RIGHT_STAND_X-448; /**< x-coordinate of from where to jump on the right dais */
47 const float STOMP_WAIT = .5; /**< time we stay on the dais before jumping again */
48 const float SAFE_TIME = .5; /**< the time we are safe when tux just hit us */
49 const int INITIAL_HITPOINTS = 5; /**< number of hits we can take */
50
51 const float YETI_SQUISH_TIME = 5;
52 }
53
54 Yeti::Yeti(const Reader& reader) :
55   BadGuy(reader, "images/creatures/yeti/yeti.sprite"),
56   state(),
57   state_timer(),
58   safe_timer(),
59   stomp_count(),
60   hit_points(),
61   hud_head()
62 {
63   hit_points = INITIAL_HITPOINTS;
64   countMe = false;
65   SoundManager::current()->preload("sounds/yeti_gna.wav");
66   SoundManager::current()->preload("sounds/yeti_roar.wav");
67   hud_head = Surface::create("images/creatures/yeti/hudlife.png");
68 }
69
70 Yeti::~Yeti()
71 {
72 }
73
74 void
75 Yeti::initialize()
76 {
77   dir = RIGHT;
78   jump_down();
79 }
80
81 void
82 Yeti::draw(DrawingContext& context)
83 {
84   // we blink when we are safe
85   if(safe_timer.started() && size_t(game_time*40)%2)
86     return;
87
88   draw_hit_points(context);
89
90   BadGuy::draw(context);
91 }
92
93 void
94 Yeti::draw_hit_points(DrawingContext& context)
95 {
96   if (hud_head)
97   {
98     context.push_transform();
99     context.set_translation(Vector(0, 0));
100
101     for (int i = 0; i < hit_points; ++i)
102     {
103       context.draw_surface(hud_head, Vector(BORDER_X + (i * hud_head->get_width()), BORDER_Y + 1), LAYER_FOREGROUND1);
104     }
105
106     context.pop_transform();
107   }
108 }
109
110 void
111 Yeti::active_update(float elapsed_time)
112 {
113   switch(state) {
114     case JUMP_DOWN:
115       physic.set_velocity_x((dir==RIGHT)?+JUMP_DOWN_VX:-JUMP_DOWN_VX);
116       break;
117     case RUN:
118       physic.set_velocity_x((dir==RIGHT)?+RUN_VX:-RUN_VX);
119       if (((dir == RIGHT) && (get_pos().x >= RIGHT_JUMP_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_JUMP_X))) jump_up();
120       break;
121     case JUMP_UP:
122       physic.set_velocity_x((dir==RIGHT)?+JUMP_UP_VX:-JUMP_UP_VX);
123       if (((dir == RIGHT) && (get_pos().x >= RIGHT_STAND_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_STAND_X))) be_angry();
124       break;
125     case BE_ANGRY:
126       if(state_timer.check()) {
127         SoundManager::current()->play("sounds/yeti_gna.wav");
128         physic.set_velocity_y(STOMP_VY);
129         sprite->set_action((dir==RIGHT)?"stomp-right":"stomp-left");
130       }
131       break;
132     case SQUISHED:
133       if (state_timer.check()) {
134         remove_me();
135       }
136       break;
137   }
138
139   movement = physic.get_movement(elapsed_time);
140 }
141
142 void
143 Yeti::jump_down()
144 {
145   sprite->set_action((dir==RIGHT)?"jump-right":"jump-left");
146   physic.set_velocity_x((dir==RIGHT)?(+JUMP_DOWN_VX):(-JUMP_DOWN_VX));
147   physic.set_velocity_y(JUMP_DOWN_VY);
148   state = JUMP_DOWN;
149 }
150
151 void
152 Yeti::run()
153 {
154   sprite->set_action((dir==RIGHT)?"run-right":"run-left");
155   physic.set_velocity_x((dir==RIGHT)?(+RUN_VX):(-RUN_VX));
156   physic.set_velocity_y(0);
157   state = RUN;
158 }
159
160 void
161 Yeti::jump_up()
162 {
163   sprite->set_action((dir==RIGHT)?"jump-right":"jump-left");
164   physic.set_velocity_x((dir==RIGHT)?(+JUMP_UP_VX):(-JUMP_UP_VX));
165   physic.set_velocity_y(JUMP_UP_VY);
166   state = JUMP_UP;
167 }
168
169 void
170 Yeti::be_angry()
171 {
172   //turn around
173   dir = (dir==RIGHT) ? LEFT : RIGHT;
174
175   sprite->set_action((dir==RIGHT) ? "stand-right" : "stand-left");
176   physic.set_velocity_x(0);
177   physic.set_velocity_y(0);
178   stomp_count = 0;
179   state = BE_ANGRY;
180   state_timer.start(STOMP_WAIT);
181 }
182
183 bool
184 Yeti::collision_squished(GameObject& object)
185 {
186   kill_squished(object);
187
188   return true;
189 }
190
191 void
192 Yeti::kill_squished(GameObject& object)
193 {
194   Player* player = dynamic_cast<Player*>(&object);
195   if (player) {
196     player->bounce(*this);
197     take_hit(*player);
198   }
199 }
200
201 void Yeti::take_hit(Player& )
202 {
203   if(safe_timer.started())
204     return;
205
206   SoundManager::current()->play("sounds/yeti_roar.wav");
207   hit_points--;
208
209   if(hit_points <= 0) {
210     // We're dead
211     physic.enable_gravity(true);
212     physic.set_velocity_x(0);
213     physic.set_velocity_y(0);
214
215     // Set the badguy layer to be above the foremost, so that
216     // this does not reveal secret tilemaps:
217     layer = Sector::current()->get_foremost_layer() + 1;
218     state = SQUISHED;
219     state_timer.start(YETI_SQUISH_TIME);
220     set_colgroup_active(COLGROUP_MOVING_ONLY_STATIC);
221     sprite->set_action("dead");
222
223     run_dead_script();
224   }
225   else {
226     safe_timer.start(SAFE_TIME);
227   }
228 }
229
230 void
231 Yeti::kill_fall()
232 {
233   // shooting bullets or being invincible won't work :)
234 }
235
236 void
237 Yeti::drop_stalactite()
238 {
239   // make a stalactite falling down and shake camera a bit
240   Sector::current()->camera->shake(.1f, 0, 10);
241
242   Player* player = this->get_nearest_player();
243   if (!player) return;
244
245   Sector* sector = Sector::current();
246   for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
247       i != sector->gameobjects.end(); ++i) {
248     YetiStalactite* stalactite = dynamic_cast<YetiStalactite*>(i->get());
249     if(stalactite && stalactite->is_hanging()) {
250       if (hit_points >= 3) {
251         // drop stalactites within 3 of player, going out with each jump
252         float distancex = fabsf(stalactite->get_bbox().get_middle().x - player->get_bbox().get_middle().x);
253         if(distancex < stomp_count*32) {
254           stalactite->start_shaking();
255         }
256       }
257       else { /* if (hitpoints < 3) */
258         // drop every 3rd pair of stalactites
259         if(((((int)stalactite->get_pos().x + 16) / 64) % 3) == (stomp_count % 3)) {
260           stalactite->start_shaking();
261         }
262       }
263     } /* if(stalactite && stalactite->is_hanging()) */
264   }
265 }
266
267 void
268 Yeti::collision_solid(const CollisionHit& hit)
269 {
270   if(hit.top || hit.bottom) {
271     // hit floor or roof
272     physic.set_velocity_y(0);
273     switch (state) {
274       case JUMP_DOWN:
275         run();
276         break;
277       case RUN:
278         break;
279       case JUMP_UP:
280         break;
281       case BE_ANGRY:
282         // we just landed
283         if(!state_timer.started()) {
284           sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
285           stomp_count++;
286           drop_stalactite();
287
288           // go to other side after 3 jumps
289           if(stomp_count == 3) {
290             jump_down();
291           } else {
292             // jump again
293             state_timer.start(STOMP_WAIT);
294           }
295         }
296         break;
297       case SQUISHED:
298         break;
299     }
300   } else if(hit.left || hit.right) {
301     // hit wall
302     jump_up();
303   }
304 }
305
306 /* EOF */