4 // Copyright (C) 2005 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // 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 static const float JUMP_VEL1 = 250;
32 static const float JUMP_VEL2 = 700;
33 static const float RUN_SPEED = 350;
34 static const float JUMP_TIME = 1.6;
35 static const float ANGRY_JUMP_WAIT = .5;
36 /// the time we are safe when tux just hit us
37 static const float SAFE_TIME = .5;
38 static const int INITIAL_HITPOINTS = 3;
40 Yeti::Yeti(const lisp::Lisp& reader)
42 reader.get("x", start_position.x);
43 reader.get("y", start_position.y);
44 bbox.set_size(80, 120);
45 sprite = sprite_manager->create("images/creatures/yeti/yeti.sprite");
46 sprite->set_action("right");
50 sound_manager->preload_sound("yeti_gna");
51 sound_manager->preload_sound("yeti_roar");
53 hit_points = INITIAL_HITPOINTS;
54 reader.get("dead-script", dead_script);
63 Yeti::draw(DrawingContext& context)
65 // we blink when we are safe
66 if(safe_timer.started() && size_t(game_time*40)%2)
69 BadGuy::draw(context);
73 Yeti::active_update(float elapsed_time)
79 physic.set_velocity_x(RUN_SPEED);
81 physic.set_velocity_y(JUMP_VEL2);
84 physic.set_velocity_x(-RUN_SPEED);
86 physic.set_velocity_y(JUMP_VEL2);
91 sound_manager->play("sounds/yeti_gna.wav");
92 physic.set_velocity_y(JUMP_VEL1);
93 if (side == LEFT) // on the left, facing Tux who is on the right
94 sprite->set_action("jump-right");
96 sprite->set_action("jump-left");
103 movement = physic.get_movement(elapsed_time);
109 // jump and move right
110 sprite->set_action("right");
111 physic.set_velocity_y(JUMP_VEL1);
112 physic.set_velocity_x(RUN_SPEED);
114 timer.start(JUMP_TIME);
120 sprite->set_action("left");
121 physic.set_velocity_y(JUMP_VEL1);
122 physic.set_velocity_x(-RUN_SPEED);
124 timer.start(JUMP_TIME);
128 Yeti::angry_jumping()
131 timer.start(ANGRY_JUMP_WAIT);
132 state = ANGRY_JUMPING;
133 physic.set_velocity_x(0);
137 Yeti::summon_snowball()
139 Sector::current()->add_object(new BouncingSnowball(get_pos().x+(side == LEFT ? 64 : -64), get_pos().y, (side == LEFT ? RIGHT : LEFT)));
143 Yeti::collision_squished(Player& player)
145 if(safe_timer.started())
148 player.bounce(*this);
149 sound_manager->play("sounds/yeti_roar.wav");
151 if(hit_points <= 0) {
152 sprite->set_action("dead");
153 kill_squished(player);
156 if(dead_script != "") {
157 std::istringstream stream(dead_script);
158 Sector::current()->run_script(stream, "Yeti - dead-script");
161 safe_timer.start(SAFE_TIME);
170 // shooting bullets or being invincible won't work :)
174 Yeti::write(lisp::Writer& writer)
176 writer.start_list("yeti");
178 writer.write_float("x", start_position.x);
179 writer.write_float("y", start_position.y);
181 if(dead_script != "") {
182 writer.write_string("dead-script", dead_script);
185 writer.end_list("yeti");
189 Yeti::drop_stalactite()
191 YetiStalactite* nearest = 0;
192 float dist = FLT_MAX;
194 Player* player = this->get_nearest_player();
197 Sector* sector = Sector::current();
198 for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
199 i != sector->gameobjects.end(); ++i) {
200 YetiStalactite* stalactite = dynamic_cast<YetiStalactite*> (*i);
201 if(stalactite && stalactite->is_hanging()) {
203 = fabsf(stalactite->get_pos().x - player->get_pos().x);
205 nearest = stalactite;
212 nearest->start_shaking();
216 Yeti::collision_solid(GameObject& , const CollisionHit& hit)
218 if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
219 physic.set_velocity_y(0);
222 } else if(state == GO_LEFT && !timer.started()) {
225 sprite->set_action("stand-right");
227 } else if(state == GO_RIGHT && !timer.started()) {
230 sprite->set_action("stand-left");
232 } else if(state == ANGRY_JUMPING) {
233 if(!timer.started()) {
235 if (side == LEFT) // standing on the left, facing Tux who is on the right
236 sprite->set_action("stand-right");
238 sprite->set_action("stand-left");
240 // make a stalactite falling down and shake camera a bit
241 Sector::current()->camera->shake(.1, 0, 10);
244 // go to other side after 3 jumps
252 timer.start(ANGRY_JUMP_WAIT);
261 IMPLEMENT_FACTORY(Yeti, "yeti")