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