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");
236 safe_timer.start(SAFE_TIME);
243 // shooting bullets or being invincible won't work :)
244 //take_hit(*get_nearest_player()); // FIXME: debug only(?)
248 Yeti::write(lisp::Writer& writer)
250 writer.start_list("yeti");
252 writer.write("x", start_position.x);
253 writer.write("y", start_position.y);
255 writer.end_list("yeti");
259 Yeti::drop_stalactite()
261 // make a stalactite falling down and shake camera a bit
262 Sector::current()->camera->shake(.1f, 0, 10);
264 YetiStalactite* nearest = 0;
265 float dist = FLT_MAX;
267 Player* player = this->get_nearest_player();
270 Sector* sector = Sector::current();
271 for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
272 i != sector->gameobjects.end(); ++i) {
273 YetiStalactite* stalactite = dynamic_cast<YetiStalactite*> (*i);
274 if(stalactite && stalactite->is_hanging()) {
276 = fabsf(stalactite->get_pos().x - player->get_pos().x);
278 nearest = stalactite;
285 nearest->start_shaking();
289 Yeti::collision_solid(const CollisionHit& hit)
291 if(hit.top || hit.bottom) {
293 physic.set_velocity_y(0);
304 if(!state_timer.started()) {
305 sprite->set_action((dir==RIGHT)?"stand-right":"stand-left");
309 // go to other side after 3 jumps
310 if(stomp_count == 3) {
314 state_timer.start(STOMP_WAIT);
321 } else if(hit.left || hit.right) {
327 IMPLEMENT_FACTORY(Yeti, "yeti")