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.h"
25 static const float SQUISH_TIME = 2;
26 static const float X_OFFSCREEN_DISTANCE = 1600;
27 static const float Y_OFFSCREEN_DISTANCE = 1200;
30 : sprite(0), dir(LEFT), state(STATE_INIT)
40 BadGuy::draw(DrawingContext& context)
44 if(state == STATE_INIT || state == STATE_INACTIVE)
46 if(state == STATE_FALLING) {
47 uint32_t old_effect = context.get_drawing_effect();
48 context.set_drawing_effect(old_effect | VERTICAL_FLIP);
49 sprite->draw(context, get_pos(), LAYER_OBJECTS);
50 context.set_drawing_effect(old_effect);
52 sprite->draw(context, get_pos(), LAYER_OBJECTS);
57 BadGuy::action(float elapsed_time)
59 if(!Sector::current()->inside(bbox)) {
64 set_state(STATE_INACTIVE);
69 active_action(elapsed_time);
73 inactive_action(elapsed_time);
77 if(state_timer.check()) {
81 movement = physic.get_movement(elapsed_time);
84 movement = physic.get_movement(elapsed_time);
100 BadGuy::active_action(float elapsed_time)
102 movement = physic.get_movement(elapsed_time);
106 BadGuy::inactive_action(float )
111 BadGuy::collision(GameObject& other, const CollisionHit& hit)
118 if(other.get_flags() & FLAG_SOLID)
119 return collision_solid(other, hit);
121 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
122 if(badguy && badguy->state == STATE_ACTIVE)
123 return collision_badguy(*badguy, hit);
125 Player* player = dynamic_cast<Player*> (&other);
127 return collision_player(*player, hit);
132 if(other.get_flags() & FLAG_SOLID)
143 BadGuy::collision_solid(GameObject& , const CollisionHit& )
149 BadGuy::collision_player(Player& player, const CollisionHit& )
151 if(player.is_invincible()) {
156 if(player.get_movement().y > 0 && player.get_bbox().p2.y <
157 (get_bbox().p1.y + get_bbox().p2.y) / 2) {
158 // if it's not is it possible to squish us, then this will hurt
159 if(!collision_squished(player))
160 player.kill(Player::SHRINK);
164 player.kill(Player::SHRINK);
169 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
175 BadGuy::collision_squished(Player& )
181 BadGuy::kill_squished(Player& player)
183 sound_manager->play_sound("squish", get_pos(), player.get_pos());
184 physic.enable_gravity(true);
185 physic.set_velocity_x(0);
186 physic.set_velocity_y(0);
187 set_state(STATE_SQUISHED);
188 player.bounce(*this);
194 sound_manager->play_sound("fall", this,
195 Sector::current()->player->get_pos());
196 physic.set_velocity_y(0);
197 physic.enable_gravity(true);
198 set_state(STATE_FALLING);
202 BadGuy::set_state(State state)
204 if(this->state == state)
207 State laststate = this->state;
211 state_timer.start(SQUISH_TIME);
214 flags &= ~FLAG_NO_COLLDET;
215 bbox.set_pos(start_position);
218 // was the badguy dead anyway?
219 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
222 flags |= FLAG_NO_COLLDET;
225 flags |= FLAG_NO_COLLDET;
233 BadGuy::is_offscreen()
235 float scroll_x = Sector::current()->camera->get_translation().x;
236 float scroll_y = Sector::current()->camera->get_translation().y;
238 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
239 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
240 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
241 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
248 BadGuy::try_activate()
250 float scroll_x = Sector::current()->camera->get_translation().x;
251 float scroll_y = Sector::current()->camera->get_translation().y;
253 /* Activate badguys if they're just around the screen to avoid
254 * the effect of having badguys suddenly popping up from nowhere.
256 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
257 start_position.x < scroll_x - bbox.get_width() &&
258 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
259 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
261 set_state(STATE_ACTIVE);
263 } else if (start_position.x > scroll_x &&
264 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
265 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
266 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
268 set_state(STATE_ACTIVE);
270 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
271 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
272 ((start_position.y > scroll_y &&
273 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
274 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
275 start_position.y < scroll_y))) {
276 dir = start_position.x < scroll_x ? RIGHT : LEFT;
277 set_state(STATE_ACTIVE);
279 } else if(state == STATE_INIT
280 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
281 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
282 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
283 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
285 set_state(STATE_ACTIVE);