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