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