4 // Copyright (C) 2005 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
23 #include "object/camera.hpp"
24 #include "object/tilemap.hpp"
26 #include "statistics.hpp"
28 static const float SQUISH_TIME = 2;
29 static const float X_OFFSCREEN_DISTANCE = 1600;
30 static const float Y_OFFSCREEN_DISTANCE = 1200;
33 : countMe(true), sprite(0), dir(LEFT), state(STATE_INIT)
35 set_group(COLGROUP_DISABLED);
44 BadGuy::draw(DrawingContext& context)
48 if(state == STATE_INIT || state == STATE_INACTIVE)
50 if(state == STATE_FALLING) {
51 DrawingEffect old_effect = context.get_drawing_effect();
52 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
53 sprite->draw(context, get_pos(), LAYER_OBJECTS);
54 context.set_drawing_effect(old_effect);
56 sprite->draw(context, get_pos(), LAYER_OBJECTS);
61 BadGuy::update(float elapsed_time)
63 if(!Sector::current()->inside(bbox)) {
68 set_state(STATE_INACTIVE);
73 active_update(elapsed_time);
77 inactive_update(elapsed_time);
81 if(state_timer.check()) {
85 movement = physic.get_movement(elapsed_time);
88 movement = physic.get_movement(elapsed_time);
104 BadGuy::save(lisp::Writer& )
106 std::cout << "Warning: tried to write out a generic badguy." << std::endl;
110 BadGuy::active_update(float elapsed_time)
112 movement = physic.get_movement(elapsed_time);
116 BadGuy::inactive_update(float )
121 BadGuy::collision_tile(uint32_t tile_attributes)
123 if(tile_attributes & Tile::HURTS)
128 BadGuy::collision(GameObject& other, const CollisionHit& hit)
135 if(other.get_flags() & FLAG_SOLID)
136 return collision_solid(other, hit);
138 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
139 if(badguy && badguy->state == STATE_ACTIVE)
140 return collision_badguy(*badguy, hit);
142 Player* player = dynamic_cast<Player*> (&other);
144 return collision_player(*player, hit);
149 if(other.get_flags() & FLAG_SOLID)
160 BadGuy::collision_solid(GameObject& , const CollisionHit& )
166 BadGuy::collision_player(Player& player, const CollisionHit& )
168 if(player.is_invincible()) {
173 if(player.get_movement().y - get_movement().y > 0 && player.get_bbox().p2.y <
174 (get_bbox().p1.y + get_bbox().p2.y) / 2) {
175 // if it's not possible to squish us, then this will hurt
176 if(!collision_squished(player))
177 player.kill(Player::SHRINK);
181 player.kill(Player::SHRINK);
186 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
192 BadGuy::collision_squished(Player& )
198 BadGuy::kill_squished(Player& player)
200 sound_manager->play("sounds/squish.wav", get_pos());
201 physic.enable_gravity(true);
202 physic.set_velocity_x(0);
203 physic.set_velocity_y(0);
204 set_state(STATE_SQUISHED);
205 set_group(COLGROUP_MOVING_ONLY_STATIC);
206 global_stats.add_points(BADGUYS_KILLED_STAT, 1);
207 player.bounce(*this);
213 sound_manager->play("sounds/fall.wav", get_pos());
214 global_stats.add_points(BADGUYS_KILLED_STAT, 1);
215 physic.set_velocity_y(0);
216 physic.enable_gravity(true);
217 set_state(STATE_FALLING);
221 BadGuy::set_state(State state)
223 if(this->state == state)
226 State laststate = this->state;
230 state_timer.start(SQUISH_TIME);
233 set_group(COLGROUP_MOVING);
234 bbox.set_pos(start_position);
237 // was the badguy dead anyway?
238 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
241 set_group(COLGROUP_DISABLED);
244 set_group(COLGROUP_DISABLED);
252 BadGuy::is_offscreen()
254 float scroll_x = Sector::current()->camera->get_translation().x;
255 float scroll_y = Sector::current()->camera->get_translation().y;
257 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
258 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
259 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
260 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
267 BadGuy::try_activate()
269 float scroll_x = Sector::current()->camera->get_translation().x;
270 float scroll_y = Sector::current()->camera->get_translation().y;
272 /* Activate badguys if they're just around the screen to avoid
273 * the effect of having badguys suddenly popping up from nowhere.
275 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
276 start_position.x < scroll_x - bbox.get_width() &&
277 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
278 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
280 set_state(STATE_ACTIVE);
282 } else if (start_position.x > scroll_x &&
283 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
284 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
285 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
287 set_state(STATE_ACTIVE);
289 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
290 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
291 ((start_position.y > scroll_y &&
292 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
293 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
294 start_position.y < scroll_y))) {
295 dir = start_position.x < scroll_x ? RIGHT : LEFT;
296 set_state(STATE_ACTIVE);
298 } else if(state == STATE_INIT
299 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
300 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
301 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
302 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
304 set_state(STATE_ACTIVE);