609b3522c700f71393448c315eed6616a0ad80ab
[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   SoundManager::current()->preload("sounds/squish.wav");
56   SoundManager::current()->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   SoundManager::current()->preload("sounds/squish.wav");
82   SoundManager::current()->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   SoundManager::current()->preload("sounds/squish.wav");
115   SoundManager::current()->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(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     if(countMe) {
144       // get badguy name from sprite_name ignoring path and extension
145       std::string badguy = sprite_name.substr(0, sprite_name.length() - 7);
146       int path_chars = badguy.rfind("/",badguy.length());
147       badguy = badguy.substr(path_chars + 1, badguy.length() - path_chars);
148       // log warning since badguys_killed can no longer reach total_badguys
149       std::string current_level = "[" + Sector::current()->get_level()->filename + "] ";
150       log_warning << current_level << "Counted badguy " << badguy << " starting at " << start_position << " has left the sector" <<std::endl;;
151     }
152     return;
153   }
154   if ((state != STATE_INACTIVE) && is_offscreen()) {
155     if (state == STATE_ACTIVE) deactivate();
156     set_state(STATE_INACTIVE);
157   }
158
159   switch(state) {
160     case STATE_ACTIVE:
161       is_active_flag = true;
162       active_update(elapsed_time);
163       break;
164     case STATE_INIT:
165     case STATE_INACTIVE:
166       is_active_flag = false;
167       inactive_update(elapsed_time);
168       try_activate();
169       break;
170     case STATE_SQUISHED:
171       is_active_flag = false;
172       if(state_timer.check()) {
173         remove_me();
174         break;
175       }
176       movement = physic.get_movement(elapsed_time);
177       break;
178     case STATE_FALLING:
179       is_active_flag = false;
180       movement = physic.get_movement(elapsed_time);
181       break;
182   }
183
184   on_ground_flag = false;
185 }
186
187 Direction
188 BadGuy::str2dir( std::string dir_str )
189 {
190   if( dir_str == "auto" )
191     return AUTO;
192   if( dir_str == "left" )
193     return LEFT;
194   if( dir_str == "right" )
195     return RIGHT;
196
197   //default to "auto"
198   log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
199   return AUTO;
200 }
201
202 void
203 BadGuy::initialize()
204 {
205 }
206
207 void
208 BadGuy::activate()
209 {
210 }
211
212 void
213 BadGuy::deactivate()
214 {
215 }
216
217 void
218 BadGuy::active_update(float elapsed_time)
219 {
220   movement = physic.get_movement(elapsed_time);
221   if(frozen)
222     sprite->stop_animation();
223 }
224
225 void
226 BadGuy::inactive_update(float )
227 {
228 }
229
230 void
231 BadGuy::collision_tile(uint32_t tile_attributes)
232 {
233   // Don't kill badguys that have already been killed
234   if (!is_active()) return;
235
236   if(tile_attributes & Tile::HURTS) {
237     if (tile_attributes & Tile::FIRE) {
238       if (is_flammable()) ignite();
239     }
240     else if (tile_attributes & Tile::ICE) {
241       if (is_freezable()) freeze();
242     }
243     else {
244       kill_fall();
245     }
246   }
247 }
248
249 HitResponse
250 BadGuy::collision(GameObject& other, const CollisionHit& hit)
251 {
252   if (!is_active()) return ABORT_MOVE;
253
254   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
255   if(badguy && badguy->is_active() && badguy->get_group() == COLGROUP_MOVING) {
256
257     /* Badguys don't let badguys squish other badguys. It's bad. */
258 #if 0
259     // hit from above?
260     if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
261       if(collision_squished(*badguy)) {
262         return ABORT_MOVE;
263       }
264     }
265 #endif
266
267     return collision_badguy(*badguy, hit);
268   }
269
270   Player* player = dynamic_cast<Player*> (&other);
271   if(player) {
272
273     // hit from above?
274     if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
275       if(player->is_stone()) {
276         kill_fall();
277         return FORCE_MOVE;
278       }
279       if(collision_squished(*player)) {
280         return FORCE_MOVE;
281       }
282     }
283
284     if(player->is_stone()) {
285       collision_solid(hit);
286       return FORCE_MOVE;
287     }
288
289     return collision_player(*player, hit);
290   }
291
292   Bullet* bullet = dynamic_cast<Bullet*> (&other);
293   if(bullet)
294     return collision_bullet(*bullet, hit);
295
296   return FORCE_MOVE;
297 }
298
299 void
300 BadGuy::collision_solid(const CollisionHit& hit)
301 {
302   physic.set_velocity_x(0);
303   physic.set_velocity_y(0);
304   update_on_ground_flag(hit);
305 }
306
307 HitResponse
308 BadGuy::collision_player(Player& player, const CollisionHit& )
309 {
310   if(player.is_invincible()) {
311     kill_fall();
312     return ABORT_MOVE;
313   }
314
315   //TODO: unfreeze timer
316   if(frozen)
317     //unfreeze();
318     return FORCE_MOVE;
319
320   player.kill(false);
321   return FORCE_MOVE;
322 }
323
324 HitResponse
325 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
326 {
327   return FORCE_MOVE;
328 }
329
330 bool
331 BadGuy::collision_squished(GameObject& object)
332 {
333   // frozen badguys can be killed with butt-jump
334   if(frozen)
335   {
336     Player* player = dynamic_cast<Player*>(&object);
337     if(player && (player->does_buttjump)) {
338       player->bounce(*this);
339       kill_fall();//TODO: shatter frozen badguys
340       return true;
341     }
342   }
343     return false;
344 }
345
346 HitResponse
347 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
348 {
349   if (is_frozen()) {
350     if(bullet.get_type() == FIRE_BONUS) {
351       // fire bullet thaws frozen badguys
352       unfreeze();
353       bullet.remove_me();
354       return ABORT_MOVE;
355     } else {
356       // other bullets ricochet
357       bullet.ricochet(*this, hit);
358       return FORCE_MOVE;
359     }
360   }
361   else if (is_ignited()) {
362     if(bullet.get_type() == ICE_BONUS) {
363       // ice bullets extinguish ignited badguys
364       extinguish();
365       bullet.remove_me();
366       return ABORT_MOVE;
367     } else {
368       // other bullets are absorbed by ignited badguys
369       bullet.remove_me();
370       return FORCE_MOVE;
371     }
372   }
373   else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
374     // fire bullets ignite flammable badguys
375     ignite();
376     bullet.remove_me();
377     return ABORT_MOVE;
378   }
379   else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
380     // ice bullets freeze freezable badguys
381     freeze();
382     bullet.remove_me();
383     return ABORT_MOVE;
384   }
385   else {
386     // in all other cases, bullets ricochet
387     bullet.ricochet(*this, hit);
388     return FORCE_MOVE;
389   }
390 }
391
392 void
393 BadGuy::kill_squished(GameObject& object)
394 {
395   if (!is_active()) return;
396
397   SoundManager::current()->play("sounds/squish.wav", get_pos());
398   physic.enable_gravity(true);
399   physic.set_velocity_x(0);
400   physic.set_velocity_y(0);
401   set_state(STATE_SQUISHED);
402   set_group(COLGROUP_MOVING_ONLY_STATIC);
403   Player* player = dynamic_cast<Player*>(&object);
404   if (player) {
405     player->bounce(*this);
406   }
407
408   // start dead-script
409   run_dead_script();
410 }
411
412 void
413 BadGuy::kill_fall()
414 {
415   if (!is_active()) return;
416
417   SoundManager::current()->play("sounds/fall.wav", get_pos());
418   physic.set_velocity_y(0);
419   physic.set_acceleration_y(0);
420   physic.enable_gravity(true);
421   set_state(STATE_FALLING);
422
423   // Set the badguy layer to be the foremost, so that
424   // this does not reveal secret tilemaps:
425   layer = Sector::current()->get_foremost_layer() + 1;
426
427   // start dead-script
428   run_dead_script();
429 }
430
431 void
432 BadGuy::run_dead_script()
433 {
434   if (countMe)
435     Sector::current()->get_level()->stats.badguys++;
436
437   countMe = false;
438
439   // start dead-script
440   if(dead_script != "") {
441     std::istringstream stream(dead_script);
442     Sector::current()->run_script(stream, "dead-script");
443   }
444 }
445
446 void
447 BadGuy::set_state(State state_)
448 {
449   if(this->state == state_)
450     return;
451
452   State laststate = this->state;
453   this->state = state_;
454   switch(state_) {
455     case STATE_SQUISHED:
456       state_timer.start(SQUISH_TIME);
457       break;
458     case STATE_ACTIVE:
459       set_group(colgroup_active);
460       //bbox.set_pos(start_position);
461       break;
462     case STATE_INACTIVE:
463       // was the badguy dead anyway?
464       if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
465         remove_me();
466       }
467       set_group(COLGROUP_DISABLED);
468       break;
469     case STATE_FALLING:
470       set_group(COLGROUP_DISABLED);
471       break;
472     default:
473       break;
474   }
475 }
476
477 bool
478 BadGuy::is_offscreen()
479 {
480   Player* player = get_nearest_player();
481   if (!player) return false;
482   Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
483   // In SuperTux 0.1.x, Badguys were activated when Tux<->Badguy center distance was approx. <= ~668px
484   // This doesn't work for wide-screen monitors which give us a virt. res. of approx. 1066px x 600px
485   if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
486     return false;
487   }
488   return true;
489 }
490
491 void
492 BadGuy::try_activate()
493 {
494   // Don't activate if player is dying
495   Player* player = get_nearest_player();
496   if (!player) return;
497
498   if (!is_offscreen()) {
499     set_state(STATE_ACTIVE);
500     if (!is_initialized) {
501
502       // if starting direction was set to AUTO, this is our chance to re-orient the badguy
503       if (start_dir == AUTO) {
504         Player* player_ = get_nearest_player();
505         if (player_ && (player_->get_bbox().p1.x > get_bbox().p2.x)) {
506           dir = RIGHT;
507         } else {
508           dir = LEFT;
509         }
510       }
511
512       initialize();
513       is_initialized = true;
514     }
515     activate();
516   }
517 }
518
519 bool
520 BadGuy::might_fall(int height)
521 {
522   // make sure we check for at least a 1-pixel fall
523   assert(height > 0);
524
525   float x1;
526   float x2;
527   float y1 = bbox.p2.y + 1;
528   float y2 = bbox.p2.y + 1 + height;
529   if (dir == LEFT) {
530     x1 = bbox.p1.x - 1;
531     x2 = bbox.p1.x;
532   } else {
533     x1 = bbox.p2.x;
534     x2 = bbox.p2.x + 1;
535   }
536   return Sector::current()->is_free_of_statics(Rectf(x1, y1, x2, y2));
537 }
538
539 Player*
540 BadGuy::get_nearest_player()
541 {
542   return Sector::current()->get_nearest_player (this->get_bbox ());
543 }
544
545 void
546 BadGuy::update_on_ground_flag(const CollisionHit& hit)
547 {
548   if (hit.bottom) {
549     on_ground_flag = true;
550     floor_normal = hit.slope_normal;
551   }
552 }
553
554 bool
555 BadGuy::on_ground()
556 {
557   return on_ground_flag;
558 }
559
560 bool
561 BadGuy::is_active()
562 {
563   return is_active_flag;
564 }
565
566 Vector
567 BadGuy::get_floor_normal()
568 {
569   return floor_normal;
570 }
571
572 void
573 BadGuy::freeze()
574 {
575   set_group(COLGROUP_MOVING_STATIC);
576   frozen = true;
577
578   if(sprite->has_action("iced-left"))
579     sprite->set_action(dir == LEFT ? "iced-left" : "iced-right", 1);
580   // when no iced action exists, default to shading badguy blue
581   else
582   {
583     sprite->set_color(Color(0.60, 0.72, 0.88f));
584     sprite->stop_animation();
585   }
586 }
587
588 void
589 BadGuy::unfreeze()
590 {
591   set_group(colgroup_active);
592   frozen = false;
593
594   // restore original color if needed
595   if(!sprite->has_action("iced-left"))
596   {
597     sprite->set_color(Color(1.00, 1.00, 1.00f));
598     sprite->set_animation_loops();
599   }
600 }
601
602 bool
603 BadGuy::is_freezable() const
604 {
605   return false;
606 }
607
608 bool
609 BadGuy::is_frozen() const
610 {
611   return frozen;
612 }
613
614 void
615 BadGuy::ignite()
616 {
617   kill_fall();
618 }
619
620 void
621 BadGuy::extinguish()
622 {
623 }
624
625 bool
626 BadGuy::is_flammable() const
627 {
628   return true;
629 }
630
631 bool
632 BadGuy::is_ignited() const
633 {
634   return ignited;
635 }
636
637 void
638 BadGuy::set_colgroup_active(CollisionGroup group_)
639 {
640   this->colgroup_active = group_;
641   if (state == STATE_ACTIVE) set_group(group_);
642 }
643
644 /* EOF */