Possible fix for issue 369: Wrong kill animation after being hit by enemy on a sloped...
[supertux.git] / src / object / infoblock.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/infoblock.hpp"
18
19 #include "object/player.hpp"
20 #include "sprite/sprite_manager.hpp"
21 #include "supertux/object_factory.hpp"
22 #include "supertux/sector.hpp"
23 #include "supertux/info_box_line.hpp"
24 #include "util/reader.hpp"
25 #include "video/drawing_context.hpp"
26 #include "sprite/sprite.hpp"
27
28 namespace {
29 const float SCROLL_DELAY = 0.5;
30 const float SCROLL_DISTANCE = 16;
31 const float WIDTH = 400;
32 const float HEIGHT = 200;
33 }
34
35 InfoBlock::InfoBlock(const Reader& lisp) :
36   Block(sprite_manager->create("images/objects/bonus_block/infoblock.sprite")), 
37   message(),
38   shown_pct(0), 
39   dest_pct(0),
40   lines(),
41   lines_height()
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& player)
70 {
71   start_bounce(&player);
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 HitResponse
98 InfoBlock::collision(GameObject& other, const CollisionHit& hit){
99
100         Player* player = dynamic_cast<Player*> (&other);
101         if (player) {
102                 if (player->does_buttjump)
103                         InfoBlock::hit(*player);
104         }
105         return Block::collision(other, hit);
106 }
107
108 Player*
109 InfoBlock::get_nearest_player()
110 {
111   // FIXME: does not really return nearest player
112
113   std::vector<Player*> players = Sector::current()->get_players();
114   for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
115     Player* player = *playerIter;
116     return player;
117   }
118
119   return 0;
120 }
121
122 void
123 InfoBlock::update(float delta)
124 {
125   Block::update(delta);
126
127   if (delta == 0) return;
128
129   // hide message if player is too far away
130   if (dest_pct > 0) {
131     Player* player = get_nearest_player();
132     if (player) {
133       Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
134       Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
135       Vector dist = (p2 - p1);
136       float d = dist.norm();
137       if (d > 128) dest_pct = 0;
138     }
139   }
140
141   // handle soft fade-in and fade-out
142   if (shown_pct != dest_pct) {
143     if (dest_pct > shown_pct) shown_pct = std::min(shown_pct + 2*delta, dest_pct);
144     if (dest_pct < shown_pct) shown_pct = std::max(shown_pct - 2*delta, dest_pct);
145   }
146 }
147
148 void
149 InfoBlock::draw(DrawingContext& context)
150 {
151   Block::draw(context);
152
153   if (shown_pct <= 0) return;
154
155   context.push_transform();
156   //context.set_translation(Vector(0, 0));
157   context.set_alpha(shown_pct);
158
159   //float x1 = SCREEN_WIDTH/2-200;
160   //float y1 = SCREEN_HEIGHT/2-200;
161   float border = 8;
162   float width = 400; // this is the text width only
163   float height = lines_height; // this is the text height only
164   float x1 = (get_bbox().p1.x + get_bbox().p2.x)/2 - width/2;
165   float x2 = (get_bbox().p1.x + get_bbox().p2.x)/2 + width/2;
166   float y1 = original_y - height;
167
168   if(x1 < 0) {
169     x1 = 0;
170     x2 = width;
171   }
172
173   if(x2 > Sector::current()->get_width()) {
174     x2 = Sector::current()->get_width();
175     x1 = x2 - width;
176   }
177
178   // lines_height includes one ITEMS_SPACE too much, so the bottom border is reduced by 4px
179   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);
180
181   float y = y1;
182   for(size_t i = 0; i < lines.size(); ++i) {
183     if(y >= y1 + height) {
184       //log_warning << "Too many lines of text in InfoBlock" << std::endl;
185       //dest_pct = 0;
186       //shown_pct = 0;
187       break;
188     }
189
190     lines[i]->draw(context, Rectf(x1, y, x2, y), LAYER_GUI-50+1);
191     y += lines[i]->get_height();
192   }
193
194   context.pop_transform();
195 }
196
197 void
198 InfoBlock::show_message()
199 {
200   dest_pct = 1;
201 }
202
203 void
204 InfoBlock::hide_message()
205 {
206   dest_pct = 0;
207 }
208
209 /* EOF */