4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
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"
30 #include "object/player.hpp"
33 const float SCROLL_DELAY = 0.5;
34 const float SCROLL_DISTANCE = 16;
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)
45 if(!lisp.get("message", message)) {
46 log_warning << "No message in InfoBlock" << std::endl;
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));
54 InfoBlock::~InfoBlock()
59 InfoBlock::hit(Player& )
64 // ringing->remove_me();
70 // first hide all other InfoBlocks' messages in same sector
71 Sector* parent = Sector::current();
73 for (Sector::GameObjects::iterator i = parent->gameobjects.begin(); i != parent->gameobjects.end(); i++) {
74 InfoBlock* block = dynamic_cast<InfoBlock*>(*i);
76 if (block != this) block->hide_message();
88 InfoBlock::get_nearest_player()
90 // FIXME: does not really return nearest player
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;
102 InfoBlock::update(float delta)
104 if (delta == 0) return;
106 // hide message if player is too far away or above infoblock
108 Player* player = get_nearest_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 ) {
118 if (dist.x > SCROLL_DISTANCE) {
119 infoBox->scrolldown();
121 if( dist.x < -SCROLL_DISTANCE) {
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);
136 InfoBlock::draw(DrawingContext& context)
138 Block::draw(context);
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();
150 InfoBlock::show_message()
156 InfoBlock::hide_message()
161 IMPLEMENT_FACTORY(InfoBlock, "infoblock")