Scroll nonblocking infoblock by moving half a block away.
[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
32 namespace {
33   const float SCROLL_DELAY = 0.5;
34   const float SCROLL_DISTANCE = 16;
35 }
36
37 InfoBlock::InfoBlock(const lisp::Lisp& lisp)
38   : Block(sprite_manager->create("images/objects/bonus_block/infoblock.sprite")), shown_pct(0), dest_pct(0), slowdown_scroll(0)
39 {
40   Vector pos;
41   lisp.get("x", pos.x);
42   lisp.get("y", pos.y);
43   bbox.set_pos(pos);
44
45   if(!lisp.get("message", message)) {
46     log_warning << "No message in InfoBlock" << std::endl;
47   }
48   //stopped = false;
49   //ringing = new AmbientSound(get_pos(), 0.5, 300, 1, "sounds/phone.wav");
50   //Sector::current()->add_object(ringing);
51   infoBox.reset(new InfoBox(message));
52 }
53
54 InfoBlock::~InfoBlock()
55 {
56 }
57
58 void
59 InfoBlock::hit(Player& )
60 {
61   start_bounce();
62
63   //if (!stopped) {
64   //  ringing->remove_me();
65   //  stopped = true;
66   //}
67
68   if (dest_pct != 1) {
69
70     // first hide all other InfoBlocks' messages in same sector
71     Sector* parent = Sector::current();
72     if (!parent) return;
73     for (Sector::GameObjects::iterator i = parent->gameobjects.begin(); i != parent->gameobjects.end(); i++) {
74       InfoBlock* block = dynamic_cast<InfoBlock*>(*i);
75       if (!block) continue;
76       if (block != this) block->hide_message();
77     }
78
79     // show our message
80     show_message();
81
82   } else {
83     hide_message();
84   }
85 }
86
87 Player*
88 InfoBlock::get_nearest_player()
89 {
90   // FIXME: does not really return nearest player
91
92   std::vector<Player*> players = Sector::current()->get_players();
93   for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
94     Player* player = *playerIter;
95     return player;
96   }
97
98   return 0;
99 }
100
101 void
102 InfoBlock::update(float delta)
103 {
104   if (delta == 0) return;
105
106   // hide message if player is too far away or above infoblock
107   if (dest_pct > 0) {
108     Player* player = get_nearest_player();
109     if (player) {
110       Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
111       Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
112       Vector dist = (p2 - p1);
113       float d = dist.norm();
114       if (d > 128 || dist.y < 0) dest_pct = 0;
115       slowdown_scroll += delta; 
116       if ( slowdown_scroll > SCROLL_DELAY ) {
117         slowdown_scroll = 0; 
118         if (dist.x > SCROLL_DISTANCE) {
119           infoBox->scrolldown();
120         }
121         if( dist.x < -SCROLL_DISTANCE) {
122           infoBox->scrollup();
123         }
124       }
125     }
126   }
127
128   // handle soft fade-in and fade-out
129   if (shown_pct != dest_pct) {
130     if (dest_pct > shown_pct) shown_pct = std::min(shown_pct + 2*delta, dest_pct);
131     if (dest_pct < shown_pct) shown_pct = std::max(shown_pct - 2*delta, dest_pct);
132   }
133 }
134
135 void
136 InfoBlock::draw(DrawingContext& context)
137 {
138   Block::draw(context);
139
140   if (shown_pct > 0) {
141     context.push_transform();
142     context.set_translation(Vector(0, 0));
143     context.set_alpha(shown_pct);
144     infoBox->draw(context);
145     context.pop_transform();
146   }
147 }
148
149 void
150 InfoBlock::show_message()
151 {
152   dest_pct = 1;
153 }
154
155 void
156 InfoBlock::hide_message()
157 {
158   dest_pct = 0;
159 }
160
161 IMPLEMENT_FACTORY(InfoBlock, "infoblock")