#include <fstream>
#include <sstream>
-#include <cassert>
-#include <cstdio>
-#include <cstdlib>
-#include <cmath>
-#include <cstring>
-#include <cerrno>
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <string.h>
+#include <errno.h>
#include <unistd.h>
-#include <ctime>
+#include <time.h>
#include <stdexcept>
#include <SDL.h>
#include "game_session.hpp"
#include "log.hpp"
+#include "console.hpp"
#include "worldmap/worldmap.hpp"
#include "mainloop.hpp"
#include "audio/sound_manager.hpp"
#include "object/endsequence_walkleft.hpp"
#include "object/endsequence_fireworks.hpp"
#include "direction.hpp"
+#include "scripting/time_scheduler.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"
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)
{
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"));
void
GameSession::restart_level()
{
+
+ if (edit_mode) {
+ force_ghost_mode();
+ return;
+ }
+
game_pause = false;
end_sequence = 0;
level->stats.total_badguys = level->get_total_badguys();
level->stats.total_secrets = level->get_total_count<SecretAreaTrigger>();
level->stats.reset();
- if(reset_sector != "")level->stats.declare_invalid();
if(reset_sector != "") {
currentsector = level->get_sector(reset_sector);
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 != "") {
delete capture_demo_stream;
delete playback_demo_stream;
delete demo_controller;
+ free_options_menu();
current_ = NULL;
}
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
}
void
GameSession::toggle_pause()
{
- if(Menu::current() == NULL) {
+ 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;
} else {
+ main_loop->set_speed(speed_before_pause);
Menu::set_current(NULL);
game_pause = false;
}
}
+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 {
+
+ // 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)
{
{
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
switch (game_menu->check()) {
case MNID_CONTINUE:
Menu::set_current(0);
+ toggle_pause();
break;
case MNID_ABORTLEVEL:
Menu::set_current(0);
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))
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)
{
using namespace WorldMapNS;
+ if (edit_mode) {
+ force_ghost_mode();
+ return;
+ }
+
if(win) {
if(WorldMap::current())
WorldMap::current()->finished_level(level.get());
}
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) {
}
// 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<GameObject*>::iterator i = currentsector->gameobjects.begin(); i != currentsector->gameobjects.end(); i++) {
- SequenceTrigger* st = dynamic_cast<SequenceTrigger*>(*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) {
+ 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();