c3216b7ab05e7aa07f518d7163c5212d5a3d004b
[supertux.git] / src / badguy / snail.cpp
1 //  $Id$
2 //
3 //  SuperTux - Badguy "Snail"
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.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 "snail.hpp"
23
24 #include "object/block.hpp"
25 #include "lisp/writer.hpp"
26 #include "sprite/sprite.hpp"
27 #include "object_factory.hpp"
28 #include "audio/sound_manager.hpp"
29 #include "object/player.hpp"
30
31 #include <math.h>
32
33 namespace {
34   const float KICKSPEED = 500;
35   const int MAXSQUISHES = 10;
36   const float KICKSPEED_Y = -500; /**< y-velocity gained when kicked */
37 }
38
39 Snail::Snail(const lisp::Lisp& reader)
40   : WalkingBadguy(reader, "images/creatures/snail/snail.sprite", "left", "right"), state(STATE_NORMAL), squishcount(0)
41 {
42   walk_speed = 80;
43   max_drop_height = 600;
44   sound_manager->preload("sounds/iceblock_bump.wav");
45   sound_manager->preload("sounds/stomp.wav");
46   sound_manager->preload("sounds/kick.wav");
47 }
48
49 Snail::Snail(const Vector& pos, Direction d)
50   : WalkingBadguy(pos, d, "images/creatures/snail/snail.sprite", "left", "right"), state(STATE_NORMAL), squishcount(0)
51 {
52   walk_speed = 80;
53   max_drop_height = 600;
54   sound_manager->preload("sounds/iceblock_bump.wav");
55   sound_manager->preload("sounds/stomp.wav");
56   sound_manager->preload("sounds/kick.wav");
57 }
58
59 void
60 Snail::write(lisp::Writer& writer)
61 {
62   writer.start_list("snail");
63   WalkingBadguy::write(writer);
64   writer.end_list("snail");
65 }
66
67 void
68 Snail::initialize()
69 {
70   WalkingBadguy::initialize();
71   be_normal();
72 }
73
74 void
75 Snail::be_normal()
76 {
77   if (state == STATE_NORMAL) return;
78
79   state = STATE_NORMAL;
80   WalkingBadguy::initialize();
81 }
82
83 void
84 Snail::be_flat()
85 {
86   state = STATE_FLAT;
87   sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
88   sprite->set_fps(64);
89
90   physic.set_velocity_x(0);
91   physic.set_velocity_y(0);
92
93   flat_timer.start(4);
94 }
95
96 void
97 Snail::be_kicked()
98 {
99   state = STATE_KICKED_DELAY;
100   sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
101   sprite->set_fps(64);
102
103   physic.set_velocity_x(0);
104   physic.set_velocity_y(0);
105
106   // start a timer to delay addition of upward movement until we are (hopefully) out from under the player
107   kicked_delay_timer.start(0.05f);
108 }
109
110 bool
111 Snail::can_break(){
112     return state == STATE_KICKED;
113 }
114
115 void
116 Snail::active_update(float elapsed_time)
117 {
118   switch (state) {
119
120     case STATE_NORMAL:
121       WalkingBadguy::active_update(elapsed_time);
122       break;
123
124     case STATE_FLAT:
125       if (flat_timer.started()) {
126         sprite->set_fps(64 - 15 * flat_timer.get_timegone());
127       }
128       if (flat_timer.check()) {
129         be_normal();
130       }
131       BadGuy::active_update(elapsed_time);
132       break;
133
134     case STATE_KICKED_DELAY:
135       if (kicked_delay_timer.check()) {
136         physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
137         physic.set_velocity_y(KICKSPEED_Y);
138         state = STATE_KICKED;
139       }
140       BadGuy::active_update(elapsed_time);
141       break;
142
143     case STATE_KICKED:
144       physic.set_velocity_x(physic.get_velocity_x() * pow(0.99, elapsed_time/0.02));
145       if (fabsf(physic.get_velocity_x()) < walk_speed) be_normal();
146       BadGuy::active_update(elapsed_time);
147       break;
148
149   }
150 }
151
152 void
153 Snail::collision_solid(const CollisionHit& hit)
154 {
155   update_on_ground_flag(hit);
156
157   switch (state) {
158     case STATE_NORMAL:
159       WalkingBadguy::collision_solid(hit);
160       break;
161     case STATE_FLAT:
162       if(hit.top || hit.bottom) {
163         physic.set_velocity_y(0);
164       }
165       if(hit.left || hit.right) {
166       }
167       break;
168     case STATE_KICKED_DELAY:
169       if(hit.top || hit.bottom) {
170         physic.set_velocity_y(0);
171       }
172       if(hit.left || hit.right) {
173         physic.set_velocity_x(0);
174       }
175       break;
176     case STATE_KICKED:
177       if(hit.top || hit.bottom) {
178         physic.set_velocity_y(0);
179       }
180       if(hit.left || hit.right) {
181         sound_manager->play("sounds/iceblock_bump.wav", get_pos());
182
183         if( ( dir == LEFT && hit.left ) || ( dir == RIGHT && hit.right) ){
184           dir = (dir == LEFT) ? RIGHT : LEFT;
185           sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
186
187           physic.set_velocity_x(-physic.get_velocity_x()*0.75);
188           if (fabsf(physic.get_velocity_x()) < walk_speed) be_normal();
189         }
190
191       }
192       break;
193   }
194
195 }
196
197 HitResponse
198 Snail::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
199 {
200   switch(state) {
201     case STATE_NORMAL:
202       return WalkingBadguy::collision_badguy(badguy, hit);
203     case STATE_FLAT:
204     case STATE_KICKED_DELAY:
205       return FORCE_MOVE;
206     case STATE_KICKED:
207       badguy.kill_fall();
208       return FORCE_MOVE;
209     default:
210       assert(false);
211   }
212
213   return ABORT_MOVE;
214 }
215
216 bool
217 Snail::collision_squished(GameObject& object)
218 {
219   switch(state) {
220
221     case STATE_KICKED:
222     case STATE_NORMAL:
223       {
224         Player* player = dynamic_cast<Player*>(&object);
225         squishcount++;
226         if ((squishcount >= MAXSQUISHES) || (player && player->does_buttjump)) {
227           kill_fall();
228           return true;
229         }
230       }
231
232       sound_manager->play("sounds/stomp.wav", get_pos());
233       be_flat();
234       break;
235
236     case STATE_FLAT:
237       sound_manager->play("sounds/kick.wav", get_pos());
238       {
239         MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
240         if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
241           dir = RIGHT;
242         } else {
243           dir = LEFT;
244         }
245       }
246       be_kicked();
247       break;
248
249     case STATE_KICKED_DELAY:
250       break;
251
252   }
253
254   Player* player = dynamic_cast<Player*>(&object);
255   if (player) player->bounce(*this);
256   return true;
257 }
258
259 IMPLEMENT_FACTORY(Snail, "snail")