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
23 #include "stalactite.h"
25 static const int SHAKE_RANGE = 40;
26 static const float SHAKE_TIME = .8;
27 static const float SQUISH_TIME = 2;
29 Stalactite::Stalactite(const lisp::Lisp& lisp)
31 lisp.get("x", start_position.x);
32 lisp.get("y", start_position.y);
33 bbox.set_size(31.8, 31.8);
34 sprite = sprite_manager->create("stalactite");
35 state = STALACTITE_HANGING;
39 Stalactite::write(lisp::Writer& writer)
41 writer.start_list("stalactite");
42 writer.write_float("x", start_position.x);
43 writer.write_float("y", start_position.y);
44 writer.end_list("stalactite");
48 Stalactite::active_action(float elapsed_time)
50 if(state == STALACTITE_HANGING) {
51 Player* player = Sector::current()->player;
52 if(player->get_bbox().p2.x > bbox.p1.x - SHAKE_RANGE
53 && player->get_bbox().p1.x < bbox.p2.x + SHAKE_RANGE
54 && player->get_bbox().p2.y > bbox.p1.y) {
55 timer.start(SHAKE_TIME);
56 state = STALACTITE_SHAKING;
58 } else if(state == STALACTITE_SHAKING) {
60 state = STALACTITE_FALLING;
61 physic.enable_gravity(true);
63 } else if(state == STALACTITE_FALLING || state == STALACTITE_SQUISHED) {
64 movement = physic.get_movement(elapsed_time);
65 if(state == STALACTITE_SQUISHED && timer.check())
71 Stalactite::collision_solid(GameObject& , const CollisionHit& hit)
73 if(state != STALACTITE_FALLING && state != STALACTITE_SQUISHED)
76 if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
77 state = STALACTITE_SQUISHED;
78 physic.set_velocity_y(0);
79 sprite->set_action("squished");
81 timer.start(SQUISH_TIME);
88 Stalactite::collision_player(Player& player, const CollisionHit& )
90 if(state != STALACTITE_SQUISHED) {
91 player.kill(Player::SHRINK);
98 Stalactite::kill_fall()
103 Stalactite::draw(DrawingContext& context)
105 if(get_state() != STATE_ACTIVE)
108 if(state == STALACTITE_SHAKING) {
109 sprite->draw(context, get_pos() + Vector((rand() % 6)-3, 0), LAYER_OBJECTS);
111 sprite->draw(context, get_pos(), LAYER_OBJECTS);
116 Stalactite::deactivate()
118 if(state != STALACTITE_HANGING)
122 IMPLEMENT_FACTORY(Stalactite, "stalactite")