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