Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[supertux.git] / src / badguy / stalactite.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "stalactite.hpp"
23
24 static const int SHAKE_RANGE = 40;
25 static const float SHAKE_TIME = .8;
26 static const float SQUISH_TIME = 2;
27
28 Stalactite::Stalactite(const lisp::Lisp& lisp)
29 {
30   lisp.get("x", start_position.x);
31   lisp.get("y", start_position.y);
32   bbox.set_size(31.8, 31.8);
33   sprite = sprite_manager->create("images/creatures/stalactite/stalactite.sprite");
34   state = STALACTITE_HANGING;
35   countMe = false;
36 }
37
38 void
39 Stalactite::write(lisp::Writer& writer)
40 {
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");
45 }
46
47 void
48 Stalactite::active_update(float elapsed_time)
49 {
50   if(state == STALACTITE_HANGING) {
51     Player* player = this->get_nearest_player();
52     if (player) {
53       if(player->get_bbox().p2.x > bbox.p1.x - SHAKE_RANGE
54           && player->get_bbox().p1.x < bbox.p2.x + SHAKE_RANGE
55           && player->get_bbox().p2.y > bbox.p1.y) {
56         timer.start(SHAKE_TIME);
57         state = STALACTITE_SHAKING;
58       }
59     }
60   } else if(state == STALACTITE_SHAKING) {
61     if(timer.check()) {
62       state = STALACTITE_FALLING;
63       physic.enable_gravity(true);
64     }
65   } else if(state == STALACTITE_FALLING || state == STALACTITE_SQUISHED) {
66     movement = physic.get_movement(elapsed_time);
67     if(state == STALACTITE_SQUISHED && timer.check())
68       remove_me();
69   }
70 }
71
72 HitResponse
73 Stalactite::collision_solid(GameObject& , const CollisionHit& hit)
74 {
75   if(state != STALACTITE_FALLING && state != STALACTITE_SQUISHED)
76     return FORCE_MOVE;
77   
78   if(hit.normal.y < .9) { // hit floor?
79     state = STALACTITE_SQUISHED;
80     set_group(COLGROUP_MOVING_ONLY_STATIC);
81     physic.set_velocity_y(0);
82     sprite->set_action("squished");
83     if(!timer.started())
84       timer.start(SQUISH_TIME);
85   }
86
87   return CONTINUE;
88 }
89
90 HitResponse
91 Stalactite::collision_player(Player& player, const CollisionHit& )
92 {
93   if(state != STALACTITE_SQUISHED) {
94     player.kill(Player::SHRINK);
95   }
96
97   return FORCE_MOVE;
98 }
99
100 void
101 Stalactite::kill_fall()
102 {
103 }
104
105 void
106 Stalactite::draw(DrawingContext& context)
107 {
108   if(get_state() != STATE_ACTIVE)
109     return;
110     
111   if(state == STALACTITE_SHAKING) {
112     sprite->draw(context, get_pos() + Vector((rand() % 6)-3, 0), LAYER_OBJECTS);
113   } else {
114     sprite->draw(context, get_pos(), LAYER_OBJECTS);
115   }
116 }
117
118 void
119 Stalactite::deactivate()
120 {
121   if(state != STALACTITE_HANGING)
122     remove_me();
123 }
124
125 IMPLEMENT_FACTORY(Stalactite, "stalactite")