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