1 // $Id: snowsnail.cpp 2738 2005-10-08 08:53:46Z wansti $
4 // Copyright (C) 2005 Matthias Braun <matze@braunis.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
23 #include "snowsnail.hpp"
24 #include "object/block.hpp"
26 static const float WALKSPEED = 80;
27 static const float KICKSPEED = 500;
28 static const int MAXSQUISHES = 10;
30 SnowSnail::SnowSnail(const lisp::Lisp& reader)
31 : ice_state(ICESTATE_NORMAL), squishcount(0)
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;
40 SnowSnail::SnowSnail(float pos_x, float pos_y, Direction d)
41 : ice_state(ICESTATE_NORMAL), squishcount(0)
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");
48 initial_direction = d;
52 SnowSnail::write(lisp::Writer& writer)
54 writer.start_list("mriceblock");
56 writer.write_float("x", start_position.x);
57 writer.write_float("y", start_position.y);
59 writer.end_list("snowsnail");
65 if (set_direction) {dir = initial_direction;}
66 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
67 sprite->set_action(dir == LEFT ? "left" : "right");
71 SnowSnail::active_update(float elapsed_time)
73 if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
74 ice_state = ICESTATE_NORMAL;
75 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
76 sprite->set_action(dir == LEFT ? "left" : "right");
78 BadGuy::active_update(elapsed_time);
82 SnowSnail::collision_solid(GameObject& object, const CollisionHit& hit)
84 if(fabsf(hit.normal.y) > .5) { // floor or roof
85 physic.set_velocity_y(0);
91 dir = dir == LEFT ? RIGHT : LEFT;
92 sprite->set_action(dir == LEFT ? "left" : "right");
93 physic.set_velocity_x(-physic.get_velocity_x());
95 case ICESTATE_KICKED: {
96 BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
98 bonusblock->try_open();
100 Brick* brick = dynamic_cast<Brick*> (&object);
105 dir = dir == LEFT ? RIGHT : LEFT;
106 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
107 physic.set_velocity_x(-physic.get_velocity_x());
108 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
112 physic.set_velocity_x(0);
120 SnowSnail::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
123 case ICESTATE_NORMAL:
124 if(fabsf(hit.normal.x) > .8) {
125 dir = dir == LEFT ? RIGHT : LEFT;
126 sprite->set_action(dir == LEFT ? "left" : "right");
127 physic.set_velocity_x(-physic.get_velocity_x());
132 case ICESTATE_KICKED:
143 SnowSnail::collision_squished(Player& player)
146 case ICESTATE_KICKED:
147 case ICESTATE_NORMAL:
149 if(squishcount >= MAXSQUISHES) {
155 sound_manager->play("sounds/stomp.wav", get_pos());
156 physic.set_velocity_x(0);
157 physic.set_velocity_y(0);
159 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
161 ice_state = ICESTATE_FLAT;
165 sound_manager->play("sounds/kick.wav", get_pos());
167 if(player.get_pos().x < get_pos().x) {
172 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
173 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
174 ice_state = ICESTATE_KICKED;
178 player.bounce(*this);
182 IMPLEMENT_FACTORY(SnowSnail, "snowsnail")