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.
27 #include "textscroller.hpp"
28 #include "functions.hpp"
29 #include "game_session.hpp"
30 #include "tinygettext/tinygettext.hpp"
31 #include "physfs/physfs_stream.hpp"
32 #include "script_manager.hpp"
33 #include "resources.hpp"
34 #include "gettext.hpp"
36 #include "mainloop.hpp"
37 #include "worldmap/worldmap.hpp"
40 #include "gameconfig.hpp"
41 #include "object/player.hpp"
42 #include "object/tilemap.hpp"
44 #include "fadeout.hpp"
45 #include "shrinkfade.hpp"
46 #include "object/camera.hpp"
47 #include "flip_level_transformer.hpp"
49 #include "squirrel_error.hpp"
50 #include "wrapper_util.hpp"
55 int display(HSQUIRRELVM vm)
57 Console::output << squirrel2string(vm, -1) << std::endl;
61 void wait(HSQUIRRELVM vm, float seconds)
63 SQUserPointer ptr = sq_getforeignptr(vm);
64 ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
65 script_manager->set_wakeup_event(vm, ScriptManager::TIME, seconds);
68 void wait_for_screenswitch(HSQUIRRELVM vm)
70 SQUserPointer ptr = sq_getforeignptr(vm);
71 ScriptManager* script_manager = reinterpret_cast<ScriptManager*> (ptr);
72 script_manager->set_wakeup_event(vm, ScriptManager::SCREEN_SWITCHED);
77 main_loop->exit_screen();
80 void fadeout_screen(float seconds)
82 main_loop->set_screen_fade(new FadeOut(seconds));
85 void shrink_screen(float dest_x, float dest_y, float seconds)
87 main_loop->set_screen_fade(new ShrinkFade(Vector(dest_x, dest_y), seconds));
90 std::string translate(const std::string& text)
92 return dictionary_manager.get_dictionary().translate(text);
95 void display_text_file(const std::string& filename)
97 main_loop->push_screen(new TextScroller(filename));
100 void load_worldmap(const std::string& filename)
102 using namespace WorldMapNS;
104 main_loop->push_screen(new WorldMap(filename));
107 void load_level(const std::string& filename)
109 main_loop->push_screen(new GameSession(filename));
112 static SQInteger squirrel_read_char(SQUserPointer file)
114 std::istream* in = reinterpret_cast<std::istream*> (file);
122 void import(HSQUIRRELVM vm, const std::string& filename)
124 IFileStream in(filename);
126 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in,
127 filename.c_str(), SQTrue)))
128 throw SquirrelError(vm, "Couldn't parse script");
130 sq_pushroottable(vm);
131 if(SQ_FAILED(sq_call(vm, 1, SQFalse))) {
133 throw SquirrelError(vm, "Couldn't execute script");
138 void add_key(int new_key)
140 player_status->set_keys(new_key);
143 void debug_collrects(bool enable)
145 Sector::show_collrects = enable;
148 void debug_draw_fps(bool enable)
150 config->show_fps = enable;
153 void debug_draw_solids_only(bool enable)
155 Sector::draw_solids_only = enable;
160 using namespace WorldMapNS;
162 if(World::current() == NULL)
163 throw std::runtime_error("Can't save state without active World");
165 if(WorldMap::current() != NULL)
166 WorldMap::current()->save_state();
167 World::current()->save_state();
170 // not added to header, function to only be used by others
172 bool validate_sector_player()
174 if (Sector::current() == 0)
176 log_info << "No current sector." << std::endl;
180 if (Sector::current()->player == 0)
182 log_info << "No player." << std::endl;
190 if (!validate_sector_player()) return;
191 ::Player* tux = Sector::current()->player; // Scripting::Player != ::Player
192 tux->physic.set_velocity_x(tux->physic.get_velocity_x()*3);
197 if (!validate_sector_player()) return;
198 ::Player* tux = Sector::current()->player;
199 tux->invincible_timer.start(10000);
204 if (!validate_sector_player()) return;
205 ::Player* tux = Sector::current()->player;
206 tux->invincible_timer.stop();
211 if (!validate_sector_player()) return;
212 ::Player* tux = Sector::current()->player;
213 tux->kill(tux->SHRINK);
218 if (!validate_sector_player()) return;
219 ::Player* tux = Sector::current()->player;
220 tux->kill(tux->KILL);
225 if (GameSession::current() == 0)
227 log_info << "No game session" << std::endl;
230 GameSession::current()->restart_level();
235 if (!validate_sector_player()) return;
236 ::Player* tux = Sector::current()->player;
237 log_info << "You are at x " << tux->get_pos().x << ", y " << tux->get_pos().y << std::endl;
242 if (!validate_sector_player()) return;
243 ::Player* tux = Sector::current()->player;
245 (Sector::current()->solids->get_width()*32) - (SCREEN_WIDTH*2), 0));
246 Sector::current()->camera->reset(
247 Vector(tux->get_pos().x, tux->get_pos().y));
252 if (!validate_sector_player()) return;
253 log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;