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"
32 static const float SQUISH_TIME = 2;
33 static const float X_OFFSCREEN_DISTANCE = 1600;
34 static const float Y_OFFSCREEN_DISTANCE = 1200;
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), start_dir(AUTO), state(STATE_INIT)
39 start_position = bbox.p1;
41 sound_manager->preload("sounds/squish.wav");
42 sound_manager->preload("sounds/fall.wav");
45 BadGuy::BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer)
46 : MovingSprite(pos, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(direction), start_dir(direction), state(STATE_INIT)
48 start_position = bbox.p1;
50 sound_manager->preload("sounds/squish.wav");
51 sound_manager->preload("sounds/fall.wav");
54 BadGuy::BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer)
55 : MovingSprite(reader, sprite_name, layer, COLGROUP_DISABLED), countMe(true), dir(LEFT), start_dir(AUTO), state(STATE_INIT)
57 start_position = bbox.p1;
59 std::string dir_str = "auto";
60 reader.get("direction", dir_str);
61 start_dir = str2dir( dir_str );
64 sound_manager->preload("sounds/squish.wav");
65 sound_manager->preload("sounds/fall.wav");
69 BadGuy::draw(DrawingContext& context)
73 if(state == STATE_INIT || state == STATE_INACTIVE)
75 if(state == STATE_FALLING) {
76 DrawingEffect old_effect = context.get_drawing_effect();
77 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
78 sprite->draw(context, get_pos(), layer);
79 context.set_drawing_effect(old_effect);
81 sprite->draw(context, get_pos(), layer);
86 BadGuy::update(float elapsed_time)
88 if(!Sector::current()->inside(bbox)) {
93 if (state == STATE_ACTIVE) deactivate();
94 set_state(STATE_INACTIVE);
99 active_update(elapsed_time);
103 inactive_update(elapsed_time);
107 if(state_timer.check()) {
111 movement = physic.get_movement(elapsed_time);
114 movement = physic.get_movement(elapsed_time);
120 BadGuy::str2dir( std::string dir_str )
122 if( dir_str == "auto" )
124 if( dir_str == "left" )
126 if( dir_str == "right" )
130 log_warning << "Badguy::str2dir: unknown direction \"" << dir_str << "\"" << std::endl;;
145 BadGuy::save(lisp::Writer& )
147 log_warning << "tried to write out a generic badguy" << std::endl;
151 BadGuy::active_update(float elapsed_time)
153 movement = physic.get_movement(elapsed_time);
157 BadGuy::inactive_update(float )
162 BadGuy::collision_tile(uint32_t tile_attributes)
164 if(tile_attributes & Tile::HURTS)
169 BadGuy::collision(GameObject& other, const CollisionHit& hit)
176 if(other.get_flags() & FLAG_SOLID)
177 return collision_solid(other, hit);
179 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
180 if(badguy && badguy->state == STATE_ACTIVE && badguy->get_group() == COLGROUP_MOVING)
181 return collision_badguy(*badguy, hit);
183 Player* player = dynamic_cast<Player*> (&other);
185 return collision_player(*player, hit);
187 Bullet* bullet = dynamic_cast<Bullet*> (&other);
189 return collision_bullet(*bullet, hit);
194 if(other.get_flags() & FLAG_SOLID)
205 BadGuy::collision_solid(GameObject& , const CollisionHit& )
211 BadGuy::collision_player(Player& player, const CollisionHit& )
214 printf("PlayerHit: GT %3.1f PM: %3.1f %3.1f BM: %3.1f %3.1f Hit: %3.1f %3.1f\n",
216 player.get_movement().x, player.get_movement().y,
217 get_movement().x, get_movement().y,
218 hit.normal.x, hit.normal.y);
222 if(player.get_bbox().p2.y < (bbox.p1.y + 16)) {
223 // if it's not possible to squish us, then this will hurt
224 if(collision_squished(player))
228 if(player.is_invincible()) {
238 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
244 BadGuy::collision_squished(Player& )
250 BadGuy::collision_bullet(Bullet& , const CollisionHit& )
257 BadGuy::kill_squished(Player& player)
259 sound_manager->play("sounds/squish.wav", get_pos());
260 physic.enable_gravity(true);
261 physic.set_velocity_x(0);
262 physic.set_velocity_y(0);
263 set_state(STATE_SQUISHED);
264 set_group(COLGROUP_MOVING_ONLY_STATIC);
265 if (countMe) Sector::current()->get_level()->stats.badguys++;
266 player.bounce(*this);
272 sound_manager->play("sounds/fall.wav", get_pos());
273 if (countMe) Sector::current()->get_level()->stats.badguys++;
274 physic.set_velocity_y(0);
275 physic.enable_gravity(true);
276 set_state(STATE_FALLING);
280 BadGuy::set_state(State state)
282 if(this->state == state)
285 State laststate = this->state;
289 state_timer.start(SQUISH_TIME);
292 set_group(COLGROUP_MOVING);
293 bbox.set_pos(start_position);
296 // was the badguy dead anyway?
297 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
300 set_group(COLGROUP_DISABLED);
303 set_group(COLGROUP_DISABLED);
311 BadGuy::is_offscreen()
313 float scroll_x = Sector::current()->camera->get_translation().x;
314 float scroll_y = Sector::current()->camera->get_translation().y;
316 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
317 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
318 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
319 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
326 BadGuy::try_activate()
328 float scroll_x = Sector::current()->camera->get_translation().x;
329 float scroll_y = Sector::current()->camera->get_translation().y;
331 /* Activate badguys if they're just around the screen to avoid
332 * the effect of having badguys suddenly popping up from nowhere.
334 //Badguy left of screen
335 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
336 start_position.x < scroll_x - bbox.get_width() &&
337 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
338 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
339 if (start_dir != AUTO) dir = start_dir; else dir = RIGHT;
340 set_state(STATE_ACTIVE);
342 //Badguy right of screen
343 } else if (start_position.x > scroll_x + SCREEN_WIDTH &&
344 start_position.x < scroll_x + SCREEN_WIDTH + X_OFFSCREEN_DISTANCE &&
345 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
346 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
347 if (start_dir != AUTO) dir = start_dir; else dir = LEFT;
348 set_state(STATE_ACTIVE);
350 //Badguy over or under screen
351 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
352 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
353 ((start_position.y > scroll_y + SCREEN_HEIGHT &&
354 start_position.y < scroll_y + SCREEN_HEIGHT + Y_OFFSCREEN_DISTANCE) ||
355 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
356 start_position.y < scroll_y - bbox.get_height() ))) {
357 if (start_dir != AUTO) dir = start_dir; else dir = start_position.x < scroll_x ? RIGHT : LEFT;
358 set_state(STATE_ACTIVE);
360 } else if(state == STATE_INIT
361 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
362 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
363 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
364 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
365 if (start_dir != AUTO) {
368 // if nearest player is to our right, start facing right
369 Player* player = get_nearest_player();
370 if (player && (player->get_bbox().p1.x > get_bbox().p2.x)) {
376 set_state(STATE_ACTIVE);
382 BadGuy::might_fall(int height)
384 // make sure we check for at least a 1-pixel fall
389 float y1 = bbox.p2.y + 1;
390 float y2 = bbox.p2.y + 1 + height;
398 return Sector::current()->is_free_space(Rect(x1, y1, x2, y2));
402 BadGuy::get_nearest_player()
404 // FIXME: does not really return nearest player
406 std::vector<Player*> players = Sector::current()->get_players();
407 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
408 Player* player = *playerIter;