X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fgame_session.cpp;h=5109ea98a1fbb9e7efa2cbc23699bcab247ca49d;hb=41b1a29c10b9c5e799d57356eee0d1701c3828b4;hp=2abb68c5f5d3b37eaeb84a8dca8ab682a7898a4d;hpb=fa4882876de63a2579e95530581d68def7ebf925;p=supertux.git diff --git a/src/game_session.cpp b/src/game_session.cpp index 2abb68c5f..5109ea98a 100644 --- a/src/game_session.cpp +++ b/src/game_session.cpp @@ -20,14 +20,14 @@ #include #include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include -#include +#include #include #include @@ -73,6 +73,8 @@ #include "object/endsequence_walkleft.hpp" #include "object/endsequence_fireworks.hpp" #include "direction.hpp" +#include "scripting/time_scheduler.hpp" +#include "levelintro.hpp" // the engine will be run with a logical framerate of 64fps. // We chose 64fps here because it is a power of 2, so 1/64 gives an "even" @@ -91,12 +93,13 @@ GameSession::GameSession(const std::string& levelfile_, Statistics* statistics) end_sequence(0), levelfile(levelfile_), best_level_statistics(statistics), capture_demo_stream(0), playback_demo_stream(0), demo_controller(0), - play_time(0) + play_time(0), edit_mode(false), levelintro_shown(false) { current_ = this; currentsector = NULL; game_pause = false; + speed_before_pause = main_loop->get_speed(); statistics_backdrop.reset(new Surface("images/engine/menu/score-backdrop.png")); @@ -114,6 +117,12 @@ GameSession::GameSession(const std::string& levelfile_, Statistics* statistics) void GameSession::restart_level() { + + if (edit_mode) { + force_ghost_mode(); + return; + } + game_pause = false; end_sequence = 0; @@ -127,7 +136,6 @@ GameSession::restart_level() level->stats.total_badguys = level->get_total_badguys(); level->stats.total_secrets = level->get_total_count(); level->stats.reset(); - if(reset_sector != "")level->stats.declare_invalid(); if(reset_sector != "") { currentsector = level->get_sector(reset_sector); @@ -136,16 +144,17 @@ GameSession::restart_level() msg << "Couldn't find sector '" << reset_sector << "' for resetting tux."; throw std::runtime_error(msg.str()); } + level->stats.declare_invalid(); currentsector->activate(reset_pos); } else { currentsector = level->get_sector("main"); if(!currentsector) throw std::runtime_error("Couldn't find main sector"); + play_time = 0; currentsector->activate("main"); } - //levelintro(); - + sound_manager->stop_music(); currentsector->play_music(LEVEL_MUSIC); if(capture_file != "") { @@ -163,6 +172,7 @@ GameSession::~GameSession() delete capture_demo_stream; delete playback_demo_stream; delete demo_controller; + free_options_menu(); current_ = NULL; } @@ -232,51 +242,14 @@ GameSession::play_demo(const std::string& filename) } void -GameSession::levelintro() -{ - sound_manager->stop_music(); - - DrawingContext context; - for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin(); - i != currentsector->gameobjects.end(); ++i) { - Background* background = dynamic_cast (*i); - if(background) { - background->draw(context); - } - Gradient* gradient = dynamic_cast (*i); - if(gradient) { - gradient->draw(context); - } - } - -// context.draw_text(gold_text, level->get_name(), Vector(SCREEN_WIDTH/2, 160), -// ALIGN_CENTER, LAYER_FOREGROUND1); - context.draw_center_text(gold_text, level->get_name(), Vector(0, 160), - LAYER_FOREGROUND1); - - std::stringstream ss_coins; - ss_coins << _("Coins") << ": " << player_status->coins; - context.draw_text(white_text, ss_coins.str(), Vector(SCREEN_WIDTH/2, 210), - ALIGN_CENTER, LAYER_FOREGROUND1); - - if((level->get_author().size()) && (level->get_author() != "SuperTux Team")) - context.draw_text(white_small_text, - std::string(_("contributed by ")) + level->get_author(), - Vector(SCREEN_WIDTH/2, 350), ALIGN_CENTER, LAYER_FOREGROUND1); - - if(best_level_statistics != NULL) - best_level_statistics->draw_message_info(context, _("Best Level Statistics")); - - wait_for_event(1.0, 3.0); -} - -void GameSession::on_escape_press() { if(currentsector->player->is_dying() || end_sequence) { // Let the timers run out, we fast-forward them to force past a sequence - if (end_sequence) end_sequence->stop(); + if (end_sequence) + end_sequence->stop(); + currentsector->player->dying_timer.start(FLT_EPSILON); return; // don't let the player open the menu, when he is dying } @@ -292,16 +265,44 @@ GameSession::on_escape_press() void GameSession::toggle_pause() { - if(Menu::current() == NULL) { + // pause + if(!game_pause) { + speed_before_pause = main_loop->get_speed(); + main_loop->set_speed(0); Menu::set_current(game_menu.get()); game_menu->set_active_item(MNID_CONTINUE); game_pause = true; + } + + // unpause is done in update() after the menu is processed +} + +void +GameSession::set_editmode(bool edit_mode) +{ + if (this->edit_mode == edit_mode) return; + this->edit_mode = edit_mode; + + currentsector->get_players()[0]->set_edit_mode(edit_mode); + + if (edit_mode) { + + // entering edit mode + } else { - Menu::set_current(NULL); - game_pause = false; + + // leaving edit mode + restart_level(); + } } +void +GameSession::force_ghost_mode() +{ + currentsector->get_players()[0]->set_ghost_mode(true); +} + HSQUIRRELVM GameSession::run_script(std::istream& in, const std::string& sourcename) { @@ -336,9 +337,14 @@ void GameSession::process_events() { // end of pause mode? + // XXX this looks like a fail-safe to unpause the game if there's no menu + // XXX having it enabled causes some unexpected problems + // XXX hopefully disabling it won't... + /* if(!Menu::current() && game_pause) { game_pause = false; } + */ // playback a demo? if(playback_demo_stream != 0) { @@ -402,7 +408,7 @@ GameSession::draw_pause(DrawingContext& context) { context.draw_filled_rect( Vector(0,0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT), - Color(.2f, .2f, .2f, .5f), LAYER_FOREGROUND1); + Color(0.0f, 0.0f, 0.0f, .25f), LAYER_FOREGROUND1); } void @@ -416,6 +422,7 @@ GameSession::process_menu() switch (game_menu->check()) { case MNID_CONTINUE: Menu::set_current(0); + toggle_pause(); break; case MNID_ABORTLEVEL: Menu::set_current(0); @@ -432,10 +439,20 @@ GameSession::setup() Menu::set_current(NULL); current_ = this; + if(currentsector != Sector::current()) { + currentsector->activate(currentsector->player->get_pos()); + } + currentsector->play_music(LEVEL_MUSIC); + // Eat unneeded events SDL_Event event; while(SDL_PollEvent(&event)) {} + + if (!levelintro_shown) { + levelintro_shown = true; + main_loop->push_screen(new LevelIntro(level.get(), best_level_statistics)); + } } void @@ -448,6 +465,12 @@ GameSession::update(float elapsed_time) process_events(); process_menu(); + // Unpause the game if the menu has been closed + if (game_pause && !Menu::current()) { + main_loop->set_speed(speed_before_pause); + game_pause = false; + } + check_end_conditions(); // respawning in new sector? @@ -472,15 +495,15 @@ GameSession::update(float elapsed_time) currentsector->update(elapsed_time); } else { if (!end_sequence->is_tux_stopped()) { - currentsector->update(elapsed_time/2); + currentsector->update(elapsed_time); } else { - end_sequence->update(elapsed_time/2); + end_sequence->update(elapsed_time); } } } // update sounds - sound_manager->set_listener_position(currentsector->player->get_pos()); + if (currentsector && currentsector->camera) sound_manager->set_listener_position(currentsector->camera->get_center()); /* Handle music: */ if (end_sequence) @@ -503,6 +526,11 @@ GameSession::finish(bool win) { using namespace WorldMapNS; + if (edit_mode) { + force_ghost_mode(); + return; + } + if(win) { if(WorldMap::current()) WorldMap::current()->finished_level(level.get()); @@ -532,45 +560,14 @@ GameSession::get_working_directory() } void -GameSession::display_info_box(const std::string& text) +GameSession::start_sequence(const std::string& sequencename) { - InfoBox* box = new InfoBox(text); - - bool running = true; - DrawingContext context; - - while(running) { - - // TODO make a screen out of this, another mainloop is ugly - main_controller->update(); - SDL_Event event; - while (SDL_PollEvent(&event)) { - main_controller->process_event(event); - if(event.type == SDL_QUIT) - main_loop->quit(); - } - - if(main_controller->pressed(Controller::JUMP) - || main_controller->pressed(Controller::ACTION) - || main_controller->pressed(Controller::PAUSE_MENU) - || main_controller->pressed(Controller::MENU_SELECT)) - running = false; - else if(main_controller->pressed(Controller::DOWN)) - box->scrolldown(); - else if(main_controller->pressed(Controller::UP)) - box->scrollup(); - box->draw(context); - draw(context); - context.do_drawing(); - sound_manager->update(); + // do not play sequences when in edit mode + if (edit_mode) { + force_ghost_mode(); + return; } - delete box; -} - -void -GameSession::start_sequence(const std::string& sequencename) -{ // handle special "stoptux" sequence if (sequencename == "stoptux") { if (!end_sequence) { @@ -582,39 +579,25 @@ GameSession::start_sequence(const std::string& sequencename) } // abort if a sequence is already playing - if (end_sequence) return; + if (end_sequence) + return; if (sequencename == "endsequence") { - - // Determine walking direction for Tux - /*float xst = 1.f, xend = 2.f; - for(std::vector::iterator i = currentsector->gameobjects.begin(); i != currentsector->gameobjects.end(); i++) { - SequenceTrigger* st = dynamic_cast(*i); - if(!st) - continue; - if(st->get_sequence_name() == "stoptux") - xend = st->get_pos().x; - else if(st->get_sequence_name() == "endsequence") - xst = st->get_pos().y; - } - - if (xst > xend) { - end_sequence = new EndSequenceWalkLeft(); - } else { - end_sequence = new EndSequenceWalkRight(); - }*/ if (currentsector->get_players()[0]->physic.get_velocity_x() < 0) { end_sequence = new EndSequenceWalkLeft(); } else { end_sequence = new EndSequenceWalkRight(); } - } - else if (sequencename == "fireworks") end_sequence = new EndSequenceFireworks(); - else { + } else if (sequencename == "fireworks") { + end_sequence = new EndSequenceFireworks(); + } else { log_warning << "Unknown sequence '" << sequencename << "'. Ignoring." << std::endl; return; } + /* slow down the game for end-sequence */ + main_loop->set_speed(0.5f); + currentsector->add_object(end_sequence); end_sequence->start(); @@ -623,7 +606,7 @@ GameSession::start_sequence(const std::string& sequencename) // Stop all clocks. for(std::vector::iterator i = currentsector->gameobjects.begin(); - i != currentsector->gameobjects.end(); ++i) + i != currentsector->gameobjects.end(); ++i) { GameObject* obj = *i;