#include <infoblock.hpp>
Inherits Block.
Public Member Functions | |
InfoBlock (const Reader &lisp) | |
virtual | ~InfoBlock () |
void | update (float elapsed_time) |
This function is called once per frame and allows the object to update it's state. | |
void | draw (DrawingContext &context) |
The GameObject should draw itself onto the provided DrawingContext if this function is called. | |
void | show_message () |
void | hide_message () |
Protected Member Functions | |
virtual void | hit (Player &player) |
Player * | get_nearest_player () |
Protected Attributes | |
std::string | message |
float | shown_pct |
Value in the range of 0. | |
float | dest_pct |
With each call to update(), shown_pct will slowly transition to this value. | |
std::vector< InfoBoxLine * > | lines |
lines of text (or images) to display | |
float | lines_height |
Definition at line 23 of file infoblock.hpp.
InfoBlock::InfoBlock | ( | const Reader & | lisp | ) |
Definition at line 35 of file infoblock.cpp.
References MovingObject::bbox, lisp::Lisp::get(), lines, lines_height, log_warning, message, Rectf::set_pos(), InfoBoxLine::split(), Vector::x, and Vector::y.
00035 : 00036 Block(sprite_manager->create("images/objects/bonus_block/infoblock.sprite")), 00037 message(), 00038 shown_pct(0), 00039 dest_pct(0), 00040 lines(), 00041 lines_height() 00042 { 00043 Vector pos; 00044 lisp.get("x", pos.x); 00045 lisp.get("y", pos.y); 00046 bbox.set_pos(pos); 00047 00048 if(!lisp.get("message", message)) { 00049 log_warning << "No message in InfoBlock" << std::endl; 00050 } 00051 //stopped = false; 00052 //ringing = new AmbientSound(get_pos(), 0.5, 300, 1, "sounds/phone.wav"); 00053 //Sector::current()->add_object(ringing); 00054 00055 // Split text string lines into a vector 00056 lines = InfoBoxLine::split(message, 400); 00057 lines_height = 0; 00058 for(size_t i = 0; i < lines.size(); ++i) lines_height+=lines[i]->get_height(); 00059 }
InfoBlock::~InfoBlock | ( | ) | [virtual] |
Definition at line 61 of file infoblock.cpp.
References lines.
00062 { 00063 for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) { 00064 delete *i; 00065 } 00066 }
void InfoBlock::update | ( | float | elapsed_time | ) | [virtual] |
This function is called once per frame and allows the object to update it's state.
The elapsed_time is the time since the last frame in seconds and should be the base for all timed calculations (don't use SDL_GetTicks directly as this will fail in pause mode)
Reimplemented from Block.
Definition at line 112 of file infoblock.cpp.
References dest_pct, MovingObject::get_bbox(), get_nearest_player(), MovingObject::get_pos(), Vector::norm(), Rectf::p1, Rectf::p2, shown_pct, Block::update(), and Vector::y.
00113 { 00114 Block::update(delta); 00115 00116 if (delta == 0) return; 00117 00118 // hide message if player is too far away or above infoblock 00119 if (dest_pct > 0) { 00120 Player* player = get_nearest_player(); 00121 if (player) { 00122 Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2; 00123 Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2; 00124 Vector dist = (p2 - p1); 00125 float d = dist.norm(); 00126 if (d > 128 || dist.y < 0) dest_pct = 0; 00127 } 00128 } 00129 00130 // handle soft fade-in and fade-out 00131 if (shown_pct != dest_pct) { 00132 if (dest_pct > shown_pct) shown_pct = std::min(shown_pct + 2*delta, dest_pct); 00133 if (dest_pct < shown_pct) shown_pct = std::max(shown_pct - 2*delta, dest_pct); 00134 } 00135 }
void InfoBlock::draw | ( | DrawingContext & | context | ) | [virtual] |
The GameObject should draw itself onto the provided DrawingContext if this function is called.
Reimplemented from Block.
Definition at line 138 of file infoblock.cpp.
References Sector::current(), Block::draw(), DrawingContext::draw_filled_rect(), MovingObject::get_bbox(), Sector::get_width(), LAYER_GUI, lines, lines_height, Block::original_y, Rectf::p1, Rectf::p2, DrawingContext::pop_transform(), DrawingContext::push_transform(), DrawingContext::set_alpha(), shown_pct, and Vector::x.
00139 { 00140 Block::draw(context); 00141 00142 if (shown_pct <= 0) return; 00143 00144 context.push_transform(); 00145 //context.set_translation(Vector(0, 0)); 00146 context.set_alpha(shown_pct); 00147 00148 //float x1 = SCREEN_WIDTH/2-200; 00149 //float y1 = SCREEN_HEIGHT/2-200; 00150 float border = 8; 00151 float width = 400; // this is the text width only 00152 float height = lines_height; // this is the text height only 00153 float x1 = (get_bbox().p1.x + get_bbox().p2.x)/2 - width/2; 00154 float x2 = (get_bbox().p1.x + get_bbox().p2.x)/2 + width/2; 00155 float y1 = original_y - height; 00156 00157 if(x1 < 0) { 00158 x1 = 0; 00159 x2 = width; 00160 } 00161 00162 if(x2 > Sector::current()->get_width()) { 00163 x2 = Sector::current()->get_width(); 00164 x1 = x2 - width; 00165 } 00166 00167 // lines_height includes one ITEMS_SPACE too much, so the bottom border is reduced by 4px 00168 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); 00169 00170 float y = y1; 00171 for(size_t i = 0; i < lines.size(); ++i) { 00172 if(y >= y1 + height) { 00173 //log_warning << "Too many lines of text in InfoBlock" << std::endl; 00174 //dest_pct = 0; 00175 //shown_pct = 0; 00176 break; 00177 } 00178 00179 lines[i]->draw(context, Rectf(x1, y, x2, y), LAYER_GUI-50+1); 00180 y += lines[i]->get_height(); 00181 } 00182 00183 context.pop_transform(); 00184 }
void InfoBlock::show_message | ( | ) |
Definition at line 187 of file infoblock.cpp.
References dest_pct.
Referenced by hit().
00188 { 00189 dest_pct = 1; 00190 }
void InfoBlock::hide_message | ( | ) |
Definition at line 193 of file infoblock.cpp.
References dest_pct.
Referenced by hit().
00194 { 00195 dest_pct = 0; 00196 }
void InfoBlock::hit | ( | Player & | player | ) | [protected, virtual] |
Implements Block.
Definition at line 69 of file infoblock.cpp.
References Sector::current(), dest_pct, Sector::gameobjects, hide_message(), show_message(), and Block::start_bounce().
00070 { 00071 start_bounce(&player); 00072 00073 //if (!stopped) { 00074 // ringing->remove_me(); 00075 // stopped = true; 00076 //} 00077 00078 if (dest_pct != 1) { 00079 00080 // first hide all other InfoBlocks' messages in same sector 00081 Sector* parent = Sector::current(); 00082 if (!parent) return; 00083 for (Sector::GameObjects::iterator i = parent->gameobjects.begin(); i != parent->gameobjects.end(); i++) { 00084 InfoBlock* block = dynamic_cast<InfoBlock*>(*i); 00085 if (!block) continue; 00086 if (block != this) block->hide_message(); 00087 } 00088 00089 // show our message 00090 show_message(); 00091 00092 } else { 00093 hide_message(); 00094 } 00095 }
Player * InfoBlock::get_nearest_player | ( | ) | [protected] |
Definition at line 98 of file infoblock.cpp.
References Sector::current(), and Sector::get_players().
Referenced by update().
00099 { 00100 // FIXME: does not really return nearest player 00101 00102 std::vector<Player*> players = Sector::current()->get_players(); 00103 for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) { 00104 Player* player = *playerIter; 00105 return player; 00106 } 00107 00108 return 0; 00109 }
std::string InfoBlock::message [protected] |
float InfoBlock::shown_pct [protected] |
Value in the range of 0.
.1, depending on how much of the infobox is currently shown
Definition at line 42 of file infoblock.hpp.
float InfoBlock::dest_pct [protected] |
With each call to update(), shown_pct will slowly transition to this value.
Definition at line 43 of file infoblock.hpp.
Referenced by hide_message(), hit(), show_message(), and update().
std::vector<InfoBoxLine*> InfoBlock::lines [protected] |
lines of text (or images) to display
Definition at line 44 of file infoblock.hpp.
Referenced by draw(), InfoBlock(), and ~InfoBlock().
float InfoBlock::lines_height [protected] |