f810d7c0d12a0037ea5b28eb6de56a0827f5dfed
[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 #include "random_generator.hpp"
25 #include "lisp/writer.hpp"
26 #include "object_factory.hpp"
27 #include "object/player.hpp"
28 #include "sprite/sprite.hpp"
29
30 static const int SHAKE_RANGE_X = 40;
31 static const float SHAKE_TIME = .8f;
32 static const float SQUISH_TIME = 2;
33 static const float SHAKE_RANGE_Y = 400;
34
35 Stalactite::Stalactite(const lisp::Lisp& lisp)
36         : BadGuy(lisp, "images/creatures/stalactite/stalactite.sprite", LAYER_TILES - 1), state(STALACTITE_HANGING)
37 {
38   countMe = false;
39   set_colgroup_active(COLGROUP_TOUCHABLE);
40 }
41
42 void
43 Stalactite::write(lisp::Writer& writer)
44 {
45   writer.start_list("stalactite");
46   writer.write("x", start_position.x);
47   writer.write("y", start_position.y);
48   writer.end_list("stalactite");
49 }
50
51 void
52 Stalactite::active_update(float elapsed_time)
53 {
54   if(state == STALACTITE_HANGING) {
55     Player* player = this->get_nearest_player();
56     if (player) {
57       if(player->get_bbox().p2.x > bbox.p1.x - SHAKE_RANGE_X
58           && player->get_bbox().p1.x < bbox.p2.x + SHAKE_RANGE_X
59           && player->get_bbox().p2.y > bbox.p1.y
60           && player->get_bbox().p1.y < bbox.p2.y + SHAKE_RANGE_Y) {
61         timer.start(SHAKE_TIME);
62         state = STALACTITE_SHAKING;
63       }
64     }
65   } else if(state == STALACTITE_SHAKING) {
66     if(timer.check()) {
67       state = STALACTITE_FALLING;
68       physic.enable_gravity(true);
69       set_colgroup_active(COLGROUP_MOVING);
70     }
71   } else if(state == STALACTITE_FALLING || state == STALACTITE_SQUISHED) {
72     movement = physic.get_movement(elapsed_time);
73     if(state == STALACTITE_SQUISHED && timer.check())
74       remove_me();
75   }
76 }
77
78 void
79 Stalactite::squish()
80 {
81   state = STALACTITE_SQUISHED;
82   set_colgroup_active(COLGROUP_MOVING_ONLY_STATIC);
83   sprite->set_action("squished");
84   if(!timer.started())
85     timer.start(SQUISH_TIME);
86 }
87
88 void
89 Stalactite::collision_solid(const CollisionHit& hit)
90 {
91   if(state == STALACTITE_FALLING) {
92     if (hit.bottom) squish();
93   }
94   if(state == STALACTITE_SQUISHED) {
95     physic.set_velocity_y(0);
96   }
97 }
98
99 HitResponse
100 Stalactite::collision_player(Player& player)
101 {
102   if(state != STALACTITE_SQUISHED) {
103     player.kill(false);
104   }
105
106   return FORCE_MOVE;
107 }
108
109 HitResponse
110 Stalactite::collision_badguy(BadGuy& other, const CollisionHit& hit)
111 {
112   if (state == STALACTITE_SQUISHED) return FORCE_MOVE;
113
114   // ignore other Stalactites
115   if (dynamic_cast<Stalactite*>(&other)) return FORCE_MOVE;
116
117   if (state != STALACTITE_FALLING) return BadGuy::collision_badguy(other, hit);
118
119   if (other.is_freezable()) {
120     other.freeze();
121   } else {
122     other.kill_fall();
123   }
124
125   remove_me();
126
127   return FORCE_MOVE;
128 }
129
130 void
131 Stalactite::kill_fall()
132 {
133 }
134
135 void
136 Stalactite::draw(DrawingContext& context)
137 {
138   if(get_state() != STATE_ACTIVE)
139     return;
140
141
142   if(state == STALACTITE_SQUISHED) {
143     sprite->draw(context, get_pos(), LAYER_OBJECTS);
144     return;
145   }
146
147   if(state == STALACTITE_SHAKING) {
148     sprite->draw(context, get_pos() + Vector(systemRandom.rand(-3,3), 0), layer);
149   } else {
150     sprite->draw(context, get_pos(), layer);
151   }
152 }
153
154 void
155 Stalactite::deactivate()
156 {
157   if(state != STALACTITE_HANGING)
158     remove_me();
159 }
160
161 IMPLEMENT_FACTORY(Stalactite, "stalactite")