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