4 // Copyright (C) 2006 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 02111-1307, USA.
22 #include "snowsnail.hpp"
23 #include "object/block.hpp"
25 static const float WALKSPEED = 80;
26 static const float KICKSPEED = 500;
27 static const int MAXSQUISHES = 10;
29 SnowSnail::SnowSnail(const lisp::Lisp& reader)
30 : ice_state(ICESTATE_NORMAL), squishcount(0)
32 reader.get("x", start_position.x);
33 reader.get("y", start_position.y);
34 stay_on_platform = false;
35 reader.get("stay-on-platform", stay_on_platform);
36 bbox.set_size(31.8, 31.8);
37 sprite = sprite_manager->create("images/creatures/snowsnail/snowsnail.sprite");
38 set_direction = false;
41 SnowSnail::SnowSnail(float pos_x, float pos_y, Direction d, bool stay_on_plat = false)
42 : ice_state(ICESTATE_NORMAL), squishcount(0)
44 start_position.x = pos_x;
45 start_position.y = pos_y;
46 stay_on_platform = stay_on_plat;
47 bbox.set_size(31.8, 31.8);
48 sprite = sprite_manager->create("images/creatures/snowsnail/snowsnail.sprite");
50 initial_direction = d;
54 SnowSnail::write(lisp::Writer& writer)
56 writer.start_list("mriceblock");
58 writer.write_float("x", start_position.x);
59 writer.write_float("y", start_position.y);
60 if (stay_on_platform) writer.write_bool("stay-on-platform", true);
62 writer.end_list("snowsnail");
68 if (set_direction) {dir = initial_direction;}
69 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
70 sprite->set_action(dir == LEFT ? "left" : "right");
74 SnowSnail::active_update(float elapsed_time)
76 if((ice_state != ICESTATE_KICKED) && flat_timer.started()) {
77 sprite->set_fps(64 - 15 * flat_timer.get_timegone());
79 if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
80 ice_state = ICESTATE_NORMAL;
81 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
82 sprite->set_action(dir == LEFT ? "left" : "right");
84 if(ice_state == ICESTATE_NORMAL && stay_on_platform
85 && may_fall_off_platform())
87 dir = (dir == LEFT ? RIGHT : LEFT);
88 sprite->set_action(dir == LEFT ? "left" : "right");
89 physic.set_velocity_x(-physic.get_velocity_x());
91 BadGuy::active_update(elapsed_time);
95 SnowSnail::collision_solid(GameObject& object, const CollisionHit& hit)
97 if(fabsf(hit.normal.y) > .5) { // floor or roof
98 physic.set_velocity_y(0);
103 case ICESTATE_NORMAL:
104 dir = dir == LEFT ? RIGHT : LEFT;
105 sprite->set_action(dir == LEFT ? "left" : "right");
106 physic.set_velocity_x(-physic.get_velocity_x());
108 case ICESTATE_KICKED: {
109 BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
111 bonusblock->try_open();
113 Brick* brick = dynamic_cast<Brick*> (&object);
118 dir = dir == LEFT ? RIGHT : LEFT;
119 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
121 physic.set_velocity_x(-physic.get_velocity_x());
122 sound_manager->play("sounds/iceblock_bump.wav", get_pos());
126 physic.set_velocity_x(0);
134 SnowSnail::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
137 case ICESTATE_NORMAL:
138 if(fabsf(hit.normal.x) > .8) {
139 dir = dir == LEFT ? RIGHT : LEFT;
140 sprite->set_action(dir == LEFT ? "left" : "right");
141 physic.set_velocity_x(-physic.get_velocity_x());
146 case ICESTATE_KICKED:
157 SnowSnail::collision_squished(Player& player)
160 case ICESTATE_KICKED:
161 case ICESTATE_NORMAL:
163 if(squishcount >= MAXSQUISHES) {
169 sound_manager->play("sounds/stomp.wav", get_pos());
170 physic.set_velocity_x(0);
171 physic.set_velocity_y(0);
173 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
176 ice_state = ICESTATE_FLAT;
180 sound_manager->play("sounds/kick.wav", get_pos());
182 if(player.get_pos().x < get_pos().x) {
187 physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
188 sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
190 ice_state = ICESTATE_KICKED;
194 player.bounce(*this);
198 IMPLEMENT_FACTORY(SnowSnail, "snowsnail")