26db65d467b2e6e7fad3b7c63b3ec6e98f3806c0
[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), state(STATE_INIT) 
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), state(STATE_INIT) 
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), state(STATE_INIT) 
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
119 Direction
120 BadGuy::str2dir( std::string dir_str )
121 {
122   if( dir_str == "auto" )
123     return AUTO;
124   if( dir_str == "left" )
125     return LEFT;
126   if( dir_str == "right" ) 
127     return RIGHT;
128
129   //default to "auto"
130   log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;    
131   return AUTO;
132 }
133
134 void
135 BadGuy::activate()
136 {
137 }
138
139 void
140 BadGuy::deactivate()
141 {
142 }
143
144 void
145 BadGuy::save(lisp::Writer& )
146 {
147   log_warning << "tried to write out a generic badguy" << std::endl;
148 }
149
150 void
151 BadGuy::active_update(float elapsed_time)
152 {
153   movement = physic.get_movement(elapsed_time);
154 }
155
156 void
157 BadGuy::inactive_update(float )
158 {
159 }
160
161 void
162 BadGuy::collision_tile(uint32_t tile_attributes)
163 {
164   if(tile_attributes & Tile::HURTS)
165     kill_fall();
166 }
167
168 HitResponse
169 BadGuy::collision(GameObject& other, const CollisionHit& hit)
170 {
171   switch(state) {
172     case STATE_INIT:
173     case STATE_INACTIVE:
174       return ABORT_MOVE;
175     case STATE_ACTIVE: {
176       BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
177       if(badguy && badguy->state == STATE_ACTIVE && badguy->get_group() == COLGROUP_MOVING)
178         return collision_badguy(*badguy, hit);
179
180       Player* player = dynamic_cast<Player*> (&other);
181       if(player)
182         return collision_player(*player, hit);
183
184       Bullet* bullet = dynamic_cast<Bullet*> (&other);
185       if(bullet)
186         return collision_bullet(*bullet, hit);
187
188       return FORCE_MOVE;
189     }
190     case STATE_SQUISHED:
191       return FORCE_MOVE;
192     case STATE_FALLING:
193       return FORCE_MOVE;
194   }
195
196   return ABORT_MOVE;
197 }
198
199 void
200 BadGuy::collision_solid(const CollisionHit& )
201 {
202 }
203
204 HitResponse
205 BadGuy::collision_player(Player& player, const CollisionHit& )
206 {
207   // hit from above?
208   if(player.get_bbox().p2.y < (bbox.p1.y + 16)) {
209     // if it's not possible to squish us, then this will hurt
210     if(collision_squished(player)) {
211       return ABORT_MOVE;
212     }
213   }
214
215   if(player.is_invincible()) {
216     kill_fall();
217     return ABORT_MOVE;
218   }
219
220   player.kill(false);
221   return FORCE_MOVE;
222 }
223
224 HitResponse
225 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
226 {
227   return FORCE_MOVE;
228 }
229
230 bool
231 BadGuy::collision_squished(Player& )
232 {
233   return false;
234 }
235
236 HitResponse
237 BadGuy::collision_bullet(Bullet& , const CollisionHit& )
238 {
239   kill_fall();
240   return ABORT_MOVE;
241 }
242
243 void
244 BadGuy::kill_squished(Player& player)
245 {
246   sound_manager->play("sounds/squish.wav", get_pos());
247   physic.enable_gravity(true);
248   physic.set_velocity_x(0);
249   physic.set_velocity_y(0);
250   set_state(STATE_SQUISHED);
251   set_group(COLGROUP_MOVING_ONLY_STATIC);
252   if (countMe) Sector::current()->get_level()->stats.badguys++;
253   player.bounce(*this);
254 }
255
256 void
257 BadGuy::kill_fall()
258 {
259   sound_manager->play("sounds/fall.wav", get_pos());
260   if (countMe) Sector::current()->get_level()->stats.badguys++;
261   physic.set_velocity_y(0);
262   physic.enable_gravity(true);
263   set_state(STATE_FALLING);
264 }
265
266 void
267 BadGuy::set_state(State state)
268 {
269   if(this->state == state)
270     return;
271
272   State laststate = this->state;
273   this->state = state;
274   switch(state) {
275     case STATE_SQUISHED:
276       state_timer.start(SQUISH_TIME);
277       break;
278     case STATE_ACTIVE:
279       set_group(COLGROUP_MOVING);
280       bbox.set_pos(start_position);
281       break;
282     case STATE_INACTIVE:
283       // was the badguy dead anyway?
284       if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
285         remove_me();
286       }
287       set_group(COLGROUP_DISABLED);
288       break;
289     case STATE_FALLING:
290       set_group(COLGROUP_DISABLED);
291       break;
292     default:
293       break;
294   }
295 }
296
297 bool
298 BadGuy::is_offscreen()
299 {
300   float scroll_x = Sector::current()->camera->get_translation().x;
301   float scroll_y = Sector::current()->camera->get_translation().y;
302      
303   if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
304       || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
305       || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
306       || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
307     return true;
308
309   return false;
310 }
311
312 void
313 BadGuy::try_activate()
314 {
315   float scroll_x = Sector::current()->camera->get_translation().x;
316   float scroll_y = Sector::current()->camera->get_translation().y;
317
318   /* Activate badguys if they're just around the screen to avoid
319    * the effect of having badguys suddenly popping up from nowhere.
320    */
321   //Badguy left of screen
322   if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE && 
323       start_position.x < scroll_x - bbox.get_width() &&
324       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
325       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
326     if (start_dir != AUTO) dir = start_dir; else dir = RIGHT;
327     set_state(STATE_ACTIVE);
328     activate();
329   //Badguy right of screen
330   } else if (start_position.x > scroll_x +  SCREEN_WIDTH &&
331       start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
332       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
333       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
334     if (start_dir != AUTO) dir = start_dir; else dir = LEFT;
335     set_state(STATE_ACTIVE);
336     activate();
337   //Badguy over or under screen
338   } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
339        start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
340        ((start_position.y > scroll_y + SCREEN_HEIGHT &&
341          start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) ||
342         (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
343          start_position.y < scroll_y - bbox.get_height()  ))) {
344      if (start_dir != AUTO) dir = start_dir;
345      else{
346          // if nearest player is to our right, start facing right
347          Player* player = get_nearest_player();
348          if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
349              dir = RIGHT;
350          } else {
351                  dir = LEFT;
352          }
353      }
354      set_state(STATE_ACTIVE);
355      activate();
356   } else if(state == STATE_INIT
357       && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
358       && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
359       && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
360       && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
361     if (start_dir != AUTO) {
362       dir = start_dir;
363     } else {
364       // if nearest player is to our right, start facing right
365       Player* player = get_nearest_player();
366       if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
367         dir = RIGHT;
368       } else {
369         dir = LEFT;
370       }
371     }
372     set_state(STATE_ACTIVE);
373     activate();
374   } 
375 }
376
377 bool
378 BadGuy::might_fall(int height)
379 {
380   // make sure we check for at least a 1-pixel fall
381   assert(height > 0);
382
383   float x1;
384   float x2;
385   float y1 = bbox.p2.y + 1;
386   float y2 = bbox.p2.y + 1 + height;
387   if (dir == LEFT) {
388     x1 = bbox.p1.x - 1;
389     x2 = bbox.p1.x - 1;
390   } else {
391     x1 = bbox.p2.x + 1;
392     x2 = bbox.p2.x + 1;
393   }
394   return Sector::current()->is_free_space(Rect(x1, y1, x2, y2));
395 }
396
397 Player* 
398 BadGuy::get_nearest_player()
399 {
400   // FIXME: does not really return nearest player
401
402   std::vector<Player*> players = Sector::current()->get_players();
403   for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
404     Player* player = *playerIter;
405     return player;
406   }
407
408   return 0;
409 }