* Make it compile again
[supertux.git] / src / object / block.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 "object/block.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "badguy/badguy.hpp"
21 #include "object/coin.hpp"
22 #include "object/flower.hpp"
23 #include "object/growup.hpp"
24 #include "object/player.hpp"
25 #include "object/portable.hpp"
26 #include "supertux/constants.hpp"
27
28 static const float BOUNCY_BRICK_MAX_OFFSET = 8;
29 static const float BOUNCY_BRICK_SPEED = 90;
30 static const float EPSILON = .0001f;
31 static const float BUMP_ROTATION_ANGLE = 10;
32
33 Block::Block(std::auto_ptr<Sprite> newsprite) :
34   sprite(newsprite), 
35   bouncing(false), 
36   breaking(false), 
37   bounce_dir(0), 
38   bounce_offset(0), 
39   original_y(-1)
40 {
41   bbox.set_size(32, 32.1f);
42   set_group(COLGROUP_STATIC);
43   sound_manager->preload("sounds/upgrade.wav");
44   sound_manager->preload("sounds/brick.wav");
45 }
46
47 Block::~Block()
48 {
49 }
50
51 HitResponse
52 Block::collision(GameObject& other, const CollisionHit& )
53 {
54   Player* player = dynamic_cast<Player*> (&other);
55   if(player) {
56     if(player->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
57       hit(*player);
58     }
59   }
60
61   // only interact with other objects if...
62   //   1) we are bouncing
63   //   2) the object is not portable (either never or not currently)
64   //   3) the object is being hit from below (baguys don't get killed for activating boxes)
65   Portable* portable = dynamic_cast<Portable*> (&other);
66   MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
67   bool is_portable = ((portable != 0) && portable->is_portable());
68   bool hit_mo_from_below = ((moving_object == 0) || (moving_object->get_bbox().get_bottom() < (get_bbox().get_top() + SHIFT_DELTA)));
69   if(bouncing && !is_portable && hit_mo_from_below) {
70
71     // Badguys get killed
72     BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
73     if(badguy) {
74       badguy->kill_fall();
75     }
76
77     // Coins get collected
78     Coin* coin = dynamic_cast<Coin*> (&other);
79     if(coin) {
80       coin->collect();
81     }
82     
83     //Eggs get jumped
84     GrowUp* growup = dynamic_cast<GrowUp*> (&other);
85     if(growup) {
86       growup->do_jump();
87     }
88
89   }
90
91   return FORCE_MOVE;
92 }
93
94 void
95 Block::update(float elapsed_time)
96 {
97   if(!bouncing)
98     return;
99
100   float offset = original_y - get_pos().y;
101   if(offset > BOUNCY_BRICK_MAX_OFFSET) {
102     bounce_dir = BOUNCY_BRICK_SPEED;
103     movement = Vector(0, bounce_dir * elapsed_time);
104     if(breaking){
105       break_me();
106     }
107   } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
108     movement = Vector(0, offset);
109     bounce_dir = 0;
110     bouncing = false;
111     sprite->set_angle(0);
112   } else {
113     movement = Vector(0, bounce_dir * elapsed_time);
114   }
115 }
116
117 void
118 Block::draw(DrawingContext& context)
119 {
120   sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
121 }
122
123 void
124 Block::start_bounce(GameObject* hitter)
125 {
126   if(original_y == -1){
127     original_y = bbox.p1.y;
128   }
129   bouncing = true;
130   bounce_dir = -BOUNCY_BRICK_SPEED;
131   bounce_offset = 0;
132
133   MovingObject* hitter_mo = dynamic_cast<MovingObject*>(hitter);
134   if (hitter_mo) {
135     float center_of_hitter = hitter_mo->get_bbox().get_middle().x;
136     float offset = (get_bbox().get_middle().x - center_of_hitter)*2 / get_bbox().get_width();
137     sprite->set_angle(BUMP_ROTATION_ANGLE*offset);
138   }
139 }
140
141 void
142 Block::start_break(GameObject* hitter)
143 {
144   start_bounce(hitter);
145   breaking = true;
146 }
147
148 /* EOF */