commented out an error-message line in glutil.hpp that caused linking errors
[supertux.git] / src / badguy / snowsnail.cpp
1 //  $Id: snowsnail.cpp 2738 2005-10-08 08:53:46Z wansti $
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 "snowsnail.hpp"
24 #include "object/block.hpp"
25
26 static const float WALKSPEED = 80;
27 static const float KICKSPEED = 500;
28 static const int MAXSQUISHES = 10;
29
30 SnowSnail::SnowSnail(const lisp::Lisp& reader)
31   : ice_state(ICESTATE_NORMAL), squishcount(0)
32 {
33   reader.get("x", start_position.x);
34   reader.get("y", start_position.y);
35   bbox.set_size(31.8, 31.8);
36   sprite = sprite_manager->create("snowsnail");
37   set_direction = false;
38 }
39
40 SnowSnail::SnowSnail(float pos_x, float pos_y, Direction d)
41   : ice_state(ICESTATE_NORMAL), squishcount(0)
42 {
43   start_position.x = pos_x;
44   start_position.y = pos_y;
45   bbox.set_size(31.8, 31.8);
46   sprite = sprite_manager->create("snowsnail");
47   set_direction = true;
48   initial_direction = d;
49 }
50
51 void
52 SnowSnail::write(lisp::Writer& writer)
53 {
54   writer.start_list("mriceblock");
55
56   writer.write_float("x", start_position.x);
57   writer.write_float("y", start_position.y);
58
59   writer.end_list("snowsnail");
60 }
61
62 void
63 SnowSnail::activate()
64 {
65   if (set_direction) {dir = initial_direction;}
66   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
67   sprite->set_action(dir == LEFT ? "left" : "right");
68 }
69
70 void
71 SnowSnail::active_update(float elapsed_time)
72 {
73   if(flat_timer.started()) {
74     sprite->set_fps(64 - 15 * flat_timer.get_timegone());
75   }
76   if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
77     ice_state = ICESTATE_NORMAL;
78     physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
79     sprite->set_action(dir == LEFT ? "left" : "right");
80   }
81   BadGuy::active_update(elapsed_time);
82 }
83
84 HitResponse
85 SnowSnail::collision_solid(GameObject& object, const CollisionHit& hit)
86 {
87   if(fabsf(hit.normal.y) > .5) { // floor or roof
88     physic.set_velocity_y(0);
89     return CONTINUE;
90   }
91   // hit left or right
92   switch(ice_state) {
93     case ICESTATE_NORMAL:
94       dir = dir == LEFT ? RIGHT : LEFT;
95       sprite->set_action(dir == LEFT ? "left" : "right");
96       physic.set_velocity_x(-physic.get_velocity_x());       
97       break;
98     case ICESTATE_KICKED: {
99       BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
100       if(bonusblock) {
101         bonusblock->try_open();
102       }
103       Brick* brick = dynamic_cast<Brick*> (&object);
104       if(brick) {
105         brick->try_break();
106       }
107       
108       dir = dir == LEFT ? RIGHT : LEFT;
109       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
110       physic.set_velocity_x(-physic.get_velocity_x());
111       sound_manager->play("sounds/iceblock_bump.wav", get_pos());
112       break;
113     }
114     case ICESTATE_FLAT:
115       physic.set_velocity_x(0);
116       break;
117   }
118
119   return CONTINUE;
120 }
121
122 HitResponse
123 SnowSnail::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
124 {
125   switch(ice_state) {
126     case ICESTATE_NORMAL:
127       if(fabsf(hit.normal.x) > .8) {
128         dir = dir == LEFT ? RIGHT : LEFT;
129         sprite->set_action(dir == LEFT ? "left" : "right");
130         physic.set_velocity_x(-physic.get_velocity_x());               
131       }
132       return CONTINUE;
133     case ICESTATE_FLAT:
134       return FORCE_MOVE;
135     case ICESTATE_KICKED:
136       badguy.kill_fall();
137       return FORCE_MOVE;
138     default:
139       assert(false);
140   }
141
142   return ABORT_MOVE;
143 }
144
145 bool
146 SnowSnail::collision_squished(Player& player)
147 {
148   switch(ice_state) {
149     case ICESTATE_KICKED:
150     case ICESTATE_NORMAL:
151       squishcount++;
152       if(squishcount >= MAXSQUISHES) {
153         kill_fall();
154         return true;
155       }
156
157       // flatten
158       sound_manager->play("sounds/stomp.wav", get_pos());
159       physic.set_velocity_x(0);
160       physic.set_velocity_y(0); 
161       
162       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
163       sprite->set_fps(64);
164       flat_timer.start(4);
165       ice_state = ICESTATE_FLAT;
166       break;
167     case ICESTATE_FLAT:
168       // kick
169       sound_manager->play("sounds/kick.wav", get_pos());
170
171       if(player.get_pos().x < get_pos().x) {
172         dir = RIGHT;
173       } else {
174         dir = LEFT;
175       }
176       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
177       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
178       ice_state = ICESTATE_KICKED;
179       break;
180   }
181
182   player.bounce(*this);
183   return true;
184 }
185
186 IMPLEMENT_FACTORY(SnowSnail, "snowsnail")