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 if (state == STATE_ACTIVE) deactivate();
79 set_state(STATE_INACTIVE);
84 active_update(elapsed_time);
88 inactive_update(elapsed_time);
92 if(state_timer.check()) {
96 movement = physic.get_movement(elapsed_time);
99 movement = physic.get_movement(elapsed_time);
115 BadGuy::save(lisp::Writer& )
117 log_warning << "tried to write out a generic badguy" << std::endl;
121 BadGuy::active_update(float elapsed_time)
123 movement = physic.get_movement(elapsed_time);
127 BadGuy::inactive_update(float )
132 BadGuy::collision_tile(uint32_t tile_attributes)
134 if(tile_attributes & Tile::HURTS)
139 BadGuy::collision(GameObject& other, const CollisionHit& hit)
146 if(other.get_flags() & FLAG_SOLID)
147 return collision_solid(other, hit);
149 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
150 if(badguy && badguy->state == STATE_ACTIVE && badguy->get_group() == COLGROUP_MOVING)
151 return collision_badguy(*badguy, hit);
153 Player* player = dynamic_cast<Player*> (&other);
155 return collision_player(*player, hit);
157 Bullet* bullet = dynamic_cast<Bullet*> (&other);
159 return collision_bullet(*bullet, hit);
164 if(other.get_flags() & FLAG_SOLID)
175 BadGuy::collision_solid(GameObject& , const CollisionHit& )
181 BadGuy::collision_player(Player& player, const CollisionHit& )
184 printf("PlayerHit: GT %3.1f PM: %3.1f %3.1f BM: %3.1f %3.1f Hit: %3.1f %3.1f\n",
186 player.get_movement().x, player.get_movement().y,
187 get_movement().x, get_movement().y,
188 hit.normal.x, hit.normal.y);
192 if(player.get_bbox().p2.y < (bbox.p1.y + 16)) {
193 // if it's not possible to squish us, then this will hurt
194 if(collision_squished(player))
198 if(player.is_invincible()) {
208 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
214 BadGuy::collision_squished(Player& )
220 BadGuy::collision_bullet(Bullet& , const CollisionHit& )
227 BadGuy::kill_squished(Player& player)
229 sound_manager->play("sounds/squish.wav", get_pos());
230 physic.enable_gravity(true);
231 physic.set_velocity_x(0);
232 physic.set_velocity_y(0);
233 set_state(STATE_SQUISHED);
234 set_group(COLGROUP_MOVING_ONLY_STATIC);
235 if (countMe) Sector::current()->get_level()->stats.badguys++;
236 player.bounce(*this);
242 sound_manager->play("sounds/fall.wav", get_pos());
243 if (countMe) Sector::current()->get_level()->stats.badguys++;
244 physic.set_velocity_y(0);
245 physic.enable_gravity(true);
246 set_state(STATE_FALLING);
250 BadGuy::set_state(State state)
252 if(this->state == state)
255 State laststate = this->state;
259 state_timer.start(SQUISH_TIME);
262 set_group(COLGROUP_MOVING);
263 bbox.set_pos(start_position);
266 // was the badguy dead anyway?
267 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
270 set_group(COLGROUP_DISABLED);
273 set_group(COLGROUP_DISABLED);
281 BadGuy::is_offscreen()
283 float scroll_x = Sector::current()->camera->get_translation().x;
284 float scroll_y = Sector::current()->camera->get_translation().y;
286 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
287 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
288 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
289 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
296 BadGuy::try_activate()
298 float scroll_x = Sector::current()->camera->get_translation().x;
299 float scroll_y = Sector::current()->camera->get_translation().y;
301 /* Activate badguys if they're just around the screen to avoid
302 * the effect of having badguys suddenly popping up from nowhere.
304 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
305 start_position.x < scroll_x - bbox.get_width() &&
306 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
307 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
309 set_state(STATE_ACTIVE);
311 } else if (start_position.x > scroll_x &&
312 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
313 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
314 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
316 set_state(STATE_ACTIVE);
318 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
319 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
320 ((start_position.y > scroll_y &&
321 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
322 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
323 start_position.y < scroll_y))) {
324 dir = start_position.x < scroll_x ? RIGHT : LEFT;
325 set_state(STATE_ACTIVE);
327 } else if(state == STATE_INIT
328 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
329 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
330 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
331 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
333 set_state(STATE_ACTIVE);
339 BadGuy::might_fall(int height)
341 // make sure we check for at least a 1-pixel fall
346 float y1 = bbox.p2.y + 1;
347 float y2 = bbox.p2.y + 1 + height;
355 return Sector::current()->is_free_space(Rect(x1, y1, x2, y2));
359 BadGuy::get_nearest_player()
361 // FIXME: does not really return nearest player
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;