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