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