Move falling badguys to LAYER_FALLING (= 500), fixes bug 1033
[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       log_warning << "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 }
222
223 void
224 BadGuy::inactive_update(float )
225 {
226 }
227
228 void
229 BadGuy::collision_tile(uint32_t tile_attributes)
230 {
231   // Don't kill badguys that have already been killed
232   if (!is_active()) return;
233
234   if(tile_attributes & Tile::HURTS) {
235     if (tile_attributes & Tile::FIRE) {
236       if (is_flammable()) ignite();
237     }
238     else if (tile_attributes & Tile::ICE) {
239       if (is_freezable()) freeze();
240     }
241     else {
242       kill_fall();
243     }
244   }
245 }
246
247 HitResponse
248 BadGuy::collision(GameObject& other, const CollisionHit& hit)
249 {
250   if (!is_active()) return ABORT_MOVE;
251
252   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
253   if(badguy && badguy->is_active() && badguy->get_group() == COLGROUP_MOVING) {
254
255     /* Badguys don't let badguys squish other badguys. It's bad. */
256 #if 0
257     // hit from above?
258     if (badguy->get_bbox().p2.y < (bbox.p1.y + 16)) {
259       if(collision_squished(*badguy)) {
260         return ABORT_MOVE;
261       }
262     }
263 #endif
264
265     return collision_badguy(*badguy, hit);
266   }
267
268   Player* player = dynamic_cast<Player*> (&other);
269   if(player) {
270
271     // hit from above?
272     if (player->get_bbox().p2.y < (bbox.p1.y + 16)) {
273       if(collision_squished(*player)) {
274         return FORCE_MOVE;
275       }
276     }
277
278     return collision_player(*player, hit);
279   }
280
281   Bullet* bullet = dynamic_cast<Bullet*> (&other);
282   if(bullet)
283     return collision_bullet(*bullet, hit);
284
285   return FORCE_MOVE;
286 }
287
288 void
289 BadGuy::collision_solid(const CollisionHit& hit)
290 {
291   physic.set_velocity_x(0);
292   physic.set_velocity_y(0);
293   update_on_ground_flag(hit);
294 }
295
296 HitResponse
297 BadGuy::collision_player(Player& player, const CollisionHit& )
298 {
299   if(player.is_invincible()) {
300     kill_fall();
301     return ABORT_MOVE;
302   }
303
304   if(frozen)
305     unfreeze();
306   player.kill(false);
307   return FORCE_MOVE;
308 }
309
310 HitResponse
311 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
312 {
313   return FORCE_MOVE;
314 }
315
316 bool
317 BadGuy::collision_squished(GameObject& )
318 {
319   return false;
320 }
321
322 HitResponse
323 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& hit)
324 {
325   if (is_frozen()) {
326     if(bullet.get_type() == FIRE_BONUS) {
327       // fire bullet thaws frozen badguys
328       unfreeze();
329       bullet.remove_me();
330       return ABORT_MOVE;
331     } else {
332       // other bullets ricochet
333       bullet.ricochet(*this, hit);
334       return FORCE_MOVE;
335     }
336   }
337   else if (is_ignited()) {
338     if(bullet.get_type() == ICE_BONUS) {
339       // ice bullets extinguish ignited badguys
340       extinguish();
341       bullet.remove_me();
342       return ABORT_MOVE;
343     } else {
344       // other bullets are absorbed by ignited badguys
345       bullet.remove_me();
346       return FORCE_MOVE;
347     }
348   }
349   else if(bullet.get_type() == FIRE_BONUS && is_flammable()) {
350     // fire bullets ignite flammable badguys
351     ignite();
352     bullet.remove_me();
353     return ABORT_MOVE;
354   }
355   else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
356     // ice bullets freeze freezable badguys
357     freeze();
358     bullet.remove_me();
359     return ABORT_MOVE;
360   }
361   else {
362     // in all other cases, bullets ricochet
363     bullet.ricochet(*this, hit);
364     return FORCE_MOVE;
365   }
366 }
367
368 void
369 BadGuy::kill_squished(GameObject& object)
370 {
371   if (!is_active()) return;
372
373   sound_manager->play("sounds/squish.wav", get_pos());
374   physic.enable_gravity(true);
375   physic.set_velocity_x(0);
376   physic.set_velocity_y(0);
377   set_state(STATE_SQUISHED);
378   set_group(COLGROUP_MOVING_ONLY_STATIC);
379   Player* player = dynamic_cast<Player*>(&object);
380   if (player) {
381     player->bounce(*this);
382   }
383
384   // start dead-script
385   run_dead_script();
386 }
387
388 void
389 BadGuy::kill_fall()
390 {
391   if (!is_active()) return;
392
393   sound_manager->play("sounds/fall.wav", get_pos());
394   physic.set_velocity_y(0);
395   physic.set_acceleration_y(0);
396   physic.enable_gravity(true);
397   set_state(STATE_FALLING);
398   layer = LAYER_FALLING;
399
400   // start dead-script
401   run_dead_script();
402 }
403
404 void
405 BadGuy::run_dead_script()
406 {
407   if (countMe)
408     Sector::current()->get_level()->stats.badguys++;
409
410   countMe = false;
411    
412   // start dead-script
413   if(dead_script != "") {
414     std::istringstream stream(dead_script);
415     Sector::current()->run_script(stream, "dead-script");
416   }
417 }
418
419 void
420 BadGuy::set_state(State state)
421 {
422   if(this->state == state)
423     return;
424
425   State laststate = this->state;
426   this->state = state;
427   switch(state) {
428     case STATE_SQUISHED:
429       state_timer.start(SQUISH_TIME);
430       break;
431     case STATE_ACTIVE:
432       set_group(colgroup_active);
433       //bbox.set_pos(start_position);
434       break;
435     case STATE_INACTIVE:
436       // was the badguy dead anyway?
437       if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
438         remove_me();
439       }
440       set_group(COLGROUP_DISABLED);
441       break;
442     case STATE_FALLING:
443       set_group(COLGROUP_DISABLED);
444       break;
445     default:
446       break;
447   }
448 }
449
450 bool
451 BadGuy::is_offscreen()
452 {
453   Player* player = get_nearest_player();
454   if (!player) return false;
455   Vector dist = player->get_bbox().get_middle() - get_bbox().get_middle();
456   // In SuperTux 0.1.x, Badguys were activated when Tux<->Badguy center distance was approx. <= ~668px
457   // This doesn't work for wide-screen monitors which give us a virt. res. of approx. 1066px x 600px
458   if ((fabsf(dist.x) <= X_OFFSCREEN_DISTANCE) && (fabsf(dist.y) <= Y_OFFSCREEN_DISTANCE)) {
459     return false;
460   }
461   return true;
462 }
463
464 void
465 BadGuy::try_activate()
466 {
467   // Don't activate if player is dying
468   Player* player = get_nearest_player();
469   if (!player) return;
470
471   if (!is_offscreen()) {
472     set_state(STATE_ACTIVE);
473     if (!is_initialized) {
474
475       // if starting direction was set to AUTO, this is our chance to re-orient the badguy
476       if (start_dir == AUTO) {
477         Player* player = get_nearest_player();
478         if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
479           dir = RIGHT;
480         } else {
481           dir = LEFT;
482         }
483       }
484
485       initialize();
486       is_initialized = true;
487     }
488     activate();
489   }
490 }
491
492 bool
493 BadGuy::might_fall(int height)
494 {
495   // make sure we check for at least a 1-pixel fall
496   assert(height > 0);
497
498   float x1;
499   float x2;
500   float y1 = bbox.p2.y + 1;
501   float y2 = bbox.p2.y + 1 + height;
502   if (dir == LEFT) {
503     x1 = bbox.p1.x - 1;
504     x2 = bbox.p1.x;
505   } else {
506     x1 = bbox.p2.x;
507     x2 = bbox.p2.x + 1;
508   }
509   return Sector::current()->is_free_of_statics(Rectf(x1, y1, x2, y2));
510 }
511
512 Player*
513 BadGuy::get_nearest_player()
514 {
515   return Sector::current()->get_nearest_player (this->get_bbox ());
516 }
517
518 void
519 BadGuy::update_on_ground_flag(const CollisionHit& hit)
520 {
521   if (hit.bottom) {
522     on_ground_flag = true;
523     floor_normal = hit.slope_normal;
524   }
525 }
526
527 bool
528 BadGuy::on_ground()
529 {
530   return on_ground_flag;
531 }
532
533 bool
534 BadGuy::is_active()
535 {
536   return is_active_flag;
537 }
538
539 Vector
540 BadGuy::get_floor_normal()
541 {
542   return floor_normal;
543 }
544
545 void
546 BadGuy::freeze()
547 {
548   set_group(COLGROUP_MOVING_STATIC);
549   frozen = true;
550 }
551
552 void
553 BadGuy::unfreeze()
554 {
555   set_group(colgroup_active);
556   frozen = false;
557 }
558
559 bool
560 BadGuy::is_freezable() const
561 {
562   return false;
563 }
564
565 bool
566 BadGuy::is_frozen() const
567 {
568   return frozen;
569 }
570
571 void
572 BadGuy::ignite()
573 {
574   kill_fall();
575 }
576
577 void
578 BadGuy::extinguish()
579 {
580 }
581
582 bool
583 BadGuy::is_flammable() const
584 {
585   return true;
586 }
587
588 bool
589 BadGuy::is_ignited() const
590 {
591   return ignited;
592 }
593   
594 void 
595 BadGuy::set_colgroup_active(CollisionGroup group)
596 {
597   this->colgroup_active = group;
598   if (state == STATE_ACTIVE) set_group(group); 
599 }
600
601 /* EOF */