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