0c293c7635edd36530f35992fda077f4d8d6a136
[supertux.git] / src / object / infoblock.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "infoblock.hpp"
23
24 #include "game_session.hpp"
25 #include "resources.hpp"
26 #include "sprite/sprite_manager.hpp"
27 #include "object_factory.hpp"
28 #include "lisp/lisp.hpp"
29 #include "sector.hpp"
30 #include "log.hpp"
31 #include "object/player.hpp"
32 #include "main.hpp"
33 #include "video/drawing_context.hpp"
34 #include "object/camera.hpp"
35
36 namespace {
37   const float SCROLL_DELAY = 0.5;
38   const float SCROLL_DISTANCE = 16;
39   const float WIDTH = 400;
40   const float HEIGHT = 200;
41 }
42
43 InfoBlock::InfoBlock(const lisp::Lisp& lisp)
44   : Block(sprite_manager->create("images/objects/bonus_block/infoblock.sprite")), shown_pct(0), dest_pct(0)
45 {
46   Vector pos;
47   lisp.get("x", pos.x);
48   lisp.get("y", pos.y);
49   bbox.set_pos(pos);
50
51   if(!lisp.get("message", message)) {
52     log_warning << "No message in InfoBlock" << std::endl;
53   }
54   //stopped = false;
55   //ringing = new AmbientSound(get_pos(), 0.5, 300, 1, "sounds/phone.wav");
56   //Sector::current()->add_object(ringing);
57
58   // Split text string lines into a vector
59   lines = InfoBoxLine::split(message, 400);
60   lines_height = 0;
61   for(size_t i = 0; i < lines.size(); ++i) lines_height+=lines[i]->get_height();
62 }
63
64 InfoBlock::~InfoBlock()
65 {
66   for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) {
67     delete *i;
68   }
69 }
70
71 void
72 InfoBlock::hit(Player& player)
73 {
74   start_bounce(&player);
75
76   //if (!stopped) {
77   //  ringing->remove_me();
78   //  stopped = true;
79   //}
80
81   if (dest_pct != 1) {
82
83     // first hide all other InfoBlocks' messages in same sector
84     Sector* parent = Sector::current();
85     if (!parent) return;
86     for (Sector::GameObjects::iterator i = parent->gameobjects.begin(); i != parent->gameobjects.end(); i++) {
87       InfoBlock* block = dynamic_cast<InfoBlock*>(*i);
88       if (!block) continue;
89       if (block != this) block->hide_message();
90     }
91
92     // show our message
93     show_message();
94
95   } else {
96     hide_message();
97   }
98 }
99
100 Player*
101 InfoBlock::get_nearest_player()
102 {
103   // FIXME: does not really return nearest player
104
105   std::vector<Player*> players = Sector::current()->get_players();
106   for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
107     Player* player = *playerIter;
108     return player;
109   }
110
111   return 0;
112 }
113
114 void
115 InfoBlock::update(float delta)
116 {
117   Block::update(delta);
118
119   if (delta == 0) return;
120
121   // hide message if player is too far away or above infoblock
122   if (dest_pct > 0) {
123     Player* player = get_nearest_player();
124     if (player) {
125       Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
126       Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
127       Vector dist = (p2 - p1);
128       float d = dist.norm();
129       if (d > 128 || dist.y < 0) dest_pct = 0;
130     }
131   }
132
133   // handle soft fade-in and fade-out
134   if (shown_pct != dest_pct) {
135     if (dest_pct > shown_pct) shown_pct = std::min(shown_pct + 2*delta, dest_pct);
136     if (dest_pct < shown_pct) shown_pct = std::max(shown_pct - 2*delta, dest_pct);
137   }
138 }
139
140 void
141 InfoBlock::draw(DrawingContext& context)
142 {
143   Block::draw(context);
144
145   if (shown_pct <= 0) return;
146
147   context.push_transform();
148   //context.set_translation(Vector(0, 0));
149   context.set_alpha(shown_pct);
150
151   //float x1 = SCREEN_WIDTH/2-200;
152   //float y1 = SCREEN_HEIGHT/2-200;
153   float border = 8;
154   float width = 400; // this is the text width only
155   float height = lines_height; // this is the text height only
156   float x1 = (get_bbox().p1.x + get_bbox().p2.x)/2 - width/2;
157   float x2 = (get_bbox().p1.x + get_bbox().p2.x)/2 + width/2;
158   float y1 = original_y - height;
159
160   if(x1 < 0) {
161     x1 = 0;
162     x2 = width;
163   }
164
165   if(x2 > Sector::current()->get_width()) {
166     x2 = Sector::current()->get_width();
167     x1 = x2 - width;
168   }
169
170   // lines_height includes one ITEMS_SPACE too much, so the bottom border is reduced by 4px
171   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);
172
173   float y = y1;
174   for(size_t i = 0; i < lines.size(); ++i) {
175     if(y >= y1 + height) {
176       //log_warning << "Too many lines of text in InfoBlock" << std::endl;
177       //dest_pct = 0;
178       //shown_pct = 0;
179       break;
180     }
181
182     lines[i]->draw(context, Rect(x1, y, x2, y), LAYER_GUI-50+1);
183     y += lines[i]->get_height();
184   }
185
186   context.pop_transform();
187 }
188
189 void
190 InfoBlock::show_message()
191 {
192   dest_pct = 1;
193 }
194
195 void
196 InfoBlock::hide_message()
197 {
198   dest_pct = 0;
199 }
200
201 IMPLEMENT_FACTORY(InfoBlock, "infoblock")