merged new collision detection branch back into mainline
[supertux.git] / src / badguy / yeti.cpp
1 //  $Id$
2 // 
3 //  SuperTux - Boss "Yeti"
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
5 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 // 
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21 #include <config.h>
22
23 #include <float.h>
24 #include <sstream>
25 #include <memory>
26 #include "yeti.hpp"
27 #include "object/camera.hpp"
28 #include "yeti_stalactite.hpp"
29 #include "bouncing_snowball.hpp"
30 #include "game_session.hpp"
31 #include "level.hpp"
32
33 namespace {
34   const float JUMP_DOWN_VX = 250; /**< horizontal speed while jumping off the dais */
35   const float JUMP_DOWN_VY = -250; /**< vertical speed while jumping off the dais */
36
37   const float RUN_VX = 350; /**< horizontal speed while running */
38
39   const float JUMP_UP_VX = 350; /**< horizontal speed while jumping on the dais */
40   const float JUMP_UP_VY = -800; /**< vertical speed while jumping on the dais */
41
42   const float STOMP_VY = -250; /** vertical speed while stomping on the dais */
43
44   const float LEFT_STAND_X = 16; /**< x-coordinate of left dais' end position */
45   const float RIGHT_STAND_X = 800-60-16; /**< x-coordinate of right dais' end position */ 
46   const float LEFT_JUMP_X = LEFT_STAND_X+224; /**< x-coordinate of from where to jump on the left dais */
47   const float RIGHT_JUMP_X = RIGHT_STAND_X-224; /**< x-coordinate of from where to jump on the right dais */
48   const float STOMP_WAIT = .5; /**< time we stay on the dais before jumping again */
49   const float SAFE_TIME = .5; /**< the time we are safe when tux just hit us */
50   const int INITIAL_HITPOINTS = 3; /**< number of hits we can take */
51
52   const float SQUISH_TIME = 5;
53 }
54
55 Yeti::Yeti(const lisp::Lisp& reader)
56         : BadGuy(reader, "images/creatures/yeti/yeti.sprite")
57 {
58   hit_points = INITIAL_HITPOINTS;
59   reader.get("dead-script", dead_script);
60   countMe = false;
61   sound_manager->preload("sounds/yeti_gna.wav");
62   sound_manager->preload("sounds/yeti_roar.wav");
63 }
64
65 Yeti::~Yeti()
66 {
67 }
68
69 void
70 Yeti::activate()
71 {
72   dir = RIGHT;
73   jump_down();
74 }
75
76 void
77 Yeti::draw(DrawingContext& context)
78 {
79   // we blink when we are safe
80   if(safe_timer.started() && size_t(game_time*40)%2)
81     return;
82
83   BadGuy::draw(context);
84 }
85
86 void
87 Yeti::active_update(float elapsed_time)
88 {
89   switch(state) {
90     case JUMP_DOWN:
91       physic.set_velocity_x((dir==RIGHT)?+JUMP_DOWN_VX:-JUMP_DOWN_VX);
92       break;
93     case RUN:
94       physic.set_velocity_x((dir==RIGHT)?+RUN_VX:-RUN_VX);
95       if (((dir == RIGHT) && (get_pos().x >= RIGHT_JUMP_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_JUMP_X))) jump_up();
96       break;
97     case JUMP_UP:
98       physic.set_velocity_x((dir==RIGHT)?+JUMP_UP_VX:-JUMP_UP_VX);
99       if (((dir == RIGHT) && (get_pos().x >= RIGHT_STAND_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_STAND_X))) be_angry();
100       break;
101     case BE_ANGRY:
102       if(state_timer.check()) {
103         sound_manager->play("sounds/yeti_gna.wav");
104         physic.set_velocity_y(STOMP_VY);
105         sprite->set_action((dir==RIGHT)?"stomp-right":"stomp-left");
106       }
107       break;
108     case SQUISHED:
109       if (state_timer.check()) {
110         remove_me();
111       }
112       break;
113   }
114
115   movement = physic.get_movement(elapsed_time);
116 }
117
118 void
119 Yeti::jump_down()
120 {
121   sprite->set_action((dir==RIGHT)?"jump-right":"jump-left");
122   physic.set_velocity_x((dir==RIGHT)?(+JUMP_DOWN_VX):(-JUMP_DOWN_VX));
123   physic.set_velocity_y(JUMP_DOWN_VY);
124   state = JUMP_DOWN;
125 }
126
127 void
128 Yeti::run()
129 {
130   sprite->set_action((dir==RIGHT)?"run-right":"run-left");
131   physic.set_velocity_x((dir==RIGHT)?(+RUN_VX):(-RUN_VX));
132   physic.set_velocity_y(0);
133   state = RUN;
134 }
135
136 void
137 Yeti::jump_up()
138 {
139   sprite->set_action((dir==RIGHT)?"jump-right":"jump-left");
140   physic.set_velocity_x((dir==RIGHT)?(+JUMP_UP_VX):(-JUMP_UP_VX));
141   physic.set_velocity_y(JUMP_UP_VY);
142   state = JUMP_UP;
143 }
144
145 void
146 Yeti::be_angry()
147 {
148   //turn around
149   dir = (dir==RIGHT)?LEFT:RIGHT;
150
151   sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
152   physic.set_velocity_x(0);
153   physic.set_velocity_y(0);
154   if (hit_points < INITIAL_HITPOINTS) summon_snowball();
155   stomp_count = 0;
156   state = BE_ANGRY;
157   state_timer.start(STOMP_WAIT);
158 }
159
160 void
161 Yeti::summon_snowball()
162 {
163   Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x+(dir == RIGHT ? 64 : -64), get_pos().y), dir));
164 }
165
166 bool
167 Yeti::collision_squished(Player& player)
168 {
169   kill_squished(player);
170
171   return true;
172 }
173
174 void
175 Yeti::kill_squished(Player& player)
176 {
177     player.bounce(*this);
178     take_hit(player);
179 }
180
181 void Yeti::take_hit(Player& )
182 {
183   if(safe_timer.started())
184     return;
185
186   sound_manager->play("sounds/yeti_roar.wav");
187   hit_points--;
188
189   if(hit_points <= 0) {
190     // We're dead
191     physic.enable_gravity(true);
192     physic.set_velocity_x(0);
193     physic.set_velocity_y(0);
194     
195     state = SQUISHED;
196     state_timer.start(SQUISH_TIME);
197     set_group(COLGROUP_MOVING_ONLY_STATIC);
198     sprite->set_action("dead");
199
200     if (countMe) Sector::current()->get_level()->stats.badguys++;
201
202     // start script
203     if(dead_script != "") {
204       std::istringstream stream(dead_script);
205       Sector::current()->run_script(stream, "Yeti - dead-script");
206     }
207   }
208   else {
209     safe_timer.start(SAFE_TIME);
210   }
211 }
212
213 void
214 Yeti::kill_fall()
215 {
216   // shooting bullets or being invincible won't work :)
217   take_hit(*get_nearest_player()); // FIXME: debug only(?)
218 }
219
220 void
221 Yeti::write(lisp::Writer& writer)
222 {
223   writer.start_list("yeti");
224
225   writer.write_float("x", start_position.x);
226   writer.write_float("y", start_position.y);
227
228   if(dead_script != "") {
229     writer.write_string("dead-script", dead_script);
230   }
231
232   writer.end_list("yeti");
233 }
234
235 void
236 Yeti::drop_stalactite()
237 {
238   // make a stalactite falling down and shake camera a bit
239   Sector::current()->camera->shake(.1, 0, 10);
240
241   YetiStalactite* nearest = 0;
242   float dist = FLT_MAX;
243
244   Player* player = this->get_nearest_player();
245   if (!player) return;
246
247   Sector* sector = Sector::current();
248   for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
249       i != sector->gameobjects.end(); ++i) {
250     YetiStalactite* stalactite = dynamic_cast<YetiStalactite*> (*i);
251     if(stalactite && stalactite->is_hanging()) {
252       float sdist 
253         = fabsf(stalactite->get_pos().x - player->get_pos().x);
254       if(sdist < dist) {
255         nearest = stalactite;
256         dist = sdist;
257       }
258     }
259   }
260
261   if(nearest)
262     nearest->start_shaking();
263 }
264
265 void
266 Yeti::collision_solid(const CollisionHit& hit)
267 {
268   if(hit.top || hit.bottom) { 
269     // hit floor or roof
270     physic.set_velocity_y(0);
271     switch (state) {
272       case JUMP_DOWN:
273         run();
274         break;
275       case RUN:
276         break;
277       case JUMP_UP:
278         break;
279       case BE_ANGRY:
280         // we just landed
281         if(!state_timer.started()) {
282           sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
283           stomp_count++;
284           drop_stalactite();
285
286           // go to other side after 3 jumps
287           if(stomp_count == 3) {
288             jump_down();
289           } else {
290             // jump again
291             state_timer.start(STOMP_WAIT);
292           }
293         }
294         break;
295       case SQUISHED:
296         break;
297     }
298   } else if(hit.left || hit.right) {
299     // hit wall
300     jump_up();
301   }
302 }
303
304 IMPLEMENT_FACTORY(Yeti, "yeti")