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");
40 Snail::Snail(const Vector& pos, Direction d)
41 : BadGuy(pos, d, "images/creatures/snail/snail.sprite"), state(STATE_NORMAL), squishcount(0)
43 sound_manager->preload("sounds/iceblock_bump.wav");
44 sound_manager->preload("sounds/stomp.wav");
45 sound_manager->preload("sounds/kick.wav");
49 Snail::write(lisp::Writer& writer)
51 writer.start_list("snail");
53 writer.write_float("x", start_position.x);
54 writer.write_float("y", start_position.y);
56 writer.end_list("snail");
69 sprite->set_action(dir == LEFT ? "left" : "right");
71 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
78 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
81 physic.set_velocity_x(0);
82 physic.set_velocity_y(0);
90 state = STATE_KICKED_DELAY;
91 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
94 physic.set_velocity_x(0);
95 physic.set_velocity_y(0);
97 // start a timer to delay addition of upward movement until we are (hopefully) out from under the player
98 kicked_delay_timer.start(0.05);
103 Snail::active_update(float elapsed_time)
108 if (on_ground() && might_fall(601)) {
109 dir = (dir == LEFT ? RIGHT : LEFT);
110 sprite->set_action(dir == LEFT ? "left" : "right");
111 physic.set_velocity_x(-physic.get_velocity_x());
116 if (flat_timer.started()) {
117 sprite->set_fps(64 - 15 * flat_timer.get_timegone());
119 if (flat_timer.check()) {
124 case STATE_KICKED_DELAY:
125 if (kicked_delay_timer.check()) {
126 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
127 physic.set_velocity_y(KICKSPEED_Y);
128 state = STATE_KICKED;
133 physic.set_velocity_x(physic.get_velocity_x() * pow(0.99, elapsed_time/0.02));
134 if (fabsf(physic.get_velocity_x()) < WALKSPEED) be_normal();
138 BadGuy::active_update(elapsed_time);
142 Snail::collision_solid(const CollisionHit& hit)
144 update_on_ground_flag(hit);
146 if(hit.top || hit.bottom) { // floor or roof
147 physic.set_velocity_y(0);
152 case STATE_KICKED_DELAY:
164 if( (dir == LEFT && hit.left) || ( dir == RIGHT && hit.right) ){
165 dir = dir == LEFT ? RIGHT : LEFT;
166 sprite->set_action(dir == LEFT ? "left" : "right");
167 physic.set_velocity_x(-physic.get_velocity_x());
172 case STATE_KICKED_DELAY:
173 physic.set_velocity_x(0);
177 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
180 // TODO move this into BonusBlock code
181 // open bonusblocks, crash bricks
182 BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
184 bonusblock->try_open();
186 Brick* brick = dynamic_cast<Brick*> (&object);
191 if( ( dir == LEFT && hit.left ) || ( dir == RIGHT && hit.right) ){
192 dir = (dir == LEFT) ? RIGHT : LEFT;
193 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
195 physic.set_velocity_x(-physic.get_velocity_x()*0.75);
196 if (fabsf(physic.get_velocity_x()) < WALKSPEED) be_normal();
205 Snail::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
209 // printf("Snail <-> Badguy %s %s %s %s %s\n", hit.left?"left":"", hit.right?"right":"", hit.top?"top":"", hit.bottom?"bottom":"", hit.crush?"crush":"");
210 if( hit.left && dir == LEFT ){
212 sprite->set_action( "right" );
213 physic.set_velocity_x(-physic.get_velocity_x());
214 } else if( hit.right && dir == RIGHT ){
216 sprite->set_action( "left" );
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")