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