e010794bdcdf72ee215104560eed68f0bd4349d7
[supertux.git] / src / badguy / badguy.cpp
1 #include <config.h>
2
3 #include "badguy.h"
4 #include "object/camera.h"
5
6 static const float SQUISH_TIME = 2;
7 static const float X_OFFSCREEN_DISTANCE = 1600;
8 static const float Y_OFFSCREEN_DISTANCE = 1200;
9
10 BadGuy::BadGuy()
11   : sprite(0), dir(LEFT), state(STATE_INIT)
12 {
13   hitpoints = 1;
14 }
15
16 BadGuy::~BadGuy()
17 {
18   delete sprite;
19 }
20
21 void
22 BadGuy::draw(DrawingContext& context)
23 {
24   if(!sprite)
25     return;
26   if(state == STATE_INIT || state == STATE_INACTIVE)
27     return;
28   if(state == STATE_FALLING) {
29     sprite->draw(context, get_pos(), LAYER_OBJECTS, VERTICAL_FLIP);
30   } else {
31     sprite->draw(context, get_pos(), LAYER_OBJECTS);
32   }
33 }
34
35 void
36 BadGuy::action(float elapsed_time)
37 {
38   if(!Sector::current()->inside(bbox)) {
39     remove_me();
40     return;
41   }
42   if(is_offscreen()) {
43     set_state(STATE_INACTIVE);
44   }
45   
46   switch(state) {
47     case STATE_ACTIVE:
48       active_action(elapsed_time);
49       break;
50     case STATE_INIT:
51     case STATE_INACTIVE:
52       inactive_action(elapsed_time);
53       try_activate();
54       break;
55     case STATE_SQUISHED:
56       if(state_timer.check()) {
57         remove_me();
58         break;
59       }
60       movement = physic.get_movement(elapsed_time);
61       break;
62     case STATE_FALLING:
63       movement = physic.get_movement(elapsed_time);
64       break;
65   }
66 }
67
68 void
69 BadGuy::activate()
70 {
71 }
72
73 void
74 BadGuy::deactivate()
75 {
76 }
77
78 void
79 BadGuy::active_action(float elapsed_time)
80 {
81   movement = physic.get_movement(elapsed_time);
82 }
83
84 void
85 BadGuy::inactive_action(float )
86 {
87 }
88
89 HitResponse
90 BadGuy::collision(GameObject& other, const CollisionHit& hit)
91 {
92   switch(state) {
93     case STATE_INIT:
94     case STATE_INACTIVE:
95       return ABORT_MOVE;
96     case STATE_ACTIVE: {
97       if(other.get_flags() & FLAG_SOLID)
98         return collision_solid(other, hit);
99
100       BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
101       if(badguy && badguy->state == STATE_ACTIVE)
102         return collision_badguy(*badguy, hit);
103
104       Player* player = dynamic_cast<Player*> (&other);
105       if(player)
106         return collision_player(*player, hit);
107
108       return FORCE_MOVE;
109     }
110     case STATE_SQUISHED:
111       if(other.get_flags() & FLAG_SOLID)
112         return CONTINUE;
113       return FORCE_MOVE;
114     case STATE_FALLING:
115       return FORCE_MOVE;
116   }
117
118   return ABORT_MOVE;
119 }
120
121 HitResponse
122 BadGuy::collision_solid(GameObject& , const CollisionHit& )
123 {
124   return FORCE_MOVE;
125 }
126
127 HitResponse
128 BadGuy::collision_player(Player& player, const CollisionHit& hit)
129 {
130   if(player.is_invincible()) {
131     kill_fall();
132     return ABORT_MOVE;
133   }
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
138     hitpoints--;
139     std::cout << "Hitpoints: " << hitpoints << std::endl;
140     if(collision_squished(player))
141       return ABORT_MOVE;
142     else if (hitpoints <= 0) {
143       player.kill(Player::SHRINK);
144       return FORCE_MOVE;
145     }
146   }
147   player.kill(Player::SHRINK);
148   return FORCE_MOVE;
149 }
150
151 HitResponse
152 BadGuy::collision_badguy(BadGuy& , const CollisionHit& )
153 {
154   return FORCE_MOVE;
155 }
156
157 bool
158 BadGuy::collision_squished(Player& )
159 {
160   return false;
161 }
162
163 void
164 BadGuy::kill_squished(Player& player)
165 {
166   SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(),
167       player.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);
173 }
174
175 void
176 BadGuy::kill_fall()
177 {
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);
183 }
184
185 void
186 BadGuy::set_state(State state)
187 {
188   if(this->state == state)
189     return;
190
191   State laststate = this->state;
192   this->state = state;
193   switch(state) {
194     case STATE_SQUISHED:
195       state_timer.start(SQUISH_TIME);
196       break;
197     case STATE_ACTIVE:
198       flags &= ~FLAG_NO_COLLDET;
199       bbox.set_pos(start_position);
200       break;
201     case STATE_INACTIVE:
202       // was the badguy dead anyway?
203       if(laststate == STATE_SQUISHED || laststate == STATE_SQUISHED) {
204         remove_me();
205       }
206       flags |= FLAG_NO_COLLDET;
207       break;
208     case STATE_FALLING:
209       flags |= FLAG_NO_COLLDET;
210       break;
211     default:
212       break;
213   }
214 }
215
216 bool
217 BadGuy::is_offscreen()
218 {
219   float scroll_x = Sector::current()->camera->get_translation().x;
220   float scroll_y = Sector::current()->camera->get_translation().y;
221      
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)
226     return true;
227
228   return false;
229 }
230
231 void
232 BadGuy::try_activate()
233 {
234   float scroll_x = Sector::current()->camera->get_translation().x;
235   float scroll_y = Sector::current()->camera->get_translation().y;
236
237   /* Activate badguys if they're just around the screen to avoid
238    * the effect of having badguys suddenly popping up from nowhere.
239    */
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) {
244     dir = RIGHT;
245     set_state(STATE_ACTIVE);
246     activate();
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) {
251     dir = LEFT;
252     set_state(STATE_ACTIVE);
253     activate();
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);
262     activate();
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) {
268     dir = LEFT;
269     set_state(STATE_ACTIVE);
270     activate();
271   } 
272 }