fade out console
[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 #include "log.hpp"
29
30 static const float SQUISH_TIME = 2;
31 static const float X_OFFSCREEN_DISTANCE = 1600;
32 static const float Y_OFFSCREEN_DISTANCE = 1200;
33
34 BadGuy::BadGuy()
35   : countMe(true), sprite(0), remove_out_of_bounds(true), dir(LEFT), state(STATE_INIT)
36 {
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_OBJECTS);
56     context.set_drawing_effect(old_effect);
57   } else {
58     sprite->draw(context, get_pos(), LAYER_OBJECTS);
59   }
60 }
61
62 void
63 BadGuy::update(float elapsed_time)
64 {
65   if(!Sector::current()->inside(bbox) && remove_out_of_bounds) {
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& hit)
169 {
170   if(player.is_invincible()) {
171     kill_fall();
172     return ABORT_MOVE;
173   }
174
175   printf("PlayerHit: GT %3.1f PM: %3.1f %3.1f BM: %3.1f %3.1f Hit: %3.1f %3.1f\n",
176           game_time,
177           player.get_movement().x, player.get_movement().y,
178           get_movement().x, get_movement().y,
179           hit.normal.x, hit.normal.y);
180   // hit from above?
181   if(player.get_movement().y /*- get_movement().y*/ > 0 
182           && player.get_bbox().p2.y <
183       (get_bbox().p1.y + get_bbox().p2.y) / 2) {
184     // if it's not possible to squish us, then this will hurt
185     if(collision_squished(player))
186       return ABORT_MOVE;
187   }
188
189   player.kill(Player::SHRINK);
190   return FORCE_MOVE;
191 }
192
193 HitResponse
194 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
195 {
196   return FORCE_MOVE;
197 }
198
199 bool
200 BadGuy::collision_squished(Player& )
201 {
202   return false;
203 }
204
205 void
206 BadGuy::kill_squished(Player& player)
207 {
208   sound_manager->play("sounds/squish.wav", get_pos());
209   physic.enable_gravity(true);
210   physic.set_velocity_x(0);
211   physic.set_velocity_y(0);
212   set_state(STATE_SQUISHED);
213   set_group(COLGROUP_MOVING_ONLY_STATIC);
214   global_stats.add_points(BADGUYS_KILLED_STAT, 1);
215   player.bounce(*this);
216 }
217
218 void
219 BadGuy::kill_fall()
220 {
221   sound_manager->play("sounds/fall.wav", get_pos());
222   global_stats.add_points(BADGUYS_KILLED_STAT, 1);
223   physic.set_velocity_y(0);
224   physic.enable_gravity(true);
225   set_state(STATE_FALLING);
226 }
227
228 void
229 BadGuy::set_state(State state)
230 {
231   if(this->state == state)
232     return;
233
234   State laststate = this->state;
235   this->state = state;
236   switch(state) {
237     case STATE_SQUISHED:
238       state_timer.start(SQUISH_TIME);
239       break;
240     case STATE_ACTIVE:
241       set_group(COLGROUP_MOVING);
242       bbox.set_pos(start_position);
243       break;
244     case STATE_INACTIVE:
245       // was the badguy dead anyway?
246       if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
247         remove_me();
248       }
249       set_group(COLGROUP_DISABLED);
250       break;
251     case STATE_FALLING:
252       set_group(COLGROUP_DISABLED);
253       break;
254     default:
255       break;
256   }
257 }
258
259 bool
260 BadGuy::is_offscreen()
261 {
262   float scroll_x = Sector::current()->camera->get_translation().x;
263   float scroll_y = Sector::current()->camera->get_translation().y;
264      
265   if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
266       || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
267       || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
268       || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
269     return true;
270
271   return false;
272 }
273
274 void
275 BadGuy::try_activate()
276 {
277   float scroll_x = Sector::current()->camera->get_translation().x;
278   float scroll_y = Sector::current()->camera->get_translation().y;
279
280   /* Activate badguys if they're just around the screen to avoid
281    * the effect of having badguys suddenly popping up from nowhere.
282    */
283   if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
284       start_position.x < scroll_x - bbox.get_width() &&
285       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
286       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
287     dir = RIGHT;
288     set_state(STATE_ACTIVE);
289     activate();
290   } else if (start_position.x > scroll_x &&
291       start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
292       start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
293       start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
294     dir = LEFT;
295     set_state(STATE_ACTIVE);
296     activate();
297   } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
298       start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
299       ((start_position.y > scroll_y &&
300         start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
301        (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
302         start_position.y < scroll_y))) {
303     dir = start_position.x < scroll_x ? RIGHT : LEFT;
304     set_state(STATE_ACTIVE);
305     activate();
306   } else if(state == STATE_INIT
307       && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
308       && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
309       && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
310       && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
311     dir = LEFT;
312     set_state(STATE_ACTIVE);
313     activate();
314   } 
315 }
316
317 bool
318 BadGuy::may_fall_off_platform()
319 {
320   int tile_x, tile_y;
321   // First, let's say the badguy moves 32 units in the
322   // direction it's heading, so do some voodoo maths magic
323   // to determine its future position.
324   Vector pos;
325   if (dir == LEFT)
326     pos = Vector(bbox.p1.x - 32.f, bbox.p2.y);
327   else
328     pos = Vector(bbox.p2.x, bbox.p2.y);
329
330   // Now, snap the badguy's X coordinate to the 32x32/cell grid.
331   if (dir == LEFT) // use the ceiling
332     tile_x = (int)ceilf(pos.x/32.0f);
333   else // use the floor
334     tile_x = (int)floorf(pos.x/32.0f);
335
336   // We might be falling down, so use the ceiling to round upward and
337   // get the lower position. (Positive Y goes downward.)
338   tile_y = (int)ceilf(pos.y/32.0f);
339
340 #if defined(DEBUG_STAY_ON_PLATFORM)
341   // Draw!
342   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);
343 #endif
344
345   // Now, if the badguy intersects with a tile, he won't fall off.
346   // If he doesn't intersect, he probably will.
347   // Note that the tile's Y coordinate is offset by +1 from the object's Y.
348   if (Sector::current()->solids->get_tile(tile_x, tile_y)->getAttributes() & FLAG_SOLID)
349   {
350     // It's a solid tile. Good.
351     return false;
352   }
353
354   // Watch out there buddy, you might take a sticky end!
355   return true;
356 }
357
358 Player* 
359 BadGuy::get_nearest_player()
360 {
361   // FIXME: does not really return nearest player
362
363   std::vector<Player*> players = Sector::current()->get_players();
364   for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
365     Player* player = *playerIter;
366     return player;
367   }
368
369   return 0;
370 }