Checking if badguy is active before double-killing in a few more places
[supertux.git] / src / badguy / badguy.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "badguy/badguy.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "object/bullet.hpp"
21 #include "object/player.hpp"
22 #include "supertux/level.hpp"
23 #include "supertux/sector.hpp"
24 #include "supertux/tile.hpp"
25 #include "util/reader.hpp"
26
27 #include <math.h>
28 #include <sstream>
29
30 static const float SQUISH_TIME = 2;
31   
32 static const float X_OFFSCREEN_DISTANCE = 1280;
33 static const float Y_OFFSCREEN_DISTANCE = 800;
34
35 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer) :
36   MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), 
37   physic(),
38   countMe(true), 
39   is_initialized(false),
40   start_position(),
41   dir(LEFT), 
42   start_dir(AUTO), 
43   frozen(false), 
44   ignited(false),
45   dead_script(),
46   state(STATE_INIT), 
47   is_active_flag(),
48   state_timer(),
49   on_ground_flag(false),
50   floor_normal(),
51   colgroup_active(COLGROUP_MOVING)
52 {
53   start_position = bbox.p1;
54
55   sound_manager->preload("sounds/squish.wav");
56   sound_manager->preload("sounds/fall.wav");
57
58   dir = (start_dir == AUTO) ? LEFT : start_dir;
59 }
60
61 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer) :
62   MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), 
63   physic(),
64   countMe(true), 
65   is_initialized(false), 
66   start_position(),
67   dir(direction), 
68   start_dir(direction), 
69   frozen(false), 
70   ignited(false),
71   dead_script(),
72   state(STATE_INIT), 
73   is_active_flag(),
74   state_timer(),
75   on_ground_flag(false), 
76   floor_normal(),
77   colgroup_active(COLGROUP_MOVING)
78 {
79   start_position = bbox.p1;
80
81   sound_manager->preload("sounds/squish.wav");
82   sound_manager->preload("sounds/fall.wav");
83
84   dir = (start_dir == AUTO) ? LEFT : start_dir;
85 }
86
87 BadGuy::BadGuy(const Reader& reader, const std::string& sprite_name, int layer) :
88   MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED), 
89   physic(),
90   countMe(true), 
91   is_initialized(false), 
92   start_position(),
93   dir(LEFT), 
94   start_dir(AUTO),
95   frozen(false), 
96   ignited(false), 
97   dead_script(),
98   state(STATE_INIT), 
99   is_active_flag(),
100   state_timer(),
101   on_ground_flag(false), 
102   floor_normal(),
103   colgroup_active(COLGROUP_MOVING)
104 {
105   start_position = bbox.p1;
106
107   std::string dir_str = "auto";
108   reader.get("direction", dir_str);
109   start_dir = str2dir( dir_str );
110   dir = start_dir;
111
112   reader.get("dead-script", dead_script);
113
114   sound_manager->preload("sounds/squish.wav");
115   sound_manager->preload("sounds/fall.wav");
116
117   dir = (start_dir == AUTO) ? LEFT : start_dir;
118 }
119
120 void
121 BadGuy::draw(DrawingContext& context)
122 {
123   if(!sprite.get())
124     return;
125   if(state == STATE_INIT || state == STATE_INACTIVE)
126     return;
127   if(state == STATE_FALLING) {
128     DrawingEffect old_effect = context.get_drawing_effect();
129     context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
130     sprite->draw(context, get_pos(), layer);
131     context.set_drawing_effect(old_effect);
132   } else {
133     sprite->draw(context, get_pos(), layer);
134   }
135 }
136
137 void
138 BadGuy::update(float elapsed_time)
139 {
140   if(!Sector::current()->inside(bbox)) {
141     is_active_flag = false;
142     remove_me();
143     return;
144   }
145   if ((state != STATE_INACTIVE) && is_offscreen()) {
146     if (state == STATE_ACTIVE) deactivate();
147     set_state(STATE_INACTIVE);
148   }
149
150   switch(state) {
151     case STATE_ACTIVE:
152       is_active_flag = true;
153       active_update(elapsed_time);
154       break;
155     case STATE_INIT:
156     case STATE_INACTIVE:
157       is_active_flag = false;
158       inactive_update(elapsed_time);
159       try_activate();
160       break;
161     case STATE_SQUISHED:
162       is_active_flag = false;
163       if(state_timer.check()) {
164         remove_me();
165         break;
166       }
167       movement = physic.get_movement(elapsed_time);
168       break;
169     case STATE_FALLING:
170       is_active_flag = false;
171       movement = physic.get_movement(elapsed_time);
172       break;
173   }
174
175   on_ground_flag = false;
176 }
177
178 Direction
179 BadGuy::str2dir( std::string dir_str )
180 {
181   if( dir_str == "auto" )
182     return AUTO;
183   if( dir_str == "left" )
184     return LEFT;
185   if( dir_str == "right" )
186     return RIGHT;
187
188   //default to "auto"
189   log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
190   return AUTO;
191 }
192
193 void
194 BadGuy::initialize()
195 {
196 }
197
198 void
199 BadGuy::activate()
200 {
201 }
202
203 void
204 BadGuy::deactivate()
205 {
206 }
207
208 void
209 BadGuy::active_update(float elapsed_time)
210 {
211   movement = physic.get_movement(elapsed_time);
212 }
213
214 void
215 BadGuy::inactive_update(float )
216 {
217 }
218
219 void
220 BadGuy::collision_tile(uint32_t tile_attributes)
221 {
222   // Don't kill badguys that have already been killed
223   if (!is_active()) return;
224
225   if(tile_attributes & Tile::HURTS) {
226     if (tile_attributes & Tile::FIRE) {
227       if (is_flammable()) ignite();
228     }
229     else if (tile_attributes & Tile::ICE) {
230       if (is_freezable()) freeze();
231     }
232     else {
233       kill_fall();
234     }
235   }
236 }
237
238 HitResponse
239 BadGuy::collision(GameObject& other, const CollisionHit& hit)
240 {
241   if (!is_active()) return ABORT_MOVE;
242
243   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
244   if(badguy && badguy->is_active() && badguy->get_group() == COLGROUP_MOVING) {
245
246     /* Badguys don't let badguys squish other badguys. It's bad. */
247 #if 0
248     // hit from above?
249     if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
250       if(collision_squished(*badguy)) {
251         return ABORT_MOVE;
252       }
253     }
254 #endif
255
256     return collision_badguy(*badguy, hit);
257   }
258
259   Player* player = dynamic_cast<Player*> (&other);
260   if(player) {
261
262     // hit from above?
263     if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
264       if(collision_squished(*player)) {
265         return FORCE_MOVE;
266       }
267     }
268
269     return collision_player(*player, hit);
270   }
271
272   Bullet* bullet = dynamic_cast<Bullet*> (&other);
273   if(bullet)
274     return collision_bullet(*bullet, hit);
275
276   return FORCE_MOVE;
277 }
278
279 void
280 BadGuy::collision_solid(const CollisionHit& hit)
281 {
282   physic.set_velocity_x(0);
283   physic.set_velocity_y(0);
284   update_on_ground_flag(hit);
285 }
286
287 HitResponse
288 BadGuy::collision_player(Player& player, const CollisionHit& )
289 {
290   if(player.is_invincible()) {
291     kill_fall();
292     return ABORT_MOVE;
293   }
294
295   if(frozen)
296     unfreeze();
297   player.kill(false);
298   return FORCE_MOVE;
299 }
300
301 HitResponse
302 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
303 {
304   return FORCE_MOVE;
305 }
306
307 bool
308 BadGuy::collision_squished(GameObject& )
309 {
310   return false;
311 }
312
313 HitResponse
314 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
315 {
316   if (is_frozen()) {
317     if(bullet.get_type() == FIRE_BONUS) {
318       // fire bullet thaws frozen badguys
319       unfreeze();
320       bullet.remove_me();
321       return ABORT_MOVE;
322     } else {
323       // other bullets ricochet
324       bullet.ricochet(*this, hit);
325       return FORCE_MOVE;
326     }
327   }
328   else if (is_ignited()) {
329     if(bullet.get_type() == ICE_BONUS) {
330       // ice bullets extinguish ignited badguys
331       extinguish();
332       bullet.remove_me();
333       return ABORT_MOVE;
334     } else {
335       // other bullets are absorbed by ignited badguys
336       bullet.remove_me();
337       return FORCE_MOVE;
338     }
339   }
340   else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
341     // fire bullets ignite flammable badguys
342     ignite();
343     bullet.remove_me();
344     return ABORT_MOVE;
345   }
346   else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
347     // ice bullets freeze freezable badguys
348     freeze();
349     bullet.remove_me();
350     return ABORT_MOVE;
351   }
352   else {
353     // in all other cases, bullets ricochet
354     bullet.ricochet(*this, hit);
355     return FORCE_MOVE;
356   }
357 }
358
359 void
360 BadGuy::kill_squished(GameObject& object)
361 {
362   if (!is_active()) return;
363
364   sound_manager->play("sounds/squish.wav", get_pos());
365   physic.enable_gravity(true);
366   physic.set_velocity_x(0);
367   physic.set_velocity_y(0);
368   set_state(STATE_SQUISHED);
369   set_group(COLGROUP_MOVING_ONLY_STATIC);
370   Player* player = dynamic_cast<Player*>(&object);
371   if (player) {
372     player->bounce(*this);
373   }
374
375   // start dead-script
376   run_dead_script();
377 }
378
379 void
380 BadGuy::kill_fall()
381 {
382   if (!is_active()) return;
383
384   sound_manager->play("sounds/fall.wav", get_pos());
385   physic.set_velocity_y(0);
386   physic.set_acceleration_y(0);
387   physic.enable_gravity(true);
388   set_state(STATE_FALLING);
389
390   // start dead-script
391   run_dead_script();
392 }
393
394 void
395 BadGuy::run_dead_script()
396 {
397   if (countMe)
398     Sector::current()->get_level()->stats.badguys++;
399
400   countMe = false;
401    
402   // start dead-script
403   if(dead_script != "") {
404     std::istringstream stream(dead_script);
405     Sector::current()->run_script(stream, "dead-script");
406   }
407 }
408
409 void
410 BadGuy::set_state(State state)
411 {
412   if(this->state == state)
413     return;
414
415   State laststate = this->state;
416   this->state = state;
417   switch(state) {
418     case STATE_SQUISHED:
419       state_timer.start(SQUISH_TIME);
420       break;
421     case STATE_ACTIVE:
422       set_group(colgroup_active);
423       //bbox.set_pos(start_position);
424       break;
425     case STATE_INACTIVE:
426       // was the badguy dead anyway?
427       if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
428         remove_me();
429       }
430       set_group(COLGROUP_DISABLED);
431       break;
432     case STATE_FALLING:
433       set_group(COLGROUP_DISABLED);
434       break;
435     default:
436       break;
437   }
438 }
439
440 bool
441 BadGuy::is_offscreen()
442 {
443   Player* player = get_nearest_player();
444   if (!player) return false;
445   Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
446   // In SuperTux 0.1.x, Badguys were activated when Tux<->Badguy center distance was approx. <= ~668px
447   // This doesn't work for wide-screen monitors which give us a virt. res. of approx. 1066px x 600px
448   if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
449     return false;
450   }
451   return true;
452 }
453
454 void
455 BadGuy::try_activate()
456 {
457   // Don't activate if player is dying
458   Player* player = get_nearest_player();
459   if (!player) return;
460
461   if (!is_offscreen()) {
462     set_state(STATE_ACTIVE);
463     if (!is_initialized) {
464
465       // if starting direction was set to AUTO, this is our chance to re-orient the badguy
466       if (start_dir == AUTO) {
467         Player* player = get_nearest_player();
468         if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
469           dir = RIGHT;
470         } else {
471           dir = LEFT;
472         }
473       }
474
475       initialize();
476       is_initialized = true;
477     }
478     activate();
479   }
480 }
481
482 bool
483 BadGuy::might_fall(int height)
484 {
485   // make sure we check for at least a 1-pixel fall
486   assert(height > 0);
487
488   float x1;
489   float x2;
490   float y1 = bbox.p2.y + 1;
491   float y2 = bbox.p2.y + 1 + height;
492   if (dir == LEFT) {
493     x1 = bbox.p1.x - 1;
494     x2 = bbox.p1.x;
495   } else {
496     x1 = bbox.p2.x;
497     x2 = bbox.p2.x + 1;
498   }
499   return Sector::current()->is_free_of_statics(Rectf(x1, y1, x2, y2));
500 }
501
502 Player*
503 BadGuy::get_nearest_player()
504 {
505   return Sector::current()->get_nearest_player (this->get_bbox ());
506 }
507
508 void
509 BadGuy::update_on_ground_flag(const CollisionHit& hit)
510 {
511   if (hit.bottom) {
512     on_ground_flag = true;
513     floor_normal = hit.slope_normal;
514   }
515 }
516
517 bool
518 BadGuy::on_ground()
519 {
520   return on_ground_flag;
521 }
522
523 bool
524 BadGuy::is_active()
525 {
526   return is_active_flag;
527 }
528
529 Vector
530 BadGuy::get_floor_normal()
531 {
532   return floor_normal;
533 }
534
535 void
536 BadGuy::freeze()
537 {
538   set_group(COLGROUP_MOVING_STATIC);
539   frozen = true;
540 }
541
542 void
543 BadGuy::unfreeze()
544 {
545   set_group(colgroup_active);
546   frozen = false;
547 }
548
549 bool
550 BadGuy::is_freezable() const
551 {
552   return false;
553 }
554
555 bool
556 BadGuy::is_frozen() const
557 {
558   return frozen;
559 }
560
561 void
562 BadGuy::ignite()
563 {
564   kill_fall();
565 }
566
567 void
568 BadGuy::extinguish()
569 {
570 }
571
572 bool
573 BadGuy::is_flammable() const
574 {
575   return true;
576 }
577
578 bool
579 BadGuy::is_ignited() const
580 {
581   return ignited;
582 }
583   
584 void 
585 BadGuy::set_colgroup_active(CollisionGroup group)
586 {
587   this->colgroup_active = group;
588   if (state == STATE_ACTIVE) set_group(group); 
589 }
590
591 /* EOF */