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