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