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
26 #include "object/camera.hpp"
27 #include "yeti_stalactite.hpp"
28 #include "bouncing_snowball.hpp"
29 #include "game_session.hpp"
31 #include "lisp/writer.hpp"
32 #include "object_factory.hpp"
33 #include "audio/sound_manager.hpp"
35 #include "object/player.hpp"
36 #include "sprite/sprite.hpp"
42 const float JUMP_DOWN_VX = 250; /**< horizontal speed while jumping off the dais */
43 const float JUMP_DOWN_VY = -250; /**< vertical speed while jumping off the dais */
45 const float RUN_VX = 350; /**< horizontal speed while running */
47 const float JUMP_UP_VX = 350; /**< horizontal speed while jumping on the dais */
48 const float JUMP_UP_VY = -800; /**< vertical speed while jumping on the dais */
50 const float STOMP_VY = -250; /** vertical speed while stomping on the dais */
52 const float LEFT_STAND_X = 16; /**< x-coordinate of left dais' end position */
53 const float RIGHT_STAND_X = 800-60-16; /**< x-coordinate of right dais' end position */
54 const float LEFT_JUMP_X = LEFT_STAND_X+224; /**< x-coordinate of from where to jump on the left dais */
55 const float RIGHT_JUMP_X = RIGHT_STAND_X-224; /**< x-coordinate of from where to jump on the right dais */
56 const float STOMP_WAIT = .5; /**< time we stay on the dais before jumping again */
57 const float SAFE_TIME = .5; /**< the time we are safe when tux just hit us */
58 const int INITIAL_HITPOINTS = 3; /**< number of hits we can take */
60 const float SQUISH_TIME = 5;
63 Yeti::Yeti(const lisp::Lisp& reader)
64 : BadGuy(reader, "images/creatures/yeti/yeti.sprite")
66 hit_points = INITIAL_HITPOINTS;
68 sound_manager->preload("sounds/yeti_gna.wav");
69 sound_manager->preload("sounds/yeti_roar.wav");
70 hud_head.reset(new Surface("images/creatures/yeti/hudlife.png"));
85 Yeti::draw(DrawingContext& context)
87 // we blink when we are safe
88 if(safe_timer.started() && size_t(game_time*40)%2)
91 draw_hit_points(context);
93 BadGuy::draw(context);
97 Yeti::draw_hit_points(DrawingContext& context)
101 Surface *hh = hud_head.get();
105 context.push_transform();
106 context.set_translation(Vector(0, 0));
108 for (i = 0; i < hit_points; ++i)
110 context.draw_surface(hh, Vector(BORDER_X + (i * hh->get_width()), BORDER_Y + 1), LAYER_FOREGROUND1);
113 context.pop_transform();
117 Yeti::active_update(float elapsed_time)
121 physic.set_velocity_x((dir==RIGHT)?+JUMP_DOWN_VX:-JUMP_DOWN_VX);
124 physic.set_velocity_x((dir==RIGHT)?+RUN_VX:-RUN_VX);
125 if (((dir == RIGHT) && (get_pos().x >= RIGHT_JUMP_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_JUMP_X))) jump_up();
128 physic.set_velocity_x((dir==RIGHT)?+JUMP_UP_VX:-JUMP_UP_VX);
129 if (((dir == RIGHT) && (get_pos().x >= RIGHT_STAND_X)) || ((dir == LEFT) && (get_pos().x <= LEFT_STAND_X))) be_angry();
132 if(state_timer.check()) {
133 sound_manager->play("sounds/yeti_gna.wav");
134 physic.set_velocity_y(STOMP_VY);
135 sprite->set_action((dir==RIGHT)?"stomp-right":"stomp-left");
139 if (state_timer.check()) {
145 movement = physic.get_movement(elapsed_time);
151 sprite->set_action((dir==RIGHT)?"jump-right":"jump-left");
152 physic.set_velocity_x((dir==RIGHT)?(+JUMP_DOWN_VX):(-JUMP_DOWN_VX));
153 physic.set_velocity_y(JUMP_DOWN_VY);
160 sprite->set_action((dir==RIGHT)?"run-right":"run-left");
161 physic.set_velocity_x((dir==RIGHT)?(+RUN_VX):(-RUN_VX));
162 physic.set_velocity_y(0);
169 sprite->set_action((dir==RIGHT)?"jump-right":"jump-left");
170 physic.set_velocity_x((dir==RIGHT)?(+JUMP_UP_VX):(-JUMP_UP_VX));
171 physic.set_velocity_y(JUMP_UP_VY);
179 dir = (dir==RIGHT)?LEFT:RIGHT;
181 sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
182 physic.set_velocity_x(0);
183 physic.set_velocity_y(0);
184 if (hit_points < INITIAL_HITPOINTS) summon_snowball();
187 state_timer.start(STOMP_WAIT);
191 Yeti::summon_snowball()
193 Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x+(dir == RIGHT ? 64 : -64), get_pos().y), dir));
197 Yeti::collision_squished(GameObject& object)
199 kill_squished(object);
205 Yeti::kill_squished(GameObject& object)
207 Player* player = dynamic_cast<Player*>(&object);
209 player->bounce(*this);
214 void Yeti::take_hit(Player& )
216 if(safe_timer.started())
219 sound_manager->play("sounds/yeti_roar.wav");
222 if(hit_points <= 0) {
224 physic.enable_gravity(true);
225 physic.set_velocity_x(0);
226 physic.set_velocity_y(0);
229 state_timer.start(SQUISH_TIME);
230 set_colgroup_active(COLGROUP_MOVING_ONLY_STATIC);
231 sprite->set_action("dead");
233 if (countMe) Sector::current()->get_level()->stats.badguys++;
235 if(dead_script != "") {
236 std::istringstream stream(dead_script);
237 Sector::current()->run_script(stream, "Yeti - dead-script");
241 safe_timer.start(SAFE_TIME);
248 // shooting bullets or being invincible won't work :)
249 //take_hit(*get_nearest_player()); // FIXME: debug only(?)
253 Yeti::write(lisp::Writer& writer)
255 writer.start_list("yeti");
257 writer.write("x", start_position.x);
258 writer.write("y", start_position.y);
260 writer.end_list("yeti");
264 Yeti::drop_stalactite()
266 // make a stalactite falling down and shake camera a bit
267 Sector::current()->camera->shake(.1f, 0, 10);
269 YetiStalactite* nearest = 0;
270 float dist = FLT_MAX;
272 Player* player = this->get_nearest_player();
275 Sector* sector = Sector::current();
276 for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
277 i != sector->gameobjects.end(); ++i) {
278 YetiStalactite* stalactite = dynamic_cast<YetiStalactite*> (*i);
279 if(stalactite && stalactite->is_hanging()) {
281 = fabsf(stalactite->get_pos().x - player->get_pos().x);
283 nearest = stalactite;
290 nearest->start_shaking();
294 Yeti::collision_solid(const CollisionHit& hit)
296 if(hit.top || hit.bottom) {
298 physic.set_velocity_y(0);
309 if(!state_timer.started()) {
310 sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
314 // go to other side after 3 jumps
315 if(stomp_count == 3) {
319 state_timer.start(STOMP_WAIT);
326 } else if(hit.left || hit.right) {
332 IMPLEMENT_FACTORY(Yeti, "yeti")