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