4 #include "object/camera.h"
6 static const float SQUISH_TIME = 2;
7 static const float X_OFFSCREEN_DISTANCE = 1600;
8 static const float Y_OFFSCREEN_DISTANCE = 1200;
11 : sprite(0), dir(LEFT), state(STATE_INIT)
22 BadGuy::draw(DrawingContext& context)
26 if(state == STATE_INIT || state == STATE_INACTIVE)
28 if(state == STATE_FALLING) {
29 sprite->draw(context, get_pos(), LAYER_OBJECTS, VERTICAL_FLIP);
31 sprite->draw(context, get_pos(), LAYER_OBJECTS);
36 BadGuy::action(float elapsed_time)
38 if(!Sector::current()->inside(bbox)) {
43 set_state(STATE_INACTIVE);
48 active_action(elapsed_time);
52 inactive_action(elapsed_time);
56 if(state_timer.check()) {
60 movement = physic.get_movement(elapsed_time);
63 movement = physic.get_movement(elapsed_time);
79 BadGuy::active_action(float elapsed_time)
81 movement = physic.get_movement(elapsed_time);
85 BadGuy::inactive_action(float )
90 BadGuy::collision(GameObject& other, const CollisionHit& hit)
97 if(other.get_flags() & FLAG_SOLID)
98 return collision_solid(other, hit);
100 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
101 if(badguy && badguy->state == STATE_ACTIVE)
102 return collision_badguy(*badguy, hit);
104 Player* player = dynamic_cast<Player*> (&other);
106 return collision_player(*player, hit);
111 if(other.get_flags() & FLAG_SOLID)
122 BadGuy::collision_solid(GameObject& , const CollisionHit& )
128 BadGuy::collision_player(Player& player, const CollisionHit& hit)
130 if(player.is_invincible()) {
134 if(hit.normal.y > .9) {
135 //TODO: fix inaccuracy (tux sometimes dies even if badguy was hit)
136 // give badguys some invincible time (prevent them from being hit multiple times)
137 // use hitpoints also when hit by fireball or invincible tux
139 std::cout << "Hitpoints: " << hitpoints << std::endl;
140 if(collision_squished(player))
142 else if (hitpoints <= 0) {
143 player.kill(Player::SHRINK);
147 player.kill(Player::SHRINK);
152 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
158 BadGuy::collision_squished(Player& )
164 BadGuy::kill_squished(Player& player)
166 SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(),
168 physic.enable_gravity(true);
169 physic.set_velocity_x(0);
170 physic.set_velocity_y(0);
171 set_state(STATE_SQUISHED);
172 player.bounce(*this);
178 SoundManager::get()->play_sound(IDToSound(SND_FALL), this,
179 Sector::current()->player->get_pos());
180 physic.set_velocity_y(0);
181 physic.enable_gravity(true);
182 set_state(STATE_FALLING);
186 BadGuy::set_state(State state)
188 if(this->state == state)
191 State laststate = this->state;
195 state_timer.start(SQUISH_TIME);
198 flags &= ~FLAG_NO_COLLDET;
199 bbox.set_pos(start_position);
202 // was the badguy dead anyway?
203 if(laststate == STATE_SQUISHED || laststate == STATE_FALLING) {
206 flags |= FLAG_NO_COLLDET;
209 flags |= FLAG_NO_COLLDET;
217 BadGuy::is_offscreen()
219 float scroll_x = Sector::current()->camera->get_translation().x;
220 float scroll_y = Sector::current()->camera->get_translation().y;
222 if(bbox.p2.x < scroll_x - X_OFFSCREEN_DISTANCE
223 || bbox.p1.x > scroll_x + X_OFFSCREEN_DISTANCE
224 || bbox.p2.y < scroll_y - Y_OFFSCREEN_DISTANCE
225 || bbox.p1.y > scroll_y + Y_OFFSCREEN_DISTANCE)
232 BadGuy::try_activate()
234 float scroll_x = Sector::current()->camera->get_translation().x;
235 float scroll_y = Sector::current()->camera->get_translation().y;
237 /* Activate badguys if they're just around the screen to avoid
238 * the effect of having badguys suddenly popping up from nowhere.
240 if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
241 start_position.x < scroll_x - bbox.get_width() &&
242 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
243 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
245 set_state(STATE_ACTIVE);
247 } else if (start_position.x > scroll_x &&
248 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
249 start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
250 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
252 set_state(STATE_ACTIVE);
254 } else if (start_position.x > scroll_x - X_OFFSCREEN_DISTANCE &&
255 start_position.x < scroll_x + X_OFFSCREEN_DISTANCE &&
256 ((start_position.y > scroll_y &&
257 start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) ||
258 (start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
259 start_position.y < scroll_y))) {
260 dir = start_position.x < scroll_x ? RIGHT : LEFT;
261 set_state(STATE_ACTIVE);
263 } else if(state == STATE_INIT
264 && start_position.x > scroll_x - X_OFFSCREEN_DISTANCE
265 && start_position.x < scroll_x + X_OFFSCREEN_DISTANCE
266 && start_position.y > scroll_y - Y_OFFSCREEN_DISTANCE
267 && start_position.y < scroll_y + Y_OFFSCREEN_DISTANCE) {
269 set_state(STATE_ACTIVE);