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.
23 #include "object/camera.hpp"
24 #include "object/tilemap.hpp"
26 #include "statistics.hpp"
27 #include "game_session.hpp"
30 #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;
37 : countMe(true), sprite(0), dir(LEFT), layer(LAYER_OBJECTS), state(STATE_INIT) {
38 set_group(COLGROUP_DISABLED);
41 BadGuy::BadGuy(const BadGuy& other)
42 : MovingObject(other), Serializable(other), countMe(other.countMe), physic(other.physic), activated(other.activated), start_position(other.start_position), dir(other.dir), layer(other.layer), state(other.state), state_timer(other.state_timer)
44 sprite = new Sprite(*other.sprite);
53 BadGuy::draw(DrawingContext& context)
57 if(state == STATE_INIT || state == STATE_INACTIVE)
59 if(state == STATE_FALLING) {
60 DrawingEffect old_effect = context.get_drawing_effect();
61 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
62 sprite->draw(context, get_pos(), layer);
63 context.set_drawing_effect(old_effect);
65 sprite->draw(context, get_pos(), layer);
70 BadGuy::update(float elapsed_time)
72 if(!Sector::current()->inside(bbox)) {
77 set_state(STATE_INACTIVE);
82 active_update(elapsed_time);
86 inactive_update(elapsed_time);
90 if(state_timer.check()) {
94 movement = physic.get_movement(elapsed_time);
97 movement = physic.get_movement(elapsed_time);
113 BadGuy::save(lisp::Writer& )
115 log_warning << "tried to write out a generic badguy" << std::endl;
119 BadGuy::active_update(float elapsed_time)
121 movement = physic.get_movement(elapsed_time);
125 BadGuy::inactive_update(float )
130 BadGuy::collision_tile(uint32_t tile_attributes)
132 if(tile_attributes & Tile::HURTS)
137 BadGuy::collision(GameObject& other, const CollisionHit& hit)
144 if(other.get_flags() & FLAG_SOLID)
145 return collision_solid(other, hit);
147 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
148 if(badguy && badguy->state == STATE_ACTIVE)
149 return collision_badguy(*badguy, hit);
151 Player* player = dynamic_cast<Player*> (&other);
153 return collision_player(*player, hit);
155 Bullet* bullet = dynamic_cast<Bullet*> (&other);
157 return collision_bullet(*bullet, hit);
162 if(other.get_flags() & FLAG_SOLID)
173 BadGuy::collision_solid(GameObject& , const CollisionHit& )
179 BadGuy::collision_player(Player& player, const CollisionHit& )
182 printf("PlayerHit: GT %3.1f PM: %3.1f %3.1f BM: %3.1f %3.1f Hit: %3.1f %3.1f\n",
184 player.get_movement().x, player.get_movement().y,
185 get_movement().x, get_movement().y,
186 hit.normal.x, hit.normal.y);
190 if(player.get_bbox().p2.y < (bbox.p1.y + 16)) {
191 // if it's not possible to squish us, then this will hurt
192 if(collision_squished(player))
196 if(player.is_invincible()) {
206 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
212 BadGuy::collision_squished(Player& )
218 BadGuy::collision_bullet(Bullet& , const CollisionHit& )
225 BadGuy::kill_squished(Player& player)
227 sound_manager->play("sounds/squish.wav", get_pos());
228 physic.enable_gravity(true);
229 physic.set_velocity_x(0);
230 physic.set_velocity_y(0);
231 set_state(STATE_SQUISHED);
232 set_group(COLGROUP_MOVING_ONLY_STATIC);
233 if (countMe) Sector::current()->get_level()->stats.badguys++;
234 player.bounce(*this);
240 sound_manager->play("sounds/fall.wav", get_pos());
241 if (countMe) Sector::current()->get_level()->stats.badguys++;
242 physic.set_velocity_y(0);
243 physic.enable_gravity(true);
244 set_state(STATE_FALLING);
248 BadGuy::set_state(State state)
250 if(this->state == state)
253 State laststate = this->state;
257 state_timer.start(SQUISH_TIME);
260 set_group(COLGROUP_MOVING);
261 bbox.set_pos(start_position);
264 // was the badguy dead anyway?
265 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
268 set_group(COLGROUP_DISABLED);
271 set_group(COLGROUP_DISABLED);
279 BadGuy::is_offscreen()
281 float scroll_x = Sector::current()->camera->get_translation().x;
282 float scroll_y = Sector::current()->camera->get_translation().y;
284 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
285 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
286 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
287 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
294 BadGuy::try_activate()
296 float scroll_x = Sector::current()->camera->get_translation().x;
297 float scroll_y = Sector::current()->camera->get_translation().y;
299 /* Activate badguys if they're just around the screen to avoid
300 * the effect of having badguys suddenly popping up from nowhere.
302 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
303 start_position.x < scroll_x - bbox.get_width() &&
304 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
305 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
307 set_state(STATE_ACTIVE);
309 } else if (start_position.x > scroll_x &&
310 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
311 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
312 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
314 set_state(STATE_ACTIVE);
316 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
317 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
318 ((start_position.y > scroll_y &&
319 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
320 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
321 start_position.y < scroll_y))) {
322 dir = start_position.x < scroll_x ? RIGHT : LEFT;
323 set_state(STATE_ACTIVE);
325 } else if(state == STATE_INIT
326 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
327 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
328 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
329 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
331 set_state(STATE_ACTIVE);
337 BadGuy::might_fall(int height)
339 // make sure we check for at least a 1-pixel fall
344 float y1 = bbox.p2.y + 1;
345 float y2 = bbox.p2.y + 1 + height;
353 return Sector::current()->is_free_space(Rect(x1, y1, x2, y2));
357 BadGuy::get_nearest_player()
359 // FIXME: does not really return nearest player
361 std::vector<Player*> players = Sector::current()->get_players();
362 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
363 Player* player = *playerIter;