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>
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.
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.
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
27 #include "object/camera.hpp"
28 #include "yeti_stalactite.hpp"
29 #include "bouncing_snowball.hpp"
30 #include "game_session.hpp"
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 */
37 const float RUN_VX = 350; /**< horizontal speed while running */
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 */
42 const float STOMP_VY = -250; /** vertical speed while stomping on the dais */
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 */
52 const float SQUISH_TIME = 5;
55 Yeti::Yeti(const lisp::Lisp& reader)
56 : BadGuy(reader, "images/creatures/yeti/yeti.sprite")
58 hit_points = INITIAL_HITPOINTS;
60 sound_manager->preload("sounds/yeti_gna.wav");
61 sound_manager->preload("sounds/yeti_roar.wav");
62 hud_head.reset(new Surface("images/creatures/yeti/hudlife.png"));
77 Yeti::draw(DrawingContext& context)
79 // we blink when we are safe
80 if(safe_timer.started() && size_t(game_time*40)%2)
83 draw_hit_points(context);
85 BadGuy::draw(context);
89 Yeti::draw_hit_points(DrawingContext& context)
93 Surface *hh = hud_head.get();
97 context.push_transform();
98 context.set_translation(Vector(0, 0));
100 for (i = 0; i < hit_points; ++i)
102 context.draw_surface(hh, Vector(BORDER_X + (i * hh->get_width()), BORDER_Y + 1), LAYER_FOREGROUND1);
105 context.pop_transform();
109 Yeti::active_update(float elapsed_time)
113 physic.set_velocity_x((dir==RIGHT)?+JUMP_DOWN_VX:-JUMP_DOWN_VX);
116 physic.set_velocity_x((dir==RIGHT)?+RUN_VX:-RUN_VX);
117 if (((dir == RIGHT) && (get_pos().x >= RIGHT_JUMP_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_JUMP_X))) jump_up();
120 physic.set_velocity_x((dir==RIGHT)?+JUMP_UP_VX:-JUMP_UP_VX);
121 if (((dir == RIGHT) && (get_pos().x >= RIGHT_STAND_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_STAND_X))) be_angry();
124 if(state_timer.check()) {
125 sound_manager->play("sounds/yeti_gna.wav");
126 physic.set_velocity_y(STOMP_VY);
127 sprite->set_action((dir==RIGHT)?"stomp-right":"stomp-left");
131 if (state_timer.check()) {
137 movement = physic.get_movement(elapsed_time);
143 sprite->set_action((dir==RIGHT)?"jump-right":"jump-left");
144 physic.set_velocity_x((dir==RIGHT)?(+JUMP_DOWN_VX):(-JUMP_DOWN_VX));
145 physic.set_velocity_y(JUMP_DOWN_VY);
152 sprite->set_action((dir==RIGHT)?"run-right":"run-left");
153 physic.set_velocity_x((dir==RIGHT)?(+RUN_VX):(-RUN_VX));
154 physic.set_velocity_y(0);
161 sprite->set_action((dir==RIGHT)?"jump-right":"jump-left");
162 physic.set_velocity_x((dir==RIGHT)?(+JUMP_UP_VX):(-JUMP_UP_VX));
163 physic.set_velocity_y(JUMP_UP_VY);
171 dir = (dir==RIGHT)?LEFT:RIGHT;
173 sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
174 physic.set_velocity_x(0);
175 physic.set_velocity_y(0);
176 if (hit_points < INITIAL_HITPOINTS) summon_snowball();
179 state_timer.start(STOMP_WAIT);
183 Yeti::summon_snowball()
185 Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x+(dir == RIGHT ? 64 : -64), get_pos().y), dir));
189 Yeti::collision_squished(GameObject& object)
191 kill_squished(object);
197 Yeti::kill_squished(GameObject& object)
199 Player* player = dynamic_cast<Player*>(&object);
201 player->bounce(*this);
206 void Yeti::take_hit(Player& )
208 if(safe_timer.started())
211 sound_manager->play("sounds/yeti_roar.wav");
214 if(hit_points <= 0) {
216 physic.enable_gravity(true);
217 physic.set_velocity_x(0);
218 physic.set_velocity_y(0);
221 state_timer.start(SQUISH_TIME);
222 set_group(COLGROUP_MOVING_ONLY_STATIC);
223 sprite->set_action("dead");
225 if (countMe) Sector::current()->get_level()->stats.badguys++;
227 if(dead_script != "") {
228 std::istringstream stream(dead_script);
229 Sector::current()->run_script(stream, "Yeti - dead-script");
233 safe_timer.start(SAFE_TIME);
240 // shooting bullets or being invincible won't work :)
241 take_hit(*get_nearest_player()); // FIXME: debug only(?)
245 Yeti::write(lisp::Writer& writer)
247 writer.start_list("yeti");
249 writer.write_float("x", start_position.x);
250 writer.write_float("y", start_position.y);
252 writer.end_list("yeti");
256 Yeti::drop_stalactite()
258 // make a stalactite falling down and shake camera a bit
259 Sector::current()->camera->shake(.1f, 0, 10);
261 YetiStalactite* nearest = 0;
262 float dist = FLT_MAX;
264 Player* player = this->get_nearest_player();
267 Sector* sector = Sector::current();
268 for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
269 i != sector->gameobjects.end(); ++i) {
270 YetiStalactite* stalactite = dynamic_cast<YetiStalactite*> (*i);
271 if(stalactite && stalactite->is_hanging()) {
273 = fabsf(stalactite->get_pos().x - player->get_pos().x);
275 nearest = stalactite;
282 nearest->start_shaking();
286 Yeti::collision_solid(const CollisionHit& hit)
288 if(hit.top || hit.bottom) {
290 physic.set_velocity_y(0);
301 if(!state_timer.started()) {
302 sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
306 // go to other side after 3 jumps
307 if(stomp_count == 3) {
311 state_timer.start(STOMP_WAIT);
318 } else if(hit.left || hit.right) {
324 IMPLEMENT_FACTORY(Yeti, "yeti")