Committed gnomino's iceflower patch. Also added placeholder graphics (selfmade, PD...
[supertux.git] / src / badguy / badguy.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include <config.h>
20
21 #include "badguy.hpp"
22 #include "object/camera.hpp"
23 #include "object/tilemap.hpp"
24 #include "tile.hpp"
25 #include "statistics.hpp"
26 #include "game_session.hpp"
27 #include "log.hpp"
28 #include "level.hpp"
29 #include "object/bullet.hpp"
30 #include "main.hpp"
31
32 static const float SQUISH_TIME = 2;
33 static const float X_OFFSCREEN_DISTANCE = 1600;
34 static const float Y_OFFSCREEN_DISTANCE = 1200;
35
36 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer)
37   : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), start_dir(AUTO), frozen(false), state(STATE_INIT), on_ground_flag(false)
38 {
39   start_position = bbox.p1;
40
41   sound_manager->preload("sounds/squish.wav");
42   sound_manager->preload("sounds/fall.wav");
43 }
44
45 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer)
46   : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(direction), start_dir(direction), frozen(false), state(STATE_INIT), on_ground_flag(false)
47 {
48   start_position = bbox.p1;
49
50   sound_manager->preload("sounds/squish.wav");
51   sound_manager->preload("sounds/fall.wav");
52 }
53
54 BadGuy::BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer)
55   : MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), start_dir(AUTO), frozen(false), state(STATE_INIT), on_ground_flag(false)
56 {
57   start_position = bbox.p1;
58
59   std::string dir_str = "auto";
60   reader.get("direction", dir_str);
61   start_dir = str2dir( dir_str );
62   dir = start_dir;
63
64   sound_manager->preload("sounds/squish.wav");
65   sound_manager->preload("sounds/fall.wav");
66 }
67
68 void
69 BadGuy::draw(DrawingContext& context)
70 {
71   if(!sprite)
72     return;
73   if(state == STATE_INIT || state == STATE_INACTIVE)
74     return;
75   if(state == STATE_FALLING) {
76     DrawingEffect old_effect = context.get_drawing_effect();
77     context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
78     sprite->draw(context, get_pos(), layer);
79     context.set_drawing_effect(old_effect);
80   } else {
81     sprite->draw(context, get_pos(), layer);
82   }
83 }
84
85 void
86 BadGuy::update(float elapsed_time)
87 {
88   if(!Sector::current()->inside(bbox)) {
89     remove_me();
90     return;
91   }
92   if(is_offscreen()) {
93     if (state == STATE_ACTIVE) deactivate();
94     set_state(STATE_INACTIVE);
95   }
96
97   switch(state) {
98     case STATE_ACTIVE:
99       active_update(elapsed_time);
100       break;
101     case STATE_INIT:
102     case STATE_INACTIVE:
103       inactive_update(elapsed_time);
104       try_activate();
105       break;
106     case STATE_SQUISHED:
107       if(state_timer.check()) {
108         remove_me();
109         break;
110       }
111       movement = physic.get_movement(elapsed_time);
112       break;
113     case STATE_FALLING:
114       movement = physic.get_movement(elapsed_time);
115       break;
116   }
117
118   on_ground_flag = false;
119 }
120
121 Direction
122 BadGuy::str2dir( std::string dir_str )
123 {
124   if( dir_str == "auto" )
125     return AUTO;
126   if( dir_str == "left" )
127     return LEFT;
128   if( dir_str == "right" )
129     return RIGHT;
130
131   //default to "auto"
132   log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
133   return AUTO;
134 }
135
136 void
137 BadGuy::activate()
138 {
139 }
140
141 void
142 BadGuy::deactivate()
143 {
144 }
145
146 void
147 BadGuy::save(lisp::Writer& )
148 {
149   log_warning << "tried to write out a generic badguy" << std::endl;
150 }
151
152 void
153 BadGuy::active_update(float elapsed_time)
154 {
155   movement = physic.get_movement(elapsed_time);
156 }
157
158 void
159 BadGuy::inactive_update(float )
160 {
161 }
162
163 void
164 BadGuy::collision_tile(uint32_t tile_attributes)
165 {
166   if(tile_attributes & Tile::HURTS)
167     kill_fall();
168 }
169
170 HitResponse
171 BadGuy::collision(GameObject& other, const CollisionHit& hit)
172 {
173   switch(state) {
174     case STATE_INIT:
175     case STATE_INACTIVE:
176       return ABORT_MOVE;
177     case STATE_ACTIVE: {
178       BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
179       if(badguy && badguy->state == STATE_ACTIVE && badguy->get_group() == COLGROUP_MOVING)
180         return collision_badguy(*badguy, hit);
181
182       Player* player = dynamic_cast<Player*> (&other);
183       if(player)
184         return collision_player(*player, hit);
185
186       Bullet* bullet = dynamic_cast<Bullet*> (&other);
187       if(bullet)
188         return collision_bullet(*bullet, hit);
189
190       return FORCE_MOVE;
191     }
192     case STATE_SQUISHED:
193       return FORCE_MOVE;
194     case STATE_FALLING:
195       return FORCE_MOVE;
196   }
197
198   return ABORT_MOVE;
199 }
200
201 void
202 BadGuy::collision_solid(const CollisionHit& hit)
203 {
204   physic.set_velocity_x(0);
205   physic.set_velocity_y(0);
206   update_on_ground_flag(hit);
207 }
208
209 HitResponse
210 BadGuy::collision_player(Player& player, const CollisionHit& )
211 {
212   // hit from above?
213   if(player.get_bbox().p2.y < (bbox.p1.y + 16)) {
214     // if it's not possible to squish us, then this will hurt
215     if(collision_squished(player)) {
216       return ABORT_MOVE;
217     }
218   }
219
220   if(player.is_invincible()) {
221     kill_fall();
222     return ABORT_MOVE;
223   }
224
225   if(frozen)
226     unfreeze();
227   player.kill(false);
228   return FORCE_MOVE;
229 }
230
231 HitResponse
232 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
233 {
234   return FORCE_MOVE;
235 }
236
237 bool
238 BadGuy::collision_squished(Player& )
239 {
240   return false;
241 }
242
243 HitResponse
244 BadGuy::collision_bullet(Bullet& bullet, const CollisionHit& )
245 {
246   if(frozen) {
247     if(bullet.get_type() == FIRE_BONUS)
248       unfreeze();
249     else
250       return FORCE_MOVE;
251   } else if(bullet.get_type() == ICE_BONUS && is_freezable()) {
252     freeze();
253   } else
254     kill_fall();
255   bullet.remove_me();
256   return ABORT_MOVE;
257 }
258
259 void
260 BadGuy::kill_squished(Player& player)
261 {
262   sound_manager->play("sounds/squish.wav", get_pos());
263   physic.enable_gravity(true);
264   physic.set_velocity_x(0);
265   physic.set_velocity_y(0);
266   set_state(STATE_SQUISHED);
267   set_group(COLGROUP_MOVING_ONLY_STATIC);
268   if (countMe) Sector::current()->get_level()->stats.badguys++;
269   player.bounce(*this);
270 }
271
272 void
273 BadGuy::kill_fall()
274 {
275   sound_manager->play("sounds/fall.wav", get_pos());
276   if (countMe) Sector::current()->get_level()->stats.badguys++;
277   physic.set_velocity_y(0);
278   physic.enable_gravity(true);
279   set_state(STATE_FALLING);
280 }
281
282 void
283 BadGuy::set_state(State state)
284 {
285   if(this->state == state)
286     return;
287
288   State laststate = this->state;
289   this->state = state;
290   switch(state) {
291     case STATE_SQUISHED:
292       state_timer.start(SQUISH_TIME);
293       break;
294     case STATE_ACTIVE:
295       set_group(COLGROUP_MOVING);
296       bbox.set_pos(start_position);
297       break;
298     case STATE_INACTIVE:
299       // was the badguy dead anyway?
300       if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
301         remove_me();
302       }
303       set_group(COLGROUP_DISABLED);
304       break;
305     case STATE_FALLING:
306       set_group(COLGROUP_DISABLED);
307       break;
308     default:
309       break;
310   }
311 }
312
313 bool
314 BadGuy::is_offscreen()
315 {
316   float scroll_x = Sector::current()->camera->get_translation().x;
317   float scroll_y = Sector::current()->camera->get_translation().y;
318
319   if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
320       || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
321       || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
322       || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
323     return true;
324
325   return false;
326 }
327
328 void
329 BadGuy::try_activate()
330 {
331   float scroll_x = Sector::current()->camera->get_translation().x;
332   float scroll_y = Sector::current()->camera->get_translation().y;
333
334   /* Activate badguys if they're just around the screen to avoid
335    * the effect of having badguys suddenly popping up from nowhere.
336    */
337   //Badguy left of screen
338   if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
339       start_position.x < scroll_x - bbox.get_width() &&
340       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
341       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
342     if (start_dir != AUTO) dir = start_dir; else dir = RIGHT;
343     set_state(STATE_ACTIVE);
344     activate();
345   //Badguy right of screen
346   } else if (start_position.x > scroll_x +  SCREEN_WIDTH &&
347       start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
348       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
349       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
350     if (start_dir != AUTO) dir = start_dir; else dir = LEFT;
351     set_state(STATE_ACTIVE);
352     activate();
353   //Badguy over or under screen
354   } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
355        start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
356        ((start_position.y > scroll_y + SCREEN_HEIGHT &&
357          start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) ||
358         (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
359          start_position.y < scroll_y - bbox.get_height()  ))) {
360      if (start_dir != AUTO) dir = start_dir;
361      else{
362          // if nearest player is to our right, start facing right
363          Player* player = get_nearest_player();
364          if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
365              dir = RIGHT;
366          } else {
367                  dir = LEFT;
368          }
369      }
370      set_state(STATE_ACTIVE);
371      activate();
372   } else if(state == STATE_INIT
373       && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
374       && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
375       && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
376       && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
377     if (start_dir != AUTO) {
378       dir = start_dir;
379     } else {
380       // if nearest player is to our right, start facing right
381       Player* player = get_nearest_player();
382       if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
383         dir = RIGHT;
384       } else {
385         dir = LEFT;
386       }
387     }
388     set_state(STATE_ACTIVE);
389     activate();
390   }
391 }
392
393 bool
394 BadGuy::might_fall(int height)
395 {
396   // make sure we check for at least a 1-pixel fall
397   assert(height > 0);
398
399   float x1;
400   float x2;
401   float y1 = bbox.p2.y + 1;
402   float y2 = bbox.p2.y + 1 + height;
403   if (dir == LEFT) {
404     x1 = bbox.p1.x - 1;
405     x2 = bbox.p1.x - 1;
406   } else {
407     x1 = bbox.p2.x + 1;
408     x2 = bbox.p2.x + 1;
409   }
410   return Sector::current()->is_free_of_statics(Rect(x1, y1, x2, y2));
411 }
412
413 Player*
414 BadGuy::get_nearest_player()
415 {
416   // FIXME: does not really return nearest player
417
418   std::vector<Player*> players = Sector::current()->get_players();
419   for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
420     Player* player = *playerIter;
421     return player;
422   }
423
424   return 0;
425 }
426
427 void
428 BadGuy::update_on_ground_flag(const CollisionHit& hit)
429 {
430   if (hit.bottom) on_ground_flag = true;
431 }
432
433 bool
434 BadGuy::on_ground()
435 {
436   return on_ground_flag;
437 }
438
439 void
440 BadGuy::freeze()
441 {
442   set_group(COLGROUP_MOVING_STATIC);
443   frozen = true;
444 }
445
446 void
447 BadGuy::unfreeze()
448 {
449   set_group(COLGROUP_MOVING);
450   frozen = false;
451 }
452
453 bool
454 BadGuy::is_freezable() const
455 {
456   return false;
457 }
458
459 bool
460 BadGuy::is_frozen() const
461 {
462   return frozen;
463 }