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 set_direction = false;
36 sound_manager->preload("sounds/iceblock_bump.wav");
37 sound_manager->preload("sounds/stomp.wav");
38 sound_manager->preload("sounds/kick.wav");
41 Snail::Snail(const Vector& pos, Direction d)
42 : BadGuy(pos, "images/creatures/snail/snail.sprite"), state(STATE_NORMAL), squishcount(0)
45 initial_direction = d;
46 sound_manager->preload("sounds/iceblock_bump.wav");
47 sound_manager->preload("sounds/stomp.wav");
48 sound_manager->preload("sounds/kick.wav");
52 Snail::write(lisp::Writer& writer)
54 writer.start_list("snail");
56 writer.write_float("x", start_position.x);
57 writer.write_float("y", start_position.y);
59 writer.end_list("snail");
65 if (set_direction) {dir = initial_direction;}
74 sprite->set_action(dir == LEFT ? "left" : "right");
76 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
83 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
86 physic.set_velocity_x(0);
87 physic.set_velocity_y(0);
95 state = STATE_KICKED_DELAY;
96 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
99 physic.set_velocity_x(0);
100 physic.set_velocity_y(0);
102 // start a timer to delay addition of upward movement until we are (hopefully) out from under the player
103 kicked_delay_timer.start(0.05);
108 Snail::active_update(float elapsed_time)
113 if (might_fall(601)) {
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(GameObject& object, const CollisionHit& hit)
149 if(fabsf(hit.normal.y) > .5) { // floor or roof
150 physic.set_velocity_y(0);
155 case STATE_KICKED_DELAY:
167 dir = dir == LEFT ? RIGHT : LEFT;
168 sprite->set_action(dir == LEFT ? "left" : "right");
169 physic.set_velocity_x(-physic.get_velocity_x());
173 case STATE_KICKED_DELAY:
174 physic.set_velocity_x(0);
178 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
180 // open bonusblocks, crash bricks
181 BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
183 bonusblock->try_open();
185 Brick* brick = dynamic_cast<Brick*> (&object);
190 dir = (dir == LEFT) ? RIGHT : LEFT;
191 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
193 physic.set_velocity_x(-physic.get_velocity_x()*0.75);
194 if (fabsf(physic.get_velocity_x()) < WALKSPEED) be_normal();
204 Snail::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
208 if(fabsf(hit.normal.x) > .5) {
209 dir = (dir == LEFT) ? RIGHT : LEFT;
210 sprite->set_action(dir == LEFT ? "left" : "right");
211 physic.set_velocity_x(-physic.get_velocity_x());
215 case STATE_KICKED_DELAY:
228 Snail::collision_squished(Player& player)
235 if(squishcount >= MAXSQUISHES) {
240 sound_manager->play("sounds/stomp.wav", get_pos());
245 sound_manager->play("sounds/kick.wav", get_pos());
247 if(player.get_pos().x < get_pos().x) {
255 case STATE_KICKED_DELAY:
260 player.bounce(*this);
264 IMPLEMENT_FACTORY(Snail, "snail")