Fix syntax error reported by cppcheck
[supertux.git] / src / object / infoblock.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/infoblock.hpp"
18
19 #include "object/player.hpp"
20 #include "sprite/sprite_manager.hpp"
21 #include "supertux/object_factory.hpp"
22 #include "supertux/sector.hpp"
23 #include "supertux/info_box_line.hpp"
24 #include "util/reader.hpp"
25 #include "video/drawing_context.hpp"
26 #include "sprite/sprite.hpp"
27
28 InfoBlock::InfoBlock(const Reader& lisp) :
29   Block(SpriteManager::current()->create("images/objects/bonus_block/infoblock.sprite")),
30   message(),
31   shown_pct(0),
32   dest_pct(0),
33   lines(),
34   lines_height()
35 {
36   Vector pos;
37   lisp.get("x", pos.x);
38   lisp.get("y", pos.y);
39   bbox.set_pos(pos);
40
41   if(!lisp.get("message", message)) {
42     log_warning << "No message in InfoBlock" << std::endl;
43   }
44   //stopped = false;
45   //ringing = new AmbientSound(get_pos(), 0.5, 300, 1, "sounds/phone.wav");
46   //Sector::current()->add_object(ringing);
47
48   // Split text string lines into a vector
49   lines = InfoBoxLine::split(message, 400);
50   lines_height = 0;
51   for(size_t i = 0; i < lines.size(); ++i) lines_height+=lines[i]->get_height();
52 }
53
54 InfoBlock::~InfoBlock()
55 {
56   for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); ++i) {
57     delete *i;
58   }
59 }
60
61 void
62 InfoBlock::hit(Player& player)
63 {
64   start_bounce(&player);
65
66   //if (!stopped) {
67   //  ringing->remove_me();
68   //  stopped = true;
69   //}
70
71   if (dest_pct != 1) {
72
73     // first hide all other InfoBlocks' messages in same sector
74     Sector* parent = Sector::current();
75     if (!parent) return;
76     for (Sector::GameObjects::iterator i = parent->gameobjects.begin(); i != parent->gameobjects.end(); ++i) {
77       InfoBlock* block = dynamic_cast<InfoBlock*>(i->get());
78       if (!block) continue;
79       if (block != this) block->hide_message();
80     }
81
82     // show our message
83     show_message();
84
85   } else {
86     hide_message();
87   }
88 }
89
90 HitResponse
91 InfoBlock::collision(GameObject& other, const CollisionHit& hit_)
92 {
93   Player* player = dynamic_cast<Player*> (&other);
94   if (player)
95   {
96     if (player->does_buttjump)
97       InfoBlock::hit(*player);
98   }
99   return Block::collision(other, hit_);
100 }
101
102 Player*
103 InfoBlock::get_nearest_player()
104 {
105   // FIXME: does not really return nearest player
106   //  for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
107   //    Player* player = *playerIter;
108   //    return player;
109   //  }
110
111   std::vector<Player*> players = Sector::current()->get_players();
112   if(players.size() > 0)
113   {
114     // Return first player
115     return players[0];
116   }
117
118   return 0;
119 }
120
121 void
122 InfoBlock::update(float delta)
123 {
124   Block::update(delta);
125
126   if (delta == 0) return;
127
128   // hide message if player is too far away
129   if (dest_pct > 0) {
130     Player* player = get_nearest_player();
131     if (player) {
132       Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
133       Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
134       Vector dist = (p2 - p1);
135       float d = dist.norm();
136       if (d > 128) dest_pct = 0;
137     }
138   }
139
140   // handle soft fade-in and fade-out
141   if (shown_pct != dest_pct) {
142     if (dest_pct > shown_pct) shown_pct = std::min(shown_pct + 2*delta, dest_pct);
143     if (dest_pct < shown_pct) shown_pct = std::max(shown_pct - 2*delta, dest_pct);
144   }
145 }
146
147 void
148 InfoBlock::draw(DrawingContext& context)
149 {
150   Block::draw(context);
151
152   if (shown_pct <= 0) return;
153
154   context.push_transform();
155   //context.set_translation(Vector(0, 0));
156   context.set_alpha(shown_pct);
157
158   //float x1 = SCREEN_WIDTH/2-200;
159   //float y1 = SCREEN_HEIGHT/2-200;
160   float border = 8;
161   float width = 400; // this is the text width only
162   float height = lines_height; // this is the text height only
163   float x1 = (get_bbox().p1.x + get_bbox().p2.x)/2 - width/2;
164   float x2 = (get_bbox().p1.x + get_bbox().p2.x)/2 + width/2;
165   float y1 = original_y - height;
166
167   if(x1 < 0) {
168     x1 = 0;
169     x2 = width;
170   }
171
172   if(x2 > Sector::current()->get_width()) {
173     x2 = Sector::current()->get_width();
174     x1 = x2 - width;
175   }
176
177   // lines_height includes one ITEMS_SPACE too much, so the bottom border is reduced by 4px
178   context.draw_filled_rect(Vector(x1-border, y1-border), Vector(width+2*border, height+2*border-4), Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-50);
179
180   float y = y1;
181   for(size_t i = 0; i < lines.size(); ++i) {
182     if(y >= y1 + height) {
183       //log_warning << "Too many lines of text in InfoBlock" << std::endl;
184       //dest_pct = 0;
185       //shown_pct = 0;
186       break;
187     }
188
189     lines[i]->draw(context, Rectf(x1, y, x2, y), LAYER_GUI-50+1);
190     y += lines[i]->get_height();
191   }
192
193   context.pop_transform();
194 }
195
196 void
197 InfoBlock::show_message()
198 {
199   dest_pct = 1;
200 }
201
202 void
203 InfoBlock::hide_message()
204 {
205   dest_pct = 0;
206 }
207
208 /* EOF */