disable gravity for tux when he is on ground, this improves handling of 1 tile holes
[supertux.git] / src / badguy / badguy.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
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
29 static const float SQUISH_TIME = 2;
30 static const float X_OFFSCREEN_DISTANCE = 1600;
31 static const float Y_OFFSCREEN_DISTANCE = 1200;
32
33 BadGuy::BadGuy()
34   : countMe(true), sprite(0), dir(LEFT), state(STATE_INIT)
35 {
36   set_group(COLGROUP_DISABLED);
37 }
38
39 BadGuy::~BadGuy()
40 {
41   delete sprite;
42 }
43
44 void
45 BadGuy::draw(DrawingContext& context)
46 {
47   if(!sprite)
48     return;
49   if(state == STATE_INIT || state == STATE_INACTIVE)
50     return;
51   if(state == STATE_FALLING) {
52     DrawingEffect old_effect = context.get_drawing_effect();
53     context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
54     sprite->draw(context, get_pos(), LAYER_OBJECTS);
55     context.set_drawing_effect(old_effect);
56   } else {
57     sprite->draw(context, get_pos(), LAYER_OBJECTS);
58   }
59 }
60
61 void
62 BadGuy::update(float elapsed_time)
63 {
64   if(!Sector::current()->inside(bbox)) {
65     remove_me();
66     return;
67   }
68   if(is_offscreen()) {
69     set_state(STATE_INACTIVE);
70   }
71   
72   switch(state) {
73     case STATE_ACTIVE:
74       active_update(elapsed_time);
75       break;
76     case STATE_INIT:
77     case STATE_INACTIVE:
78       inactive_update(elapsed_time);
79       try_activate();
80       break;
81     case STATE_SQUISHED:
82       if(state_timer.check()) {
83         remove_me();
84         break;
85       }
86       movement = physic.get_movement(elapsed_time);
87       break;
88     case STATE_FALLING:
89       movement = physic.get_movement(elapsed_time);
90       break;
91   }
92 }
93
94 void
95 BadGuy::activate()
96 {
97 }
98
99 void
100 BadGuy::deactivate()
101 {
102 }
103
104 void
105 BadGuy::save(lisp::Writer& )
106 {
107         std::cout << "Warning: tried to write out a generic badguy." << std::endl;
108 }
109
110 void
111 BadGuy::active_update(float elapsed_time)
112 {
113   movement = physic.get_movement(elapsed_time);
114 }
115
116 void
117 BadGuy::inactive_update(float )
118 {
119 }
120
121 void
122 BadGuy::collision_tile(uint32_t tile_attributes)
123 {
124   if(tile_attributes & Tile::HURTS)
125     kill_fall();
126 }
127
128 HitResponse
129 BadGuy::collision(GameObject& other, const CollisionHit& hit)
130 {
131   switch(state) {
132     case STATE_INIT:
133     case STATE_INACTIVE:
134       return ABORT_MOVE;
135     case STATE_ACTIVE: {
136       if(other.get_flags() & FLAG_SOLID)
137         return collision_solid(other, hit);
138
139       BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
140       if(badguy && badguy->state == STATE_ACTIVE)
141         return collision_badguy(*badguy, hit);
142
143       Player* player = dynamic_cast<Player*> (&other);
144       if(player)
145         return collision_player(*player, hit);
146
147       return FORCE_MOVE;
148     }
149     case STATE_SQUISHED:
150       if(other.get_flags() & FLAG_SOLID)
151         return CONTINUE;
152       return FORCE_MOVE;
153     case STATE_FALLING:
154       return FORCE_MOVE;
155   }
156
157   return ABORT_MOVE;
158 }
159
160 HitResponse
161 BadGuy::collision_solid(GameObject& , const CollisionHit& )
162 {
163   return FORCE_MOVE;
164 }
165
166 HitResponse
167 BadGuy::collision_player(Player& player, const CollisionHit& )
168 {
169   if(player.is_invincible()) {
170     kill_fall();
171     return ABORT_MOVE;
172   }
173
174   // hit from above?
175   if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
176       (get_bbox().p1.y + get_bbox().p2.y) / 2) {
177     // if it's not possible to squish us, then this will hurt
178     if(collision_squished(player))
179       return CONTINUE;
180   }
181
182   player.kill(Player::SHRINK);
183   return FORCE_MOVE;
184 }
185
186 HitResponse
187 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
188 {
189   return FORCE_MOVE;
190 }
191
192 bool
193 BadGuy::collision_squished(Player& )
194 {
195   return false;
196 }
197
198 void
199 BadGuy::kill_squished(Player& player)
200 {
201   sound_manager->play("sounds/squish.wav", get_pos());
202   physic.enable_gravity(true);
203   physic.set_velocity_x(0);
204   physic.set_velocity_y(0);
205   set_state(STATE_SQUISHED);
206   set_group(COLGROUP_MOVING_ONLY_STATIC);
207   global_stats.add_points(BADGUYS_KILLED_STAT, 1);
208   player.bounce(*this);
209 }
210
211 void
212 BadGuy::kill_fall()
213 {
214   sound_manager->play("sounds/fall.wav", get_pos());
215   global_stats.add_points(BADGUYS_KILLED_STAT, 1);
216   physic.set_velocity_y(0);
217   physic.enable_gravity(true);
218   set_state(STATE_FALLING);
219 }
220
221 void
222 BadGuy::set_state(State state)
223 {
224   if(this->state == state)
225     return;
226
227   State laststate = this->state;
228   this->state = state;
229   switch(state) {
230     case STATE_SQUISHED:
231       state_timer.start(SQUISH_TIME);
232       break;
233     case STATE_ACTIVE:
234       set_group(COLGROUP_MOVING);
235       bbox.set_pos(start_position);
236       break;
237     case STATE_INACTIVE:
238       // was the badguy dead anyway?
239       if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
240         remove_me();
241       }
242       set_group(COLGROUP_DISABLED);
243       break;
244     case STATE_FALLING:
245       set_group(COLGROUP_DISABLED);
246       break;
247     default:
248       break;
249   }
250 }
251
252 bool
253 BadGuy::is_offscreen()
254 {
255   float scroll_x = Sector::current()->camera->get_translation().x;
256   float scroll_y = Sector::current()->camera->get_translation().y;
257      
258   if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
259       || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
260       || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
261       || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
262     return true;
263
264   return false;
265 }
266
267 void
268 BadGuy::try_activate()
269 {
270   float scroll_x = Sector::current()->camera->get_translation().x;
271   float scroll_y = Sector::current()->camera->get_translation().y;
272
273   /* Activate badguys if they're just around the screen to avoid
274    * the effect of having badguys suddenly popping up from nowhere.
275    */
276   if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
277       start_position.x < scroll_x - bbox.get_width() &&
278       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
279       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
280     dir = RIGHT;
281     set_state(STATE_ACTIVE);
282     activate();
283   } else if (start_position.x > scroll_x &&
284       start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
285       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
286       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
287     dir = LEFT;
288     set_state(STATE_ACTIVE);
289     activate();
290   } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
291       start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
292       ((start_position.y > scroll_y &&
293         start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
294        (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
295         start_position.y < scroll_y))) {
296     dir = start_position.x < scroll_x ? RIGHT : LEFT;
297     set_state(STATE_ACTIVE);
298     activate();
299   } else if(state == STATE_INIT
300       && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
301       && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
302       && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
303       && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
304     dir = LEFT;
305     set_state(STATE_ACTIVE);
306     activate();
307   } 
308 }
309
310 bool
311 BadGuy::may_fall_off_platform()
312 {
313   int tile_x, tile_y;
314   // First, let's say the badguy moves 32 units in the
315   // direction it's heading, so do some voodoo maths magic
316   // to determine its future position.
317   Vector pos;
318   if (dir == LEFT)
319     pos = Vector(bbox.p1.x - 32.f, bbox.p2.y);
320   else
321     pos = Vector(bbox.p2.x, bbox.p2.y);
322
323   // Now, snap the badguy's X coordinate to the 32x32/cell grid.
324   if (dir == LEFT) // use the ceiling
325     tile_x = (int)ceilf(pos.x/32.0f);
326   else // use the floor
327     tile_x = (int)floorf(pos.x/32.0f);
328
329   // We might be falling down, so use the ceiling to round upward and
330   // get the lower position. (Positive Y goes downward.)
331   tile_y = (int)ceilf(pos.y/32.0f);
332
333 #if defined(DEBUG_STAY_ON_PLATFORM)
334   // Draw!
335   GameSession::current()->context->draw_filled_rect(Vector(tile_x*32.0f, tile_y*32.0f), Vector(32.f, 32.f), Color(1.f, 0.f, 0.f), 999);
336 #endif
337
338   // Now, if the badguy intersects with a tile, he won't fall off.
339   // If he doesn't intersect, he probably will.
340   // Note that the tile's Y coordinate is offset by +1 from the object's Y.
341   if (Sector::current()->solids->get_tile(tile_x, tile_y)->getAttributes() & FLAG_SOLID)
342   {
343     // It's a solid tile. Good.
344     return false;
345   }
346
347   // Watch out there buddy, you might take a sticky end!
348   return true;
349 }