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 static const float SQUISH_TIME = 2;
31 static const float X_OFFSCREEN_DISTANCE = 1600;
32 static const float Y_OFFSCREEN_DISTANCE = 1200;
35 : countMe(true), sprite(0), dir(LEFT), state(STATE_INIT)
37 set_group(COLGROUP_DISABLED);
46 BadGuy::draw(DrawingContext& context)
50 if(state == STATE_INIT || state == STATE_INACTIVE)
52 if(state == STATE_FALLING) {
53 DrawingEffect old_effect = context.get_drawing_effect();
54 context.set_drawing_effect((DrawingEffect) (old_effect | VERTICAL_FLIP));
55 sprite->draw(context, get_pos(), LAYER_OBJECTS);
56 context.set_drawing_effect(old_effect);
58 sprite->draw(context, get_pos(), LAYER_OBJECTS);
63 BadGuy::update(float elapsed_time)
65 if(!Sector::current()->inside(bbox)) {
70 set_state(STATE_INACTIVE);
75 active_update(elapsed_time);
79 inactive_update(elapsed_time);
83 if(state_timer.check()) {
87 movement = physic.get_movement(elapsed_time);
90 movement = physic.get_movement(elapsed_time);
106 BadGuy::save(lisp::Writer& )
108 log_warning << "tried to write out a generic badguy" << std::endl;
112 BadGuy::active_update(float elapsed_time)
114 movement = physic.get_movement(elapsed_time);
118 BadGuy::inactive_update(float )
123 BadGuy::collision_tile(uint32_t tile_attributes)
125 if(tile_attributes & Tile::HURTS)
130 BadGuy::collision(GameObject& other, const CollisionHit& hit)
137 if(other.get_flags() & FLAG_SOLID)
138 return collision_solid(other, hit);
140 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
141 if(badguy && badguy->state == STATE_ACTIVE)
142 return collision_badguy(*badguy, hit);
144 Player* player = dynamic_cast<Player*> (&other);
146 return collision_player(*player, hit);
151 if(other.get_flags() & FLAG_SOLID)
162 BadGuy::collision_solid(GameObject& , const CollisionHit& )
168 BadGuy::collision_player(Player& player, const CollisionHit& )
170 if(player.is_invincible()) {
176 printf("PlayerHit: GT %3.1f PM: %3.1f %3.1f BM: %3.1f %3.1f Hit: %3.1f %3.1f\n",
178 player.get_movement().x, player.get_movement().y,
179 get_movement().x, get_movement().y,
180 hit.normal.x, hit.normal.y);
184 if(player.get_movement().y /*- get_movement().y*/ > 0
185 && player.get_bbox().p2.y <
186 (get_bbox().p1.y + get_bbox().p2.y) / 2) {
187 // if it's not possible to squish us, then this will hurt
188 if(collision_squished(player))
192 player.kill(Player::SHRINK);
197 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
203 BadGuy::collision_squished(Player& )
209 BadGuy::kill_squished(Player& player)
211 sound_manager->play("sounds/squish.wav", get_pos());
212 physic.enable_gravity(true);
213 physic.set_velocity_x(0);
214 physic.set_velocity_y(0);
215 set_state(STATE_SQUISHED);
216 set_group(COLGROUP_MOVING_ONLY_STATIC);
217 Sector::current()->get_level()->stats.add_points(BADGUYS_KILLED_STAT, 1);
218 player.bounce(*this);
224 sound_manager->play("sounds/fall.wav", get_pos());
225 Sector::current()->get_level()->stats.add_points(BADGUYS_KILLED_STAT, 1);
226 physic.set_velocity_y(0);
227 physic.enable_gravity(true);
228 set_state(STATE_FALLING);
232 BadGuy::set_state(State state)
234 if(this->state == state)
237 State laststate = this->state;
241 state_timer.start(SQUISH_TIME);
244 set_group(COLGROUP_MOVING);
245 bbox.set_pos(start_position);
248 // was the badguy dead anyway?
249 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
252 set_group(COLGROUP_DISABLED);
255 set_group(COLGROUP_DISABLED);
263 BadGuy::is_offscreen()
265 float scroll_x = Sector::current()->camera->get_translation().x;
266 float scroll_y = Sector::current()->camera->get_translation().y;
268 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
269 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
270 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
271 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
278 BadGuy::try_activate()
280 float scroll_x = Sector::current()->camera->get_translation().x;
281 float scroll_y = Sector::current()->camera->get_translation().y;
283 /* Activate badguys if they're just around the screen to avoid
284 * the effect of having badguys suddenly popping up from nowhere.
286 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
287 start_position.x < scroll_x - bbox.get_width() &&
288 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
289 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
291 set_state(STATE_ACTIVE);
293 } else if (start_position.x > scroll_x &&
294 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
295 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
296 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
298 set_state(STATE_ACTIVE);
300 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
301 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
302 ((start_position.y > scroll_y &&
303 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
304 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
305 start_position.y < scroll_y))) {
306 dir = start_position.x < scroll_x ? RIGHT : LEFT;
307 set_state(STATE_ACTIVE);
309 } else if(state == STATE_INIT
310 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
311 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
312 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
313 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
315 set_state(STATE_ACTIVE);
321 BadGuy::may_fall_off_platform()
324 // First, let's say the badguy moves 32 units in the
325 // direction it's heading, so do some voodoo maths magic
326 // to determine its future position.
329 pos = Vector(bbox.p1.x - 32.f, bbox.p2.y);
331 pos = Vector(bbox.p2.x, bbox.p2.y);
333 // Now, snap the badguy's X coordinate to the 32x32/cell grid.
334 if (dir == LEFT) // use the ceiling
335 tile_x = (int)ceilf(pos.x/32.0f);
336 else // use the floor
337 tile_x = (int)floorf(pos.x/32.0f);
339 // We might be falling down, so use the ceiling to round upward and
340 // get the lower position. (Positive Y goes downward.)
341 tile_y = (int)ceilf(pos.y/32.0f);
343 #if defined(DEBUG_STAY_ON_PLATFORM)
345 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);
348 // Now, if the badguy intersects with a tile, he won't fall off.
349 // If he doesn't intersect, he probably will.
350 // Note that the tile's Y coordinate is offset by +1 from the object's Y.
351 if (Sector::current()->solids->get_tile(tile_x, tile_y)->getAttributes() & FLAG_SOLID)
353 // It's a solid tile. Good.
357 // Watch out there buddy, you might take a sticky end!
362 BadGuy::get_nearest_player()
364 // FIXME: does not really return nearest player
366 std::vector<Player*> players = Sector::current()->get_players();
367 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
368 Player* player = *playerIter;