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.25;
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(60, 90);
45 sprite = sprite_manager->create("images/creatures/yeti/yeti.sprite");
46 sprite->set_action("right");
49 hit_points = INITIAL_HITPOINTS;
50 reader.get("dead-script", dead_script);
59 Yeti::draw(DrawingContext& context)
61 // we blink when we are safe
62 if(safe_timer.started() && size_t(game_time*40)%2)
65 BadGuy::draw(context);
69 Yeti::active_update(float elapsed_time)
75 physic.set_velocity_x(RUN_SPEED);
77 physic.set_velocity_y(JUMP_VEL2);
80 physic.set_velocity_x(-RUN_SPEED);
82 physic.set_velocity_y(JUMP_VEL2);
87 sound_manager->play("sounds/yeti_gna.wav");
88 physic.set_velocity_y(JUMP_VEL1);
89 if (side == LEFT) // on the left, facing Tux who is on the right
90 sprite->set_action("jump-right");
92 sprite->set_action("jump-left");
99 movement = physic.get_movement(elapsed_time);
105 // jump and move right
106 sprite->set_action("right");
107 physic.set_velocity_y(JUMP_VEL1);
108 physic.set_velocity_x(RUN_SPEED);
110 timer.start(JUMP_TIME);
116 sprite->set_action("left");
117 physic.set_velocity_y(JUMP_VEL1);
118 physic.set_velocity_x(-RUN_SPEED);
120 timer.start(JUMP_TIME);
124 Yeti::angry_jumping()
127 timer.start(ANGRY_JUMP_WAIT);
128 state = ANGRY_JUMPING;
129 physic.set_velocity_x(0);
133 Yeti::summon_snowball()
135 Sector::current()->add_object(new BouncingSnowball(get_pos().x+(side == LEFT ? 64 : -64), get_pos().y, (side == LEFT ? RIGHT : LEFT)));
139 Yeti::collision_squished(Player& player)
141 if(safe_timer.started())
144 player.bounce(*this);
145 sound_manager->play("sounds/yeti_roar.wav");
147 if(hit_points <= 0) {
148 sprite->set_action("dead");
149 kill_squished(player);
152 if(dead_script != "") {
153 std::istringstream stream(dead_script);
154 Sector::current()->run_script(stream, "Yeti - dead-script");
157 safe_timer.start(SAFE_TIME);
166 // shooting bullets or being invincible won't work :)
170 Yeti::write(lisp::Writer& writer)
172 writer.start_list("yeti");
174 writer.write_float("x", start_position.x);
175 writer.write_float("y", start_position.y);
177 if(dead_script != "") {
178 writer.write_string("dead-script", dead_script);
181 writer.end_list("yeti");
185 Yeti::drop_stalactite()
187 YetiStalactite* nearest = 0;
188 float dist = FLT_MAX;
190 Player* player = this->get_nearest_player();
193 Sector* sector = Sector::current();
194 for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
195 i != sector->gameobjects.end(); ++i) {
196 YetiStalactite* stalactite = dynamic_cast<YetiStalactite*> (*i);
197 if(stalactite && stalactite->is_hanging()) {
199 = fabsf(stalactite->get_pos().x - player->get_pos().x);
201 nearest = stalactite;
208 nearest->start_shaking();
212 Yeti::collision_solid(GameObject& , const CollisionHit& hit)
214 if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
215 physic.set_velocity_y(0);
218 } else if(state == GO_LEFT && !timer.started()) {
221 sprite->set_action("stand-right");
223 } else if(state == GO_RIGHT && !timer.started()) {
226 sprite->set_action("stand-left");
228 } else if(state == ANGRY_JUMPING) {
229 if(!timer.started()) {
231 if (side == LEFT) // standing on the left, facing Tux who is on the right
232 sprite->set_action("stand-right");
234 sprite->set_action("stand-left");
236 // make a stalactite falling down and shake camera a bit
237 Sector::current()->camera->shake(.1, 0, 10);
240 // go to other side after 3 jumps
248 timer.start(ANGRY_JUMP_WAIT);
257 IMPLEMENT_FACTORY(Yeti, "yeti")