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