Badguys now inherit from MovingSprite
[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 }
62
63 Yeti::~Yeti()
64 {
65 }
66
67 void
68 Yeti::activate()
69 {
70   dir = RIGHT;
71   jump_down();
72 }
73
74 void
75 Yeti::draw(DrawingContext& context)
76 {
77   // we blink when we are safe
78   if(safe_timer.started() && size_t(game_time*40)%2)
79     return;
80
81   BadGuy::draw(context);
82 }
83
84 void
85 Yeti::active_update(float elapsed_time)
86 {
87   switch(state) {
88     case JUMP_DOWN:
89       physic.set_velocity_x((dir==RIGHT)?+JUMP_DOWN_VX:-JUMP_DOWN_VX);
90       break;
91     case RUN:
92       physic.set_velocity_x((dir==RIGHT)?+RUN_VX:-RUN_VX);
93       if (((dir == RIGHT) && (get_pos().x >= RIGHT_JUMP_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_JUMP_X))) jump_up();
94       break;
95     case JUMP_UP:
96       physic.set_velocity_x((dir==RIGHT)?+JUMP_UP_VX:-JUMP_UP_VX);
97       if (((dir == RIGHT) && (get_pos().x >= RIGHT_STAND_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_STAND_X))) be_angry();
98       break;
99     case BE_ANGRY:
100       if(state_timer.check()) {
101         sound_manager->play("sounds/yeti_gna.wav");
102         physic.set_velocity_y(STOMP_VY);
103         sprite->set_action((dir==RIGHT)?"stomp-right":"stomp-left");
104       }
105       break;
106     case SQUISHED:
107       if (state_timer.check()) {
108         remove_me();
109       }
110       break;
111   }
112
113   movement = physic.get_movement(elapsed_time);
114 }
115
116 void
117 Yeti::jump_down()
118 {
119   sprite->set_action((dir==RIGHT)?"jump-right":"jump-left");
120   physic.set_velocity_x((dir==RIGHT)?(+JUMP_DOWN_VX):(-JUMP_DOWN_VX));
121   physic.set_velocity_y(JUMP_DOWN_VY);
122   state = JUMP_DOWN;
123 }
124
125 void
126 Yeti::run()
127 {
128   sprite->set_action((dir==RIGHT)?"run-right":"run-left");
129   physic.set_velocity_x((dir==RIGHT)?(+RUN_VX):(-RUN_VX));
130   physic.set_velocity_y(0);
131   state = RUN;
132 }
133
134 void
135 Yeti::jump_up()
136 {
137   sprite->set_action((dir==RIGHT)?"jump-right":"jump-left");
138   physic.set_velocity_x((dir==RIGHT)?(+JUMP_UP_VX):(-JUMP_UP_VX));
139   physic.set_velocity_y(JUMP_UP_VY);
140   state = JUMP_UP;
141 }
142
143 void
144 Yeti::be_angry()
145 {
146   //turn around
147   dir = (dir==RIGHT)?LEFT:RIGHT;
148
149   sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
150   physic.set_velocity_x(0);
151   physic.set_velocity_y(0);
152   if (hit_points < INITIAL_HITPOINTS) summon_snowball();
153   stomp_count = 0;
154   state = BE_ANGRY;
155   state_timer.start(STOMP_WAIT);
156 }
157
158 void
159 Yeti::summon_snowball()
160 {
161   Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x+(dir == RIGHT ? 64 : -64), get_pos().y), dir));
162 }
163
164 bool
165 Yeti::collision_squished(Player& player)
166 {
167   kill_squished(player);
168
169   return true;
170 }
171
172 void
173 Yeti::kill_squished(Player& player)
174 {
175     player.bounce(*this);
176     take_hit(player);
177 }
178
179 void Yeti::take_hit(Player& )
180 {
181   if(safe_timer.started())
182     return;
183
184   sound_manager->play("sounds/yeti_roar.wav");
185   hit_points--;
186
187   if(hit_points <= 0) {
188     // We're dead
189     physic.enable_gravity(true);
190     physic.set_velocity_x(0);
191     physic.set_velocity_y(0);
192     
193     state = SQUISHED;
194     state_timer.start(SQUISH_TIME);
195     set_group(COLGROUP_MOVING_ONLY_STATIC);
196     sprite->set_action("dead");
197
198     if (countMe) Sector::current()->get_level()->stats.badguys++;
199
200     // start script
201     if(dead_script != "") {
202       std::istringstream stream(dead_script);
203       Sector::current()->run_script(stream, "Yeti - dead-script");
204     }
205   }
206   else {
207     safe_timer.start(SAFE_TIME);
208   }
209 }
210
211 void
212 Yeti::kill_fall()
213 {
214   // shooting bullets or being invincible won't work :)
215   take_hit(*get_nearest_player()); // FIXME: debug only(?)
216 }
217
218 void
219 Yeti::write(lisp::Writer& writer)
220 {
221   writer.start_list("yeti");
222
223   writer.write_float("x", start_position.x);
224   writer.write_float("y", start_position.y);
225
226   if(dead_script != "") {
227     writer.write_string("dead-script", dead_script);
228   }
229
230   writer.end_list("yeti");
231 }
232
233 void
234 Yeti::drop_stalactite()
235 {
236   // make a stalactite falling down and shake camera a bit
237   Sector::current()->camera->shake(.1, 0, 10);
238
239   YetiStalactite* nearest = 0;
240   float dist = FLT_MAX;
241
242   Player* player = this->get_nearest_player();
243   if (!player) return;
244
245   Sector* sector = Sector::current();
246   for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
247       i != sector->gameobjects.end(); ++i) {
248     YetiStalactite* stalactite = dynamic_cast<YetiStalactite*> (*i);
249     if(stalactite && stalactite->is_hanging()) {
250       float sdist 
251         = fabsf(stalactite->get_pos().x - player->get_pos().x);
252       if(sdist < dist) {
253         nearest = stalactite;
254         dist = sdist;
255       }
256     }
257   }
258
259   if(nearest)
260     nearest->start_shaking();
261 }
262
263 HitResponse
264 Yeti::collision_solid(GameObject& , const CollisionHit& hit)
265 {
266   if(fabsf(hit.normal.y) > .5) { 
267     // hit floor or roof
268     physic.set_velocity_y(0);
269     switch (state) {
270       case JUMP_DOWN:
271         run();
272         break;
273       case RUN:
274         break;
275       case JUMP_UP:
276         break;
277       case BE_ANGRY:
278         // we just landed
279         if(!state_timer.started()) {
280           sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
281           stomp_count++;
282           drop_stalactite();
283
284           // go to other side after 3 jumps
285           if(stomp_count == 3) {
286             jump_down();
287           } else {
288             // jump again
289             state_timer.start(STOMP_WAIT);
290           }
291         }
292         break;
293       case SQUISHED:
294         break;
295     }
296   } else 
297   if(fabsf(hit.normal.x) > .5) {
298     // hit wall
299     jump_up();
300   }
301
302   return CONTINUE;
303 }
304
305 IMPLEMENT_FACTORY(Yeti, "yeti")