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