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
24 #include "object/camera.h"
26 static const float SQUISH_TIME = 2;
27 static const float X_OFFSCREEN_DISTANCE = 1600;
28 static const float Y_OFFSCREEN_DISTANCE = 1200;
31 : sprite(0), dir(LEFT), state(STATE_INIT)
33 //Set hitpoints and bullet hitpoints
44 BadGuy::draw(DrawingContext& context)
48 if(state == STATE_INIT || state == STATE_INACTIVE)
50 if(state == STATE_FALLING) {
51 sprite->draw(context, get_pos(), LAYER_OBJECTS, VERTICAL_FLIP);
53 sprite->draw(context, get_pos(), LAYER_OBJECTS);
58 BadGuy::action(float elapsed_time)
60 if(!Sector::current()->inside(bbox)) {
65 set_state(STATE_INACTIVE);
70 active_action(elapsed_time);
74 inactive_action(elapsed_time);
78 if(state_timer.check()) {
82 movement = physic.get_movement(elapsed_time);
85 movement = physic.get_movement(elapsed_time);
101 BadGuy::active_action(float elapsed_time)
103 movement = physic.get_movement(elapsed_time);
107 BadGuy::inactive_action(float )
112 BadGuy::collision(GameObject& other, const CollisionHit& hit)
119 if(other.get_flags() & FLAG_SOLID)
120 return collision_solid(other, hit);
122 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
123 if(badguy && badguy->state == STATE_ACTIVE)
124 return collision_badguy(*badguy, hit);
126 Player* player = dynamic_cast<Player*> (&other);
128 return collision_player(*player, hit);
133 if(other.get_flags() & FLAG_SOLID)
144 BadGuy::collision_solid(GameObject& , const CollisionHit& )
150 BadGuy::collision_player(Player& player, const CollisionHit& hit)
152 if(player.is_invincible()) {
156 if(hit.normal.y > .9) {
157 //TODO: fix inaccuracy (tux sometimes dies even if badguy was hit)
158 // give badguys some invincible time (prevent them from being hit multiple times)
161 if(collision_squished(player))
163 else if (hitpoints <= 0) {
164 bullet_hitpoints = 0;
165 player.kill(Player::SHRINK);
169 player.kill(Player::SHRINK);
174 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
180 BadGuy::collision_squished(Player& )
186 BadGuy::kill_squished(Player& player)
188 sound_manager->play_sound("squish", get_pos(), player.get_pos());
189 physic.enable_gravity(true);
190 physic.set_velocity_x(0);
191 physic.set_velocity_y(0);
192 set_state(STATE_SQUISHED);
193 player.bounce(*this);
200 if (bullet_hitpoints <= 0) {
202 sound_manager->play_sound("fall", this,
203 Sector::current()->player->get_pos());
204 physic.set_velocity_y(0);
205 physic.enable_gravity(true);
206 set_state(STATE_FALLING);
208 std::cout << "KILL_FALL - HITPOINTS: " << hitpoints << ", BULLET HP: " << bullet_hitpoints << std::endl;
212 BadGuy::set_state(State state)
214 if(this->state == state)
217 State laststate = this->state;
221 state_timer.start(SQUISH_TIME);
224 flags &= ~FLAG_NO_COLLDET;
225 bbox.set_pos(start_position);
228 // was the badguy dead anyway?
229 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
232 flags |= FLAG_NO_COLLDET;
235 flags |= FLAG_NO_COLLDET;
243 BadGuy::is_offscreen()
245 float scroll_x = Sector::current()->camera->get_translation().x;
246 float scroll_y = Sector::current()->camera->get_translation().y;
248 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
249 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
250 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
251 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
258 BadGuy::try_activate()
260 float scroll_x = Sector::current()->camera->get_translation().x;
261 float scroll_y = Sector::current()->camera->get_translation().y;
263 /* Activate badguys if they're just around the screen to avoid
264 * the effect of having badguys suddenly popping up from nowhere.
266 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
267 start_position.x < scroll_x - bbox.get_width() &&
268 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
269 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
271 set_state(STATE_ACTIVE);
273 } else if (start_position.x > scroll_x &&
274 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
275 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
276 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
278 set_state(STATE_ACTIVE);
280 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
281 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
282 ((start_position.y > scroll_y &&
283 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
284 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
285 start_position.y < scroll_y))) {
286 dir = start_position.x < scroll_x ? RIGHT : LEFT;
287 set_state(STATE_ACTIVE);
289 } else if(state == STATE_INIT
290 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
291 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
292 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
293 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
295 set_state(STATE_ACTIVE);