map_dialogue.cpp

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00016 #include "utils.h"
00017 
00018 #include "audio.h"
00019 #include "video.h"
00020 #include "input.h"
00021 #include "script.h"
00022 #include "global.h"
00023 #include "menu.h"
00024 
00025 #include "map_dialogue.h"
00026 #include "map.h"
00027 #include "map_objects.h"
00028 #include "map_sprites.h"
00029 
00030 using namespace std;
00031 using namespace hoa_utils;
00032 using namespace hoa_audio;
00033 using namespace hoa_video;
00034 using namespace hoa_input;
00035 using namespace hoa_script;
00036 using namespace hoa_system;
00037 using namespace hoa_global;
00038 using namespace hoa_menu;
00039 
00040 namespace hoa_map {
00041 
00042 namespace private_map {
00043 
00044 // ****************************************************************************
00045 // *********************** MapDialogue Class Functions ************************
00046 // ****************************************************************************
00047 
00048 DialogueManager::DialogueManager() {
00049   VideoManager->PushState();
00050   VideoManager->SetCoordSys(0, 1024, 768, 0);
00051 //  MenuWindow::Create(1024.0f, 256.0f);
00052 //  MenuWindow::SetPosition(0.0f, 512.0f);
00053 //  MenuWindow::SetDisplayMode(VIDEO_MENU_EXPAND_FROM_CENTER);
00054 
00055   _background_image.SetFilename("img/menus/dialogue_box.png");
00056   if (_background_image.Load() == false)
00057     cerr << "MAP ERROR: failed to load image: " << _background_image.GetFilename() << endl;
00058 
00059   _nameplate_image.SetFilename("img/menus/dialogue_nameplate.png");
00060   if (_nameplate_image.Load() == false)
00061     cerr << "MAP ERROR: failed to load image: " << _nameplate_image.GetFilename() << endl;
00062 
00063   _display_textbox.SetDisplaySpeed(30);
00064   _display_textbox.SetPosition(300.0f, 768.0f - 180.0f);
00065   _display_textbox.SetDimensions(1024.0f - 300.0f - 60.0f, 180.0f - 70.0f);
00066   _display_textbox.SetFont("map");
00067   _display_textbox.SetTextColor(Color::black);
00068   _display_textbox.SetDisplayMode(VIDEO_TEXT_FADECHAR);
00069   _display_textbox.SetAlignment(VIDEO_X_LEFT, VIDEO_Y_TOP);
00070 
00071   VideoManager->PopState();
00072 }
00073 
00074 
00075 DialogueManager::~DialogueManager() {
00076 //  MenuWindow::Destroy();
00077 //_display_textbox.SetDisplayText(_current_dialogue->GetLine());
00078 }
00079 
00080 
00081 void DialogueManager::Update() {
00082   static int32 time_remaining = 0;          // The time that remains for the display of the current line
00083   static MapDialogue* last_dialogue = NULL; // Used to detect if this is the first update to a new piece of dialogue
00084   bool finish_line = false;                 // When set to true, indicates that the current line of dialogue is finished
00085 
00086   if (_current_dialogue == NULL)
00087     return;
00088 
00089   if (_current_dialogue != last_dialogue) {
00090     time_remaining = _current_dialogue->GetCurrentTime();
00091     _display_textbox.SetDisplayText(_current_dialogue->GetCurrentText());
00092     last_dialogue = _current_dialogue;
00093   }
00094 
00095   _display_textbox.Update(MapMode::_current_map->_time_elapsed);
00096 
00097   // Update the dialogue timer
00098   if (time_remaining > 0) {
00099     time_remaining -= MapMode::_current_map->_time_elapsed;
00100     // If it get below 0, clip it to 0 as -1 means infinite
00101     if (time_remaining < 0) {
00102       time_remaining = 0;
00103       finish_line = true;
00104     }
00105   }
00106 
00107   // Check for user input only if this dialogue is non-blocking
00108   if (_current_dialogue->IsBlocked() == false) {
00109     if (InputManager->ConfirmPress()) {
00110       // If the line is not yet finished displaying, display the rest of the text
00111       if (!_display_textbox.IsFinished()) {
00112         _display_textbox.ForceFinish();
00113       }
00114       // Otherwise, finish this line
00115       else {
00116         finish_line = true;
00117       }
00118     }
00119   }
00120 
00121   // If the line has been finished, process the post-line action if it exists and move on to the next line
00122   if (finish_line == true) {
00123     if (_current_dialogue->GetCurrentAction() != NULL) {
00124       try {
00125         ScriptCallFunction<void>(*(_current_dialogue->GetCurrentAction()));
00126       } catch (luabind::error& e) {
00127         ScriptManager->HandleLuaError(e);
00128       }
00129     }
00130   
00131     // Move to the next line of dialogue
00132     if (_current_dialogue->ReadNextLine() == true) {
00133       time_remaining = _current_dialogue->GetCurrentTime();
00134       _display_textbox.SetDisplayText(_current_dialogue->GetCurrentText());
00135     }
00136 
00137     // This dialogue is finished, restore game state as necessary
00138     else {
00139       // The is no more line, the dialogue is over
00140       MapMode::_current_map->_map_state = EXPLORE;
00141       // Restore the status of the map sprites
00142       if (_current_dialogue->IsSaving()) {
00143         for (uint32 i = 0; i < _current_dialogue->GetNumLines(); i++) {
00144           static_cast<VirtualSprite*>(MapMode::_current_map->_all_objects[_current_dialogue->GetLineSpeaker(i)])->LoadState();
00145         }
00146       }
00147       _current_dialogue = NULL;
00148       last_dialogue = NULL;
00149     }
00150   }
00151 } // void DialogueManager::Update()
00152 
00153 
00154 
00155 void DialogueManager::Draw() {
00156   VideoManager->PushState();
00157   VideoManager->SetCoordSys(0.0f, 1024.0f, 768.0f, 0.0f);
00158   VideoManager->SetDrawFlags(VIDEO_X_LEFT, VIDEO_Y_BOTTOM, 0);
00159   VideoManager->Move(0.0f, 768.0f);
00160   _background_image.Draw();
00161   VideoManager->MoveRelative(47.0f, -42.0f);
00162   _nameplate_image.Draw();
00163 
00164   VideoManager->SetDrawFlags(VIDEO_X_CENTER, VIDEO_Y_BOTTOM, 0);
00165   VideoManager->SetFont("map");
00166   VideoManager->SetTextColor(Color(Color::black));
00167   VideoManager->MoveRelative(120.0f, -10.0f);
00168   VirtualSprite* speaker = reinterpret_cast<VirtualSprite*>(MapMode::_current_map->_all_objects[_current_dialogue->GetCurrentSpeaker()]);
00169   VideoManager->DrawText(speaker->name);
00170   if (speaker->face_portrait != NULL) {
00171     VideoManager->MoveRelative(0.0f, -26.0f);
00172     speaker->face_portrait->Draw();
00173   }
00174   _display_textbox.Draw();
00175   VideoManager->PopState();
00176 } // void DialogueManager::Draw()
00177 
00178 // ****************************************************************************
00179 // *********************** MapDialogue Class Functions ************************
00180 // ****************************************************************************
00181 
00182 MapDialogue::MapDialogue(bool save_state) :
00183   _seen(0),
00184   _current_line(0),
00185   _blocked(false)
00186 {
00187   _save_state = save_state;
00188 }
00189 
00190 
00191 
00192 MapDialogue::~MapDialogue() {
00193   for (uint32 i = 0; i < _actions.size(); ++i) {
00194     if (_actions[i] != NULL) {
00195       delete _actions[i];
00196     }
00197   }
00198 }
00199 
00200 
00201 
00202 bool MapDialogue::ReadNextLine() {
00203   // Determine if the dialogue is finished
00204   if (++_current_line >= _text.size()) {
00205     _current_line = 0;
00206     IncrementTimesSeen();
00207     if (_owner != NULL) {
00208       _owner->UpdateSeenDialogue();
00209     }
00210     return false;
00211   }
00212   // Return true if the dialogue has additional lines to read
00213   return true;
00214 }
00215 
00216 
00217 
00218 void MapDialogue::AddText(std::string text, uint32 speaker_id, int32 time, int32 action) {
00219   _text.push_back(MakeUnicodeString(text));
00220   _speakers.push_back(speaker_id);
00221   _time.push_back(time);
00222   if (action >= 0) {
00223     MapMode::_loading_map->_map_script.OpenTable("map_functions");
00224     ScriptObject* new_action = new ScriptObject();
00225     *new_action = MapMode::_loading_map->_map_script.ReadFunctionPointer(action);
00226     MapMode::_loading_map->_map_script.CloseTable();
00227     _actions.push_back(new_action);
00228   }
00229   else {
00230     _actions.push_back(NULL);
00231   }
00232 }
00233 
00234 } // namespace private_map
00235 
00236 } // namespace hoa_map

Generated on Fri Jul 6 23:11:17 2007 for Hero of Allacrost by  doxygen 1.5.1