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.
35 #include "game_session.hpp"
37 #include "worldmap/worldmap.hpp"
38 #include "mainloop.hpp"
39 #include "video/screen.hpp"
40 #include "audio/sound_manager.hpp"
41 #include "gui/menu.hpp"
45 #include "player_status.hpp"
46 #include "object/particlesystem.hpp"
47 #include "object/background.hpp"
48 #include "object/gradient.hpp"
49 #include "object/tilemap.hpp"
50 #include "object/camera.hpp"
51 #include "object/player.hpp"
52 #include "object/level_time.hpp"
53 #include "lisp/lisp.hpp"
54 #include "lisp/parser.hpp"
55 #include "resources.hpp"
56 #include "statistics.hpp"
58 #include "options_menu.hpp"
59 #include "object/fireworks.hpp"
60 #include "textscroller.hpp"
61 #include "control/codecontroller.hpp"
62 #include "control/joystickkeyboardcontroller.hpp"
64 #include "file_system.hpp"
65 #include "gameconfig.hpp"
66 #include "gettext.hpp"
67 #include "console.hpp"
68 #include "flip_level_transformer.hpp"
70 // the engine will be run with a logical framerate of 64fps.
71 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
73 static const float LOGICAL_FPS = 64.0;
80 GameSession* GameSession::current_ = NULL;
82 GameSession::GameSession(const std::string& levelfile_, Statistics* statistics)
83 : level(0), currentsector(0),
84 end_sequence(NO_ENDSEQUENCE), end_sequence_controller(0),
85 levelfile(levelfile_), best_level_statistics(statistics),
86 capture_demo_stream(0), playback_demo_stream(0), demo_controller(0)
93 statistics_backdrop.reset(new Surface("images/engine/menu/score-backdrop.png"));
97 game_menu.reset(new Menu());
98 game_menu->add_label(_("Pause"));
100 game_menu->add_entry(MNID_CONTINUE, _("Continue"));
101 game_menu->add_submenu(_("Options"), get_options_menu());
103 game_menu->add_entry(MNID_ABORTLEVEL, _("Abort Level"));
107 GameSession::restart_level(bool fromBeginning)
110 end_sequence = NO_ENDSEQUENCE;
112 main_controller->reset();
116 level.reset(new Level);
117 level->load(levelfile);
119 global_stats.reset();
120 global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
121 global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
125 for(std::vector<Sector*>::iterator i = level->sectors.begin(); i != level->sectors.end(); ++i)
129 for(std::vector<GameObject*>::iterator j = sec->gameobjects.begin();
130 j != sec->gameobjects.end(); ++j)
132 GameObject* obj = *j;
134 LevelTime* lt = dynamic_cast<LevelTime*> (obj);
136 time += int(lt->get_level_time());
139 global_stats.set_total_points(TIME_NEEDED_STAT, (time == 0) ? -1 : time);
141 if (fromBeginning) reset_sector="";
142 if(reset_sector != "") {
143 currentsector = level->get_sector(reset_sector);
145 std::stringstream msg;
146 msg << "Couldn't find sector '" << reset_sector << "' for resetting tux.";
147 throw std::runtime_error(msg.str());
149 currentsector->activate(reset_pos);
151 currentsector = level->get_sector("main");
153 throw std::runtime_error("Couldn't find main sector");
154 currentsector->activate("main");
159 currentsector->play_music(LEVEL_MUSIC);
161 if(capture_file != "")
162 record_demo(capture_file);
165 GameSession::~GameSession()
167 delete capture_demo_stream;
168 delete playback_demo_stream;
169 delete demo_controller;
171 delete end_sequence_controller;
177 GameSession::record_demo(const std::string& filename)
179 delete capture_demo_stream;
181 capture_demo_stream = new std::ofstream(filename.c_str());
182 if(!capture_demo_stream->good()) {
183 std::stringstream msg;
184 msg << "Couldn't open demo file '" << filename << "' for writing.";
185 throw std::runtime_error(msg.str());
187 capture_file = filename;
191 GameSession::play_demo(const std::string& filename)
193 delete playback_demo_stream;
194 delete demo_controller;
196 playback_demo_stream = new std::ifstream(filename.c_str());
197 if(!playback_demo_stream->good()) {
198 std::stringstream msg;
199 msg << "Couldn't open demo file '" << filename << "' for reading.";
200 throw std::runtime_error(msg.str());
203 Player& tux = *currentsector->player;
204 demo_controller = new CodeController();
205 tux.set_controller(demo_controller);
209 GameSession::levelintro()
213 sound_manager->stop_music();
215 DrawingContext context;
216 for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
217 i != currentsector->gameobjects.end(); ++i) {
218 Background* background = dynamic_cast<Background*> (*i);
220 background->draw(context);
222 Gradient* gradient = dynamic_cast<Gradient*> (*i);
224 gradient->draw(context);
228 // context.draw_text(gold_text, level->get_name(), Vector(SCREEN_WIDTH/2, 160),
229 // CENTER_ALLIGN, LAYER_FOREGROUND1);
230 context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
233 sprintf(str, "Coins: %d", player_status->coins);
234 context.draw_text(white_text, str, Vector(SCREEN_WIDTH/2, 210),
235 CENTER_ALLIGN, LAYER_FOREGROUND1);
237 if((level->get_author().size()) && (level->get_author() != "SuperTux Team"))
238 //TODO make author check case/blank-insensitive
239 context.draw_text(white_small_text,
240 std::string(_("contributed by ")) + level->get_author(),
241 Vector(SCREEN_WIDTH/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
244 if(best_level_statistics != NULL)
245 best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
247 wait_for_event(1.0, 3.0);
251 GameSession::on_escape_press()
253 if(currentsector->player->is_dying() || end_sequence != NO_ENDSEQUENCE)
254 return; // don't let the player open the menu, when he is dying
256 if (!Menu::current()) {
257 Menu::set_current(game_menu.get());
258 game_menu->set_active_item(MNID_CONTINUE);
261 Menu::set_current(NULL);
267 GameSession::process_events()
269 Player& tux = *currentsector->player;
271 // end of pause mode?
272 if(!Menu::current() && game_pause) {
276 if (end_sequence != NO_ENDSEQUENCE) {
277 if(end_sequence_controller == 0) {
278 end_sequence_controller = new CodeController();
279 tux.set_controller(end_sequence_controller);
282 end_sequence_controller->press(Controller::RIGHT);
284 if (int(last_x_pos) == int(tux.get_pos().x))
285 end_sequence_controller->press(Controller::JUMP);
286 last_x_pos = tux.get_pos().x;
290 if(playback_demo_stream != 0) {
291 demo_controller->update();
298 playback_demo_stream->get(left);
299 playback_demo_stream->get(right);
300 playback_demo_stream->get(up);
301 playback_demo_stream->get(down);
302 playback_demo_stream->get(jump);
303 playback_demo_stream->get(action);
304 demo_controller->press(Controller::LEFT, left);
305 demo_controller->press(Controller::RIGHT, right);
306 demo_controller->press(Controller::UP, up);
307 demo_controller->press(Controller::DOWN, down);
308 demo_controller->press(Controller::JUMP, jump);
309 demo_controller->press(Controller::ACTION, action);
312 // save input for demo?
313 if(capture_demo_stream != 0) {
314 capture_demo_stream ->put(main_controller->hold(Controller::LEFT));
315 capture_demo_stream ->put(main_controller->hold(Controller::RIGHT));
316 capture_demo_stream ->put(main_controller->hold(Controller::UP));
317 capture_demo_stream ->put(main_controller->hold(Controller::DOWN));
318 capture_demo_stream ->put(main_controller->hold(Controller::JUMP));
319 capture_demo_stream ->put(main_controller->hold(Controller::ACTION));
324 GameSession::check_end_conditions()
326 Player* tux = currentsector->player;
329 if(end_sequence && endsequence_timer.check()) {
332 } else if (!end_sequence && tux->is_dead()) {
333 if (player_status->coins < 0) {
334 // No more coins: restart level from beginning
335 player_status->coins = 0;
338 // Still has coins: restart level from last reset point
339 restart_level(false);
347 GameSession::draw(DrawingContext& context)
349 currentsector->draw(context);
357 GameSession::draw_pause(DrawingContext& context)
359 context.draw_filled_rect(
360 Vector(0,0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
361 Color(.2, .2, .2, .5), LAYER_FOREGROUND1);
365 GameSession::process_menu()
367 Menu* menu = Menu::current();
371 if(menu == game_menu.get()) {
372 switch (game_menu->check()) {
374 Menu::set_current(0);
376 case MNID_ABORTLEVEL:
377 Menu::set_current(0);
378 main_loop->exit_screen();
388 Menu::set_current(NULL);
391 // Eat unneeded events
393 while(SDL_PollEvent(&event))
398 GameSession::update(float elapsed_time)
403 check_end_conditions();
406 if(main_controller->pressed(Controller::PAUSE_MENU))
409 // respawning in new sector?
410 if(newsector != "" && newspawnpoint != "") {
411 Sector* sector = level->get_sector(newsector);
413 log_warning << "Sector '" << newsector << "' not found" << std::endl;
415 sector->activate(newspawnpoint);
416 sector->play_music(LEVEL_MUSIC);
417 currentsector = sector;
422 // Update the world state and all objects in the world
425 if (end_sequence == ENDSEQUENCE_RUNNING) {
426 currentsector->update(elapsed_time/2);
427 } else if(end_sequence == NO_ENDSEQUENCE) {
428 if(!currentsector->player->growing_timer.started())
429 currentsector->update(elapsed_time);
434 sound_manager->set_listener_position(currentsector->player->get_pos());
440 if(currentsector->player->invincible_timer.started()) {
441 if(currentsector->player->invincible_timer.get_timeleft() <=
442 TUX_INVINCIBLE_TIME_WARNING) {
443 currentsector->play_music(HERRING_WARNING_MUSIC);
445 currentsector->play_music(HERRING_MUSIC);
447 } else if(currentsector->get_music_type() != LEVEL_MUSIC) {
448 currentsector->play_music(LEVEL_MUSIC);
453 GameSession::finish(bool win)
455 using namespace WorldMapNS;
458 if(WorldMap::current())
459 WorldMap::current()->finished_level(levelfile);
462 main_loop->exit_screen();
466 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
469 newspawnpoint = spawnpoint;
473 GameSession::set_reset_point(const std::string& sector, const Vector& pos)
475 reset_sector = sector;
480 GameSession::get_working_directory()
482 return FileSystem::dirname(levelfile);
486 GameSession::display_info_box(const std::string& text)
488 InfoBox* box = new InfoBox(text);
491 DrawingContext context;
495 // TODO make a screen out of this, another mainloop is ugly
496 main_controller->update();
498 while (SDL_PollEvent(&event)) {
499 main_controller->process_event(event);
500 if(event.type == SDL_QUIT)
504 if(main_controller->pressed(Controller::JUMP)
505 || main_controller->pressed(Controller::ACTION)
506 || main_controller->pressed(Controller::PAUSE_MENU)
507 || main_controller->pressed(Controller::MENU_SELECT))
509 else if(main_controller->pressed(Controller::DOWN))
511 else if(main_controller->pressed(Controller::UP))
515 context.do_drawing();
516 sound_manager->update();
523 GameSession::start_sequence(const std::string& sequencename)
525 if(sequencename == "endsequence" || sequencename == "fireworks") {
529 end_sequence = ENDSEQUENCE_RUNNING;
530 endsequence_timer.start(7.3);
532 sound_manager->play_music("music/leveldone.ogg", false);
533 currentsector->player->invincible_timer.start(7.3);
536 for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin();
537 i != currentsector->gameobjects.end(); ++i)
539 GameObject* obj = *i;
541 LevelTime* lt = dynamic_cast<LevelTime*> (obj);
546 // add time spent to statistics
547 int tottime = 0, remtime = 0;
548 for(std::vector<Sector*>::iterator i = level->sectors.begin(); i != level->sectors.end(); ++i)
552 for(std::vector<GameObject*>::iterator j = sec->gameobjects.begin();
553 j != sec->gameobjects.end(); ++j)
555 GameObject* obj = *j;
557 LevelTime* lt = dynamic_cast<LevelTime*> (obj);
560 tottime += int(lt->get_level_time());
561 remtime += int(lt->get_remaining_time());
565 global_stats.set_points(TIME_NEEDED_STAT, (tottime == 0 ? -1 : (tottime-remtime)));
567 if(sequencename == "fireworks") {
568 currentsector->add_object(new Fireworks());
570 } else if(sequencename == "stoptux") {
572 log_warning << "Final target reached without an active end sequence" << std::endl;
573 this->start_sequence("endsequence");
575 end_sequence = ENDSEQUENCE_WAITING;
577 log_warning << "Unknown sequence '" << sequencename << "'" << std::endl;
583 GameSession::drawstatus(DrawingContext& context)
585 player_status->draw(context);
587 // draw level stats while end_sequence is running
589 global_stats.draw_endseq_panel(context, best_level_statistics, statistics_backdrop.get());