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"
31 static const float SQUISH_TIME = 2;
32 static const float X_OFFSCREEN_DISTANCE = 1600;
33 static const float Y_OFFSCREEN_DISTANCE = 1200;
36 : countMe(true), sprite(0), dir(LEFT), state(STATE_INIT)
38 set_group(COLGROUP_DISABLED);
47 BadGuy::draw(DrawingContext& context)
51 if(state == STATE_INIT || state == STATE_INACTIVE)
53 if(state == STATE_FALLING) {
54 DrawingEffect old_effect = context.get_drawing_effect();
55 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
56 sprite->draw(context, get_pos(), LAYER_OBJECTS);
57 context.set_drawing_effect(old_effect);
59 sprite->draw(context, get_pos(), LAYER_OBJECTS);
64 BadGuy::update(float elapsed_time)
66 if(!Sector::current()->inside(bbox)) {
71 set_state(STATE_INACTIVE);
76 active_update(elapsed_time);
80 inactive_update(elapsed_time);
84 if(state_timer.check()) {
88 movement = physic.get_movement(elapsed_time);
91 movement = physic.get_movement(elapsed_time);
107 BadGuy::save(lisp::Writer& )
109 log_warning << "tried to write out a generic badguy" << std::endl;
113 BadGuy::active_update(float elapsed_time)
115 movement = physic.get_movement(elapsed_time);
119 BadGuy::inactive_update(float )
124 BadGuy::collision_tile(uint32_t tile_attributes)
126 if(tile_attributes & Tile::HURTS)
131 BadGuy::collision(GameObject& other, const CollisionHit& hit)
138 if(other.get_flags() & FLAG_SOLID)
139 return collision_solid(other, hit);
141 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
142 if(badguy && badguy->state == STATE_ACTIVE)
143 return collision_badguy(*badguy, hit);
145 Player* player = dynamic_cast<Player*> (&other);
147 return collision_player(*player, hit);
152 if(other.get_flags() & FLAG_SOLID)
163 BadGuy::collision_solid(GameObject& , const CollisionHit& )
169 BadGuy::collision_player(Player& player, const CollisionHit& )
171 if(player.is_invincible()) {
177 printf("PlayerHit: GT %3.1f PM: %3.1f %3.1f BM: %3.1f %3.1f Hit: %3.1f %3.1f\n",
179 player.get_movement().x, player.get_movement().y,
180 get_movement().x, get_movement().y,
181 hit.normal.x, hit.normal.y);
185 if(player.get_movement().y /*- get_movement().y*/ > 0
186 && player.get_bbox().p2.y <
187 (get_bbox().p1.y + get_bbox().p2.y) / 2) {
188 // if it's not possible to squish us, then this will hurt
189 if(collision_squished(player))
193 player.kill(Player::SHRINK);
198 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
204 BadGuy::collision_squished(Player& )
210 BadGuy::kill_squished(Player& player)
212 sound_manager->play("sounds/squish.wav", get_pos());
213 physic.enable_gravity(true);
214 physic.set_velocity_x(0);
215 physic.set_velocity_y(0);
216 set_state(STATE_SQUISHED);
217 set_group(COLGROUP_MOVING_ONLY_STATIC);
218 if (countMe) Sector::current()->get_level()->stats.badguys++;
219 player.bounce(*this);
225 sound_manager->play("sounds/fall.wav", get_pos());
226 if (countMe) Sector::current()->get_level()->stats.badguys++;
227 physic.set_velocity_y(0);
228 physic.enable_gravity(true);
229 set_state(STATE_FALLING);
233 BadGuy::set_state(State state)
235 if(this->state == state)
238 State laststate = this->state;
242 state_timer.start(SQUISH_TIME);
245 set_group(COLGROUP_MOVING);
246 bbox.set_pos(start_position);
249 // was the badguy dead anyway?
250 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
253 set_group(COLGROUP_DISABLED);
256 set_group(COLGROUP_DISABLED);
264 BadGuy::is_offscreen()
266 float scroll_x = Sector::current()->camera->get_translation().x;
267 float scroll_y = Sector::current()->camera->get_translation().y;
269 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
270 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
271 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
272 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
279 BadGuy::try_activate()
281 float scroll_x = Sector::current()->camera->get_translation().x;
282 float scroll_y = Sector::current()->camera->get_translation().y;
284 /* Activate badguys if they're just around the screen to avoid
285 * the effect of having badguys suddenly popping up from nowhere.
287 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
288 start_position.x < scroll_x - bbox.get_width() &&
289 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
290 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
292 set_state(STATE_ACTIVE);
294 } else if (start_position.x > scroll_x &&
295 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
296 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
297 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
299 set_state(STATE_ACTIVE);
301 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
302 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
303 ((start_position.y > scroll_y &&
304 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
305 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
306 start_position.y < scroll_y))) {
307 dir = start_position.x < scroll_x ? RIGHT : LEFT;
308 set_state(STATE_ACTIVE);
310 } else if(state == STATE_INIT
311 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
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);
322 BadGuy::may_fall_off_platform()
325 // First, let's say the badguy moves 32 units in the
326 // direction it's heading, so do some voodoo maths magic
327 // to determine its future position.
330 pos = Vector(bbox.p1.x - 32.f, bbox.p2.y);
332 pos = Vector(bbox.p2.x, bbox.p2.y);
334 // Now, snap the badguy's X coordinate to the 32x32/cell grid.
335 if (dir == LEFT) // use the ceiling
336 tile_x = (int)ceilf(pos.x/32.0f);
337 else // use the floor
338 tile_x = (int)floorf(pos.x/32.0f);
340 // We might be falling down, so use the ceiling to round upward and
341 // get the lower position. (Positive Y goes downward.)
342 tile_y = (int)ceilf(pos.y/32.0f);
344 #if defined(DEBUG_STAY_ON_PLATFORM)
346 GameSession::current()->context->draw_filled_rect(Vector(tile_x*32.0f, tile_y*32.0f), Vector(32.f, 32.f), Color(1.f, 0.f, 0.f), 999);
349 // Now, if the badguy intersects with a tile, he won't fall off.
350 // If he doesn't intersect, he probably will.
351 // Note that the tile's Y coordinate is offset by +1 from the object's Y.
352 if (Sector::current()->solids->get_tile(tile_x, tile_y)->getAttributes() & FLAG_SOLID)
354 // It's a solid tile. Good.
358 // Watch out there buddy, you might take a sticky end!
363 BadGuy::get_nearest_player()
365 // FIXME: does not really return nearest player
367 std::vector<Player*> players = Sector::current()->get_players();
368 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
369 Player* player = *playerIter;