4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
22 #include "object/camera.hpp"
23 #include "object/tilemap.hpp"
25 #include "statistics.hpp"
26 #include "game_session.hpp"
29 #include "object/bullet.hpp"
31 static const float SQUISH_TIME = 2;
32 static const float X_OFFSCREEN_DISTANCE = 1600;
33 static const float Y_OFFSCREEN_DISTANCE = 1200;
35 BadGuy::BadGuy(const Vector& pos, const std::string& sprite_name, int layer)
36 : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), state(STATE_INIT)
38 start_position = bbox.p1;
40 sound_manager->preload("sounds/squish.wav");
41 sound_manager->preload("sounds/fall.wav");
44 BadGuy::BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer)
45 : MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), state(STATE_INIT)
47 start_position = bbox.p1;
49 sound_manager->preload("sounds/squish.wav");
50 sound_manager->preload("sounds/fall.wav");
54 BadGuy::draw(DrawingContext& context)
58 if(state == STATE_INIT || state == STATE_INACTIVE)
60 if(state == STATE_FALLING) {
61 DrawingEffect old_effect = context.get_drawing_effect();
62 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
63 sprite->draw(context, get_pos(), layer);
64 context.set_drawing_effect(old_effect);
66 sprite->draw(context, get_pos(), layer);
71 BadGuy::update(float elapsed_time)
73 if(!Sector::current()->inside(bbox)) {
78 set_state(STATE_INACTIVE);
83 active_update(elapsed_time);
87 inactive_update(elapsed_time);
91 if(state_timer.check()) {
95 movement = physic.get_movement(elapsed_time);
98 movement = physic.get_movement(elapsed_time);
114 BadGuy::save(lisp::Writer& )
116 log_warning << "tried to write out a generic badguy" << std::endl;
120 BadGuy::active_update(float elapsed_time)
122 movement = physic.get_movement(elapsed_time);
126 BadGuy::inactive_update(float )
131 BadGuy::collision_tile(uint32_t tile_attributes)
133 if(tile_attributes & Tile::HURTS)
138 BadGuy::collision(GameObject& other, const CollisionHit& hit)
145 if(other.get_flags() & FLAG_SOLID)
146 return collision_solid(other, hit);
148 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
149 if(badguy && badguy->state == STATE_ACTIVE)
150 return collision_badguy(*badguy, hit);
152 Player* player = dynamic_cast<Player*> (&other);
154 return collision_player(*player, hit);
156 Bullet* bullet = dynamic_cast<Bullet*> (&other);
158 return collision_bullet(*bullet, hit);
163 if(other.get_flags() & FLAG_SOLID)
174 BadGuy::collision_solid(GameObject& , const CollisionHit& )
180 BadGuy::collision_player(Player& player, const CollisionHit& )
183 printf("PlayerHit: GT %3.1f PM: %3.1f %3.1f BM: %3.1f %3.1f Hit: %3.1f %3.1f\n",
185 player.get_movement().x, player.get_movement().y,
186 get_movement().x, get_movement().y,
187 hit.normal.x, hit.normal.y);
191 if(player.get_bbox().p2.y < (bbox.p1.y + 16)) {
192 // if it's not possible to squish us, then this will hurt
193 if(collision_squished(player))
197 if(player.is_invincible()) {
207 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
213 BadGuy::collision_squished(Player& )
219 BadGuy::collision_bullet(Bullet& , const CollisionHit& )
226 BadGuy::kill_squished(Player& player)
228 sound_manager->play("sounds/squish.wav", get_pos());
229 physic.enable_gravity(true);
230 physic.set_velocity_x(0);
231 physic.set_velocity_y(0);
232 set_state(STATE_SQUISHED);
233 set_group(COLGROUP_MOVING_ONLY_STATIC);
234 if (countMe) Sector::current()->get_level()->stats.badguys++;
235 player.bounce(*this);
241 sound_manager->play("sounds/fall.wav", get_pos());
242 if (countMe) Sector::current()->get_level()->stats.badguys++;
243 physic.set_velocity_y(0);
244 physic.enable_gravity(true);
245 set_state(STATE_FALLING);
249 BadGuy::set_state(State state)
251 if(this->state == state)
254 State laststate = this->state;
258 state_timer.start(SQUISH_TIME);
261 set_group(COLGROUP_MOVING);
262 bbox.set_pos(start_position);
265 // was the badguy dead anyway?
266 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
269 set_group(COLGROUP_DISABLED);
272 set_group(COLGROUP_DISABLED);
280 BadGuy::is_offscreen()
282 float scroll_x = Sector::current()->camera->get_translation().x;
283 float scroll_y = Sector::current()->camera->get_translation().y;
285 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
286 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
287 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
288 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
295 BadGuy::try_activate()
297 float scroll_x = Sector::current()->camera->get_translation().x;
298 float scroll_y = Sector::current()->camera->get_translation().y;
300 /* Activate badguys if they're just around the screen to avoid
301 * the effect of having badguys suddenly popping up from nowhere.
303 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
304 start_position.x < scroll_x - bbox.get_width() &&
305 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
306 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
308 set_state(STATE_ACTIVE);
310 } else if (start_position.x > scroll_x &&
311 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
312 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
313 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
315 set_state(STATE_ACTIVE);
317 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
318 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
319 ((start_position.y > scroll_y &&
320 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
321 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
322 start_position.y < scroll_y))) {
323 dir = start_position.x < scroll_x ? RIGHT : LEFT;
324 set_state(STATE_ACTIVE);
326 } else if(state == STATE_INIT
327 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
328 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
329 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
330 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
332 set_state(STATE_ACTIVE);
338 BadGuy::might_fall(int height)
340 // make sure we check for at least a 1-pixel fall
345 float y1 = bbox.p2.y + 1;
346 float y2 = bbox.p2.y + 1 + height;
354 return Sector::current()->is_free_space(Rect(x1, y1, x2, y2));
358 BadGuy::get_nearest_player()
360 // FIXME: does not really return nearest player
362 std::vector<Player*> players = Sector::current()->get_players();
363 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
364 Player* player = *playerIter;