3 // SuperTux - Badguy "Snail"
4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
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.
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.
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.
23 #include "object/block.hpp"
26 const float WALKSPEED = 80;
27 const float KICKSPEED = 500;
28 const int MAXSQUISHES = 10;
29 const float KICKSPEED_Y = -500; /**< y-velocity gained when kicked */
32 Snail::Snail(const lisp::Lisp& reader)
33 : BadGuy(reader, "images/creatures/snail/snail.sprite"), state(STATE_NORMAL), squishcount(0)
35 sound_manager->preload("sounds/iceblock_bump.wav");
36 sound_manager->preload("sounds/stomp.wav");
37 sound_manager->preload("sounds/kick.wav");
38 recently_changed_direction = false;
41 Snail::Snail(const Vector& pos, Direction d)
42 : BadGuy(pos, d, "images/creatures/snail/snail.sprite"), state(STATE_NORMAL), squishcount(0)
44 sound_manager->preload("sounds/iceblock_bump.wav");
45 sound_manager->preload("sounds/stomp.wav");
46 sound_manager->preload("sounds/kick.wav");
47 recently_changed_direction = false;
51 Snail::write(lisp::Writer& writer)
53 writer.start_list("snail");
55 writer.write_float("x", start_position.x);
56 writer.write_float("y", start_position.y);
58 writer.end_list("snail");
71 sprite->set_action(dir == LEFT ? "left" : "right");
73 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
80 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
83 physic.set_velocity_x(0);
84 physic.set_velocity_y(0);
92 state = STATE_KICKED_DELAY;
93 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
96 physic.set_velocity_x(0);
97 physic.set_velocity_y(0);
99 // start a timer to delay addition of upward movement until we are (hopefully) out from under the player
100 kicked_delay_timer.start(0.05);
105 Snail::active_update(float elapsed_time)
107 recently_changed_direction = false;
111 if (might_fall(601)) {
112 if( recently_changed_direction ) break;
113 recently_changed_direction = true;
114 dir = (dir == LEFT ? RIGHT : LEFT);
115 sprite->set_action(dir == LEFT ? "left" : "right");
116 physic.set_velocity_x(-physic.get_velocity_x());
121 if (flat_timer.started()) {
122 sprite->set_fps(64 - 15 * flat_timer.get_timegone());
124 if (flat_timer.check()) {
129 case STATE_KICKED_DELAY:
130 if (kicked_delay_timer.check()) {
131 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
132 physic.set_velocity_y(KICKSPEED_Y);
133 state = STATE_KICKED;
138 physic.set_velocity_x(physic.get_velocity_x() * pow(0.99, elapsed_time/0.02));
139 if (fabsf(physic.get_velocity_x()) < WALKSPEED) be_normal();
143 BadGuy::active_update(elapsed_time);
147 Snail::collision_solid(const CollisionHit& hit)
149 if(hit.top || hit.bottom) { // floor or roof
150 physic.set_velocity_y(0);
155 case STATE_KICKED_DELAY:
167 if( recently_changed_direction ) break;
168 recently_changed_direction = true;
169 dir = dir == LEFT ? RIGHT : LEFT;
170 sprite->set_action(dir == LEFT ? "left" : "right");
171 physic.set_velocity_x(-physic.get_velocity_x());
175 case STATE_KICKED_DELAY:
176 physic.set_velocity_x(0);
180 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
183 // TODO move this into BonusBlock code
184 // open bonusblocks, crash bricks
185 BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
187 bonusblock->try_open();
189 Brick* brick = dynamic_cast<Brick*> (&object);
194 if( recently_changed_direction ) break;
195 recently_changed_direction = true;
196 dir = (dir == LEFT) ? RIGHT : LEFT;
197 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
199 physic.set_velocity_x(-physic.get_velocity_x()*0.75);
200 if (fabsf(physic.get_velocity_x()) < WALKSPEED) be_normal();
208 Snail::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
212 if(hit.left || hit.right) {
213 if( recently_changed_direction ) return CONTINUE;
214 recently_changed_direction = true;
215 dir = (dir == LEFT) ? RIGHT : LEFT;
216 sprite->set_action(dir == LEFT ? "left" : "right");
217 physic.set_velocity_x(-physic.get_velocity_x());
221 case STATE_KICKED_DELAY:
234 Snail::collision_squished(Player& player)
241 if(squishcount >= MAXSQUISHES) {
246 sound_manager->play("sounds/stomp.wav", get_pos());
251 sound_manager->play("sounds/kick.wav", get_pos());
253 if(player.get_pos().x < get_pos().x) {
261 case STATE_KICKED_DELAY:
266 player.bounce(*this);
270 IMPLEMENT_FACTORY(Snail, "snail")