Changed ObjectFactory code so that it works properly when building SuperTux as library
[supertux.git] / src / badguy / mriceblock.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/mriceblock.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "object/player.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23
24 namespace {
25 const float KICKSPEED = 500;
26 const int MAXSQUISHES = 10;
27 const float NOKICK_TIME = 0.1f;
28 }
29
30 MrIceBlock::MrIceBlock(const Reader& reader) :
31   WalkingBadguy(reader, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), 
32   ice_state(ICESTATE_NORMAL), 
33   nokick_timer(),
34   flat_timer(),
35   squishcount(0)
36 {
37   walk_speed = 80;
38   max_drop_height = 600;
39   sound_manager->preload("sounds/iceblock_bump.wav");
40   sound_manager->preload("sounds/stomp.wav");
41   sound_manager->preload("sounds/kick.wav");
42 }
43
44 MrIceBlock::MrIceBlock(const Vector& pos, Direction d) :
45   WalkingBadguy(pos, d, "images/creatures/mr_iceblock/mr_iceblock.sprite", "left", "right"), 
46   ice_state(ICESTATE_NORMAL), 
47   nokick_timer(),
48   flat_timer(),
49   squishcount(0)
50 {
51   walk_speed = 80;
52   max_drop_height = 600;
53   sound_manager->preload("sounds/iceblock_bump.wav");
54   sound_manager->preload("sounds/stomp.wav");
55   sound_manager->preload("sounds/kick.wav");
56 }
57
58 void
59 MrIceBlock::initialize()
60 {
61   WalkingBadguy::initialize();
62   set_state(ICESTATE_NORMAL);
63 }
64
65 void
66 MrIceBlock::active_update(float elapsed_time)
67 {
68   if(ice_state == ICESTATE_GRABBED)
69     return;
70
71   if(ice_state == ICESTATE_FLAT && flat_timer.check()) {
72     set_state(ICESTATE_NORMAL);
73   }
74
75   if (ice_state == ICESTATE_NORMAL)
76   {
77     WalkingBadguy::active_update(elapsed_time);
78     return;
79   }
80
81   BadGuy::active_update(elapsed_time);
82 }
83
84 bool
85 MrIceBlock::can_break(){
86   return ice_state == ICESTATE_KICKED;
87 }
88
89 void
90 MrIceBlock::collision_solid(const CollisionHit& hit)
91 {
92   update_on_ground_flag(hit);
93
94   if(hit.top || hit.bottom) { // floor or roof
95     physic.set_velocity_y(0);
96     return;
97   }
98
99   // hit left or right
100   switch(ice_state) {
101     case ICESTATE_NORMAL:
102       WalkingBadguy::collision_solid(hit);
103       break;
104     case ICESTATE_KICKED: {
105       if(hit.right && dir == RIGHT) {
106         dir = LEFT;
107         sound_manager->play("sounds/iceblock_bump.wav", get_pos());
108         physic.set_velocity_x(-KICKSPEED);
109       } else if(hit.left && dir == LEFT) {
110         dir = RIGHT;
111         sound_manager->play("sounds/iceblock_bump.wav", get_pos());
112         physic.set_velocity_x(KICKSPEED);
113       }
114       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
115       break;
116     }
117     case ICESTATE_FLAT:
118       physic.set_velocity_x(0);
119       break;
120     case ICESTATE_GRABBED:
121       break;
122   }
123 }
124
125 HitResponse
126 MrIceBlock::collision(GameObject& object, const CollisionHit& hit)
127 {
128   if(ice_state == ICESTATE_GRABBED)
129     return FORCE_MOVE;
130
131   return BadGuy::collision(object, hit);
132 }
133
134 HitResponse
135 MrIceBlock::collision_player(Player& player, const CollisionHit& hit)
136 {
137   if(dir == UP) {
138     return FORCE_MOVE;
139   }
140
141   // handle kicks from left or right side
142   if(ice_state == ICESTATE_FLAT && get_state() == STATE_ACTIVE) {
143     if(hit.left) {
144       dir = RIGHT;
145       player.kick();
146       set_state(ICESTATE_KICKED);
147       return FORCE_MOVE;
148     } else if(hit.right) {
149       dir = LEFT;
150       player.kick();
151       set_state(ICESTATE_KICKED);
152       return FORCE_MOVE;
153     }
154   }
155
156   return BadGuy::collision_player(player, hit);
157 }
158
159 HitResponse
160 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
161 {
162   switch(ice_state) {
163     case ICESTATE_NORMAL:
164       return WalkingBadguy::collision_badguy(badguy, hit);
165     case ICESTATE_FLAT:
166       return FORCE_MOVE;
167     case ICESTATE_KICKED:
168       badguy.kill_fall();
169       return FORCE_MOVE;
170     default:
171       assert(false);
172   }
173
174   return ABORT_MOVE;
175 }
176
177 bool
178 MrIceBlock::collision_squished(GameObject& object)
179 {
180   switch(ice_state) {
181     case ICESTATE_KICKED:
182     {
183       BadGuy* badguy = dynamic_cast<BadGuy*>(&object);
184       if (badguy) {
185         badguy->kill_fall();
186         break;
187       }
188     }
189
190     // fall through
191     case ICESTATE_NORMAL:
192     {
193       Player* player = dynamic_cast<Player*>(&object);
194       squishcount++;
195       if ((squishcount >= MAXSQUISHES) || (player && player->does_buttjump)) {
196         kill_fall();
197         return true;
198       }
199     }
200
201     set_state(ICESTATE_FLAT);
202     nokick_timer.start(NOKICK_TIME);
203     break;
204     case ICESTATE_FLAT:
205     {
206       MovingObject* movingobject = dynamic_cast<MovingObject*>(&object);
207       if (movingobject && (movingobject->get_pos().x < get_pos().x)) {
208         dir = RIGHT;
209       } else {
210         dir = LEFT;
211       }
212     }
213     if (nokick_timer.check()) set_state(ICESTATE_KICKED);
214     break;
215     case ICESTATE_GRABBED:
216       assert(false);
217       break;
218   }
219
220   Player* player = dynamic_cast<Player*>(&object);
221   if (player) player->bounce(*this);
222   return true;
223 }
224
225 void
226 MrIceBlock::set_state(IceState state)
227 {
228   if(ice_state == state)
229     return;
230
231   switch(state) {
232     case ICESTATE_NORMAL:
233       WalkingBadguy::initialize();
234       break;
235     case ICESTATE_FLAT:
236       if(dir == UP) {
237         physic.set_velocity_y(-KICKSPEED);
238         bbox.set_size(34, 31.8f);
239       } else {
240         sound_manager->play("sounds/stomp.wav", get_pos());
241         physic.set_velocity_x(0);
242         physic.set_velocity_y(0);
243
244         sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
245       }
246       flat_timer.start(4);
247       break;
248     case ICESTATE_KICKED:
249       sound_manager->play("sounds/kick.wav", get_pos());
250
251       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
252       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
253       // we should slide above 1 block holes now...
254       bbox.set_size(34, 31.8f);
255       break;
256     case ICESTATE_GRABBED:
257       flat_timer.stop();
258       break;
259     default:
260       assert(false);
261   }
262   ice_state = state;
263 }
264
265 void
266 MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir)
267 {
268   movement = pos - get_pos();
269   this->dir = dir;
270   sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
271   set_state(ICESTATE_GRABBED);
272   set_colgroup_active(COLGROUP_DISABLED);
273 }
274
275 void
276 MrIceBlock::ungrab(MovingObject& , Direction dir)
277 {
278   this->dir = dir;
279   set_state(dir == UP ? ICESTATE_FLAT : ICESTATE_KICKED);
280   set_colgroup_active(COLGROUP_MOVING);
281 }
282
283 bool
284 MrIceBlock::is_portable() const
285 {
286   return ice_state == ICESTATE_FLAT;
287 }
288
289 /* EOF */