changed worldmap format a bit to be more consistent with level format
[supertux.git] / src / badguy / mriceblock.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 // 
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
19 //  02111-1307, USA.
20
21 #include <config.h>
22
23 #include "mriceblock.h"
24 #include "object/block.h"
25
26 static const float WALKSPEED = 80;
27 static const float KICKSPEED = 500;
28 static const int MAXSQUISHES = 10;
29
30 MrIceBlock::MrIceBlock(const lisp::Lisp& reader)
31   : ice_state(ICESTATE_NORMAL), squishcount(0)
32 {
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("mriceblock");
37   set_direction = false;
38 }
39
40 MrIceBlock::MrIceBlock(float pos_x, float pos_y, Direction d)
41   : ice_state(ICESTATE_NORMAL), squishcount(0)
42 {
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("mriceblock");
47   set_direction = true;
48   initial_direction = d;
49 }
50
51 void
52 MrIceBlock::write(lisp::Writer& writer)
53 {
54   writer.start_list("mriceblock");
55
56   writer.write_float("x", start_position.x);
57   writer.write_float("y", start_position.y);
58
59   writer.end_list("mriceblock");
60 }
61
62 void
63 MrIceBlock::activate()
64 {
65   if (set_direction) {dir = initial_direction;}
66   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
67   sprite->set_action(dir == LEFT ? "left" : "right");
68 }
69
70 void
71 MrIceBlock::active_update(float elapsed_time)
72 {
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");
77   }
78   BadGuy::active_update(elapsed_time);
79 }
80
81 HitResponse
82 MrIceBlock::collision_solid(GameObject& object, const CollisionHit& hit)
83 {
84   if(fabsf(hit.normal.y) > .5) { // floor or roof
85     physic.set_velocity_y(0);
86     return CONTINUE;
87   }
88   // hit left or right
89   switch(ice_state) {
90     case ICESTATE_NORMAL:
91       dir = dir == LEFT ? RIGHT : LEFT;
92       sprite->set_action(dir == LEFT ? "left" : "right");
93       physic.set_velocity_x(-physic.get_velocity_x());       
94       break;
95     case ICESTATE_KICKED: {
96       BonusBlock* bonusblock = dynamic_cast<BonusBlock*> (&object);
97       if(bonusblock) {
98         bonusblock->try_open();
99       }
100       Brick* brick = dynamic_cast<Brick*> (&object);
101       if(brick) {
102         brick->try_break();
103       }
104       
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_sound("ricochet", get_pos(),
109                                 Sector::current()->player->get_pos());
110       break;
111     }
112     case ICESTATE_FLAT:
113       physic.set_velocity_x(0);
114       break;
115   }
116
117   return CONTINUE;
118 }
119
120 HitResponse
121 MrIceBlock::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
122 {
123   switch(ice_state) {
124     case ICESTATE_NORMAL:
125       if(fabsf(hit.normal.x) > .8) {
126         dir = dir == LEFT ? RIGHT : LEFT;
127         sprite->set_action(dir == LEFT ? "left" : "right");
128         physic.set_velocity_x(-physic.get_velocity_x());               
129       }
130       return CONTINUE;
131     case ICESTATE_FLAT:
132       return FORCE_MOVE;
133     case ICESTATE_KICKED:
134       badguy.kill_fall();
135       return FORCE_MOVE;
136     default:
137       assert(false);
138   }
139
140   return ABORT_MOVE;
141 }
142
143 bool
144 MrIceBlock::collision_squished(Player& player)
145 {
146   switch(ice_state) {
147     case ICESTATE_KICKED:
148     case ICESTATE_NORMAL:
149       squishcount++;
150       if(squishcount >= MAXSQUISHES) {
151         kill_fall();
152         return true;
153       }
154
155       // flatten
156       sound_manager->play_sound("stomp", get_pos(), player.get_pos());
157       physic.set_velocity_x(0);
158       physic.set_velocity_y(0); 
159       
160       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
161       flat_timer.start(4);
162       ice_state = ICESTATE_FLAT;
163       break;
164     case ICESTATE_FLAT:
165       // kick
166       sound_manager->play_sound("kick", this, player.get_pos());
167
168       if(player.get_pos().x < get_pos().x) {
169         dir = RIGHT;
170       } else {
171         dir = LEFT;
172       }
173       physic.set_velocity_x(dir == LEFT ? -KICKSPEED : KICKSPEED);
174       sprite->set_action(dir == LEFT ? "flat-left" : "flat-right");
175       ice_state = ICESTATE_KICKED;
176       break;
177   }
178
179   player.bounce(*this);
180   return true;
181 }
182
183 IMPLEMENT_FACTORY(MrIceBlock, "mriceblock")