Support for initial direction "left"|"right"|"auto" in Dispenser,
[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), 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 lisp::Lisp& reader, const std::string& sprite_name, int layer)
46   : MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), 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 void
55 BadGuy::draw(DrawingContext& context)
56 {
57   if(!sprite)
58     return;
59   if(state == STATE_INIT || state == STATE_INACTIVE)
60     return;
61   if(state == STATE_FALLING) {
62     DrawingEffect old_effect = context.get_drawing_effect();
63     context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
64     sprite->draw(context, get_pos(), layer);
65     context.set_drawing_effect(old_effect);
66   } else {
67     sprite->draw(context, get_pos(), layer);
68   }
69 }
70
71 void
72 BadGuy::update(float elapsed_time)
73 {
74   if(!Sector::current()->inside(bbox)) {
75     remove_me();
76     return;
77   }
78   if(is_offscreen()) {
79     if (state == STATE_ACTIVE) deactivate();
80     set_state(STATE_INACTIVE);
81   }
82   
83   switch(state) {
84     case STATE_ACTIVE:
85       active_update(elapsed_time);
86       break;
87     case STATE_INIT:
88     case STATE_INACTIVE:
89       inactive_update(elapsed_time);
90       try_activate();
91       break;
92     case STATE_SQUISHED:
93       if(state_timer.check()) {
94         remove_me();
95         break;
96       }
97       movement = physic.get_movement(elapsed_time);
98       break;
99     case STATE_FALLING:
100       movement = physic.get_movement(elapsed_time);
101       break;
102   }
103 }
104
105 Direction
106 BadGuy::str2dir( std::string dir_str )
107 {
108   if( dir_str == "auto" || dir_str == "" )
109     return dir;
110   if( dir_str == "left" )
111     return LEFT;
112   if( dir_str == "right" ) 
113     return RIGHT;
114
115   //default to "auto"
116   log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"\n";    
117   return dir;
118 }
119
120 void
121 BadGuy::activate()
122 {
123 }
124
125 void
126 BadGuy::deactivate()
127 {
128 }
129
130 void
131 BadGuy::save(lisp::Writer& )
132 {
133         log_warning << "tried to write out a generic badguy" << std::endl;
134 }
135
136 void
137 BadGuy::active_update(float elapsed_time)
138 {
139   movement = physic.get_movement(elapsed_time);
140 }
141
142 void
143 BadGuy::inactive_update(float )
144 {
145 }
146
147 void
148 BadGuy::collision_tile(uint32_t tile_attributes)
149 {
150   if(tile_attributes & Tile::HURTS)
151     kill_fall();
152 }
153
154 HitResponse
155 BadGuy::collision(GameObject& other, const CollisionHit& hit)
156 {
157   switch(state) {
158     case STATE_INIT:
159     case STATE_INACTIVE:
160       return ABORT_MOVE;
161     case STATE_ACTIVE: {
162       if(other.get_flags() & FLAG_SOLID)
163         return collision_solid(other, hit);
164
165       BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
166       if(badguy && badguy->state == STATE_ACTIVE && badguy->get_group() == COLGROUP_MOVING)
167         return collision_badguy(*badguy, hit);
168
169       Player* player = dynamic_cast<Player*> (&other);
170       if(player)
171         return collision_player(*player, hit);
172
173       Bullet* bullet = dynamic_cast<Bullet*> (&other);
174       if(bullet)
175         return collision_bullet(*bullet, hit);
176
177       return FORCE_MOVE;
178     }
179     case STATE_SQUISHED:
180       if(other.get_flags() & FLAG_SOLID)
181         return CONTINUE;
182       return FORCE_MOVE;
183     case STATE_FALLING:
184       return FORCE_MOVE;
185   }
186
187   return ABORT_MOVE;
188 }
189
190 HitResponse
191 BadGuy::collision_solid(GameObject& , const CollisionHit& )
192 {
193   return FORCE_MOVE;
194 }
195
196 HitResponse
197 BadGuy::collision_player(Player& player, const CollisionHit& )
198 {
199   /*
200   printf("PlayerHit: GT %3.1f PM: %3.1f %3.1f BM: %3.1f %3.1f Hit: %3.1f %3.1f\n",
201           game_time,
202           player.get_movement().x, player.get_movement().y,
203           get_movement().x, get_movement().y,
204           hit.normal.x, hit.normal.y);
205   */
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   if(player.is_invincible()) {
215     kill_fall();
216     return ABORT_MOVE;
217   }
218
219   player.kill(false);
220   return FORCE_MOVE;
221 }
222
223 HitResponse
224 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
225 {
226   return FORCE_MOVE;
227 }
228
229 bool
230 BadGuy::collision_squished(Player& )
231 {
232   return false;
233 }
234
235 HitResponse
236 BadGuy::collision_bullet(Bullet& , const CollisionHit& )
237 {
238   kill_fall();
239   return ABORT_MOVE;
240 }
241
242 void
243 BadGuy::kill_squished(Player& player)
244 {
245   sound_manager->play("sounds/squish.wav", get_pos());
246   physic.enable_gravity(true);
247   physic.set_velocity_x(0);
248   physic.set_velocity_y(0);
249   set_state(STATE_SQUISHED);
250   set_group(COLGROUP_MOVING_ONLY_STATIC);
251   if (countMe) Sector::current()->get_level()->stats.badguys++;
252   player.bounce(*this);
253 }
254
255 void
256 BadGuy::kill_fall()
257 {
258   sound_manager->play("sounds/fall.wav", get_pos());
259   if (countMe) Sector::current()->get_level()->stats.badguys++;
260   physic.set_velocity_y(0);
261   physic.enable_gravity(true);
262   set_state(STATE_FALLING);
263 }
264
265 void
266 BadGuy::set_state(State state)
267 {
268   if(this->state == state)
269     return;
270
271   State laststate = this->state;
272   this->state = state;
273   switch(state) {
274     case STATE_SQUISHED:
275       state_timer.start(SQUISH_TIME);
276       break;
277     case STATE_ACTIVE:
278       set_group(COLGROUP_MOVING);
279       bbox.set_pos(start_position);
280       break;
281     case STATE_INACTIVE:
282       // was the badguy dead anyway?
283       if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
284         remove_me();
285       }
286       set_group(COLGROUP_DISABLED);
287       break;
288     case STATE_FALLING:
289       set_group(COLGROUP_DISABLED);
290       break;
291     default:
292       break;
293   }
294 }
295
296 bool
297 BadGuy::is_offscreen()
298 {
299   float scroll_x = Sector::current()->camera->get_translation().x;
300   float scroll_y = Sector::current()->camera->get_translation().y;
301      
302   if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
303       || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
304       || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
305       || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
306     return true;
307
308   return false;
309 }
310
311 void
312 BadGuy::try_activate()
313 {
314   float scroll_x = Sector::current()->camera->get_translation().x;
315   float scroll_y = Sector::current()->camera->get_translation().y;
316
317   /* Activate badguys if they're just around the screen to avoid
318    * the effect of having badguys suddenly popping up from nowhere.
319    */
320   //Badguy left of screen
321   if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE && 
322       start_position.x < scroll_x - bbox.get_width() &&
323       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
324       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
325     dir = RIGHT;
326     set_state(STATE_ACTIVE);
327     activate();
328   //Badguy right of screen
329   } else if (start_position.x > scroll_x +  SCREEN_WIDTH &&
330       start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
331       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
332       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
333     dir = LEFT;
334     set_state(STATE_ACTIVE);
335     activate();
336   //Badguy over or under screen
337   } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
338        start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
339        ((start_position.y > scroll_y + SCREEN_HEIGHT &&
340          start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) ||
341         (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
342          start_position.y < scroll_y - bbox.get_height()  ))) {
343      dir = start_position.x < scroll_x ? RIGHT : LEFT;
344      set_state(STATE_ACTIVE);
345      activate();
346   } else if(state == STATE_INIT
347       && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
348       && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
349       && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
350       && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
351     dir = LEFT;
352     set_state(STATE_ACTIVE);
353     activate();
354   } 
355 }
356
357 bool
358 BadGuy::might_fall(int height)
359 {
360   // make sure we check for at least a 1-pixel fall
361   assert(height > 0);
362
363   float x1;
364   float x2;
365   float y1 = bbox.p2.y + 1;
366   float y2 = bbox.p2.y + 1 + height;
367   if (dir == LEFT) {
368     x1 = bbox.p1.x - 1;
369     x2 = bbox.p1.x - 1;
370   } else {
371     x1 = bbox.p2.x + 1;
372     x2 = bbox.p2.x + 1;
373   }
374   return Sector::current()->is_free_space(Rect(x1, y1, x2, y2));
375 }
376
377 Player* 
378 BadGuy::get_nearest_player()
379 {
380   // FIXME: does not really return nearest player
381
382   std::vector<Player*> players = Sector::current()->get_players();
383   for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
384     Player* player = *playerIter;
385     return player;
386   }
387
388   return 0;
389 }