return D_NORTH;
else if (directory == "south")
return D_SOUTH;
- else
+ else if (directory == "none")
+ return D_NONE;
+ else {
+ log_warning << "unknown direction: \"" << directory << "\"" << std::endl;
return D_NONE;
+ }
}
//---------------------------------------------------------------------------
WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpoint)
- : tux(0), solids(0), force_spawnpoint(force_spawnpoint)
+ : tux(0), solids(0), ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), force_spawnpoint(force_spawnpoint), in_level(false)
{
tile_manager.reset(new TileManager("images/worldmap.strf"));
-
+
tux = new Tux(this);
add_object(tux);
-
+
name = "<no title>";
music = "music/salcon.ogg";
throw Scripting::SquirrelError(global_vm, "Couldn't get table from stack");
sq_addref(global_vm, &worldmap_table);
- sq_pop(global_vm, 1);
+ sq_pop(global_vm, 1);
}
WorldMap::~WorldMap()
{
using namespace Scripting;
+ save_state();
+
for(ScriptList::iterator i = scripts.begin();
i != scripts.end(); ++i) {
HSQOBJECT& object = *i;
sq_release(global_vm, &worldmap_table);
sq_collectgarbage(global_vm);
-
+
if(current_ == this)
current_ = NULL;
if(sp->name == spawnpoint) {
Vector p = sp->pos;
tux->set_tile_pos(p);
+ tux->set_direction(sp->auto_dir);
return;
}
}
throw std::runtime_error("file isn't a supertux-level file.");
lisp->get("name", name);
-
+
const lisp::Lisp* sector = lisp->get_lisp("sector");
if(!sector)
throw std::runtime_error("No sector sepcified in worldmap file.");
-
+
lisp::ListIterator iter(sector);
while(iter.next()) {
if(iter.item() == "tilemap") {
Teleporter* teleporter = new Teleporter(iter.lisp());
teleporters.push_back(teleporter);
add_object(teleporter);
+ } else if(iter.item() == "ambient-light") {
+ std::vector<float> vColor;
+ sector->get_vector( "ambient-light", vColor );
+ if(vColor.size() < 3) {
+ log_warning << "(ambient-light) requires a color as argument" << std::endl;
+ } else {
+ ambient_light = Color( vColor );
+ }
} else if(iter.item() == "name") {
// skip
} else {
if(solids == 0)
throw std::runtime_error("No solid tilemap specified");
- move_to_spawnpoint("main");
+ move_to_spawnpoint("main");
} catch(std::exception& e) {
std::stringstream msg;
try {
lisp::Parser parser;
- std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
+ std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.get_name()));
const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
if(!level_lisp)
return;
-
+
level_lisp->get("name", level.title);
} catch(std::exception& e) {
log_warning << "Problem when reading leveltitle: " << e.what() << std::endl;
calculate_total_stats();
save_state();
- if(World::current() != NULL)
- World::current()->save_state();
- if (old_level_state != level->solved && level->auto_path) {
+ if (old_level_state != level->solved) {
// Try to detect the next direction to which we should walk
// FIXME: Mostly a hack
Direction dir = D_NONE;
-
+
const Tile* tile = at(tux->get_tile_pos());
+ int dirdata = tile->getData() & Tile::WORLDMAP_DIR_MASK;
// first, test for crossroads
- if (tile->getData() & Tile::WORLDMAP_CNSE || tile->getData() && Tile::WORLDMAP_CNSW
- || tile->getData() & Tile::WORLDMAP_CNEW || tile->getData() && Tile::WORLDMAP_CSEW
- || tile->getData() & Tile::WORLDMAP_CNSEW)
+ if (dirdata == Tile::WORLDMAP_CNSE ||
+ dirdata == Tile::WORLDMAP_CNSW ||
+ dirdata == Tile::WORLDMAP_CNEW ||
+ dirdata == Tile::WORLDMAP_CSEW ||
+ dirdata == Tile::WORLDMAP_CNSEW)
dir = D_NONE;
- else if (tile->getData() & Tile::WORLDMAP_NORTH
+ else if (dirdata & Tile::WORLDMAP_NORTH
&& tux->back_direction != D_NORTH)
dir = D_NORTH;
- else if (tile->getData() & Tile::WORLDMAP_SOUTH
+ else if (dirdata & Tile::WORLDMAP_SOUTH
&& tux->back_direction != D_SOUTH)
dir = D_SOUTH;
- else if (tile->getData() & Tile::WORLDMAP_EAST
+ else if (dirdata & Tile::WORLDMAP_EAST
&& tux->back_direction != D_EAST)
dir = D_EAST;
- else if (tile->getData() & Tile::WORLDMAP_WEST
+ else if (dirdata & Tile::WORLDMAP_WEST
&& tux->back_direction != D_WEST)
dir = D_WEST;
void
WorldMap::update(float delta)
{
- Menu* menu = Menu::current();
- if(menu != NULL) {
- menu->update();
+ if(!in_level) {
+ Menu* menu = Menu::current();
+ if(menu != NULL) {
+ menu->update();
- if(menu == worldmap_menu.get()) {
- switch (worldmap_menu->check())
- {
- case MNID_RETURNWORLDMAP: // Return to game
- Menu::set_current(0);
- break;
- case MNID_QUITWORLDMAP: // Quit Worldmap
- main_loop->exit_screen();
- break;
+ if(menu == worldmap_menu.get()) {
+ switch (worldmap_menu->check())
+ {
+ case MNID_RETURNWORLDMAP: // Return to game
+ Menu::set_current(0);
+ break;
+ case MNID_QUITWORLDMAP: // Quit Worldmap
+ main_loop->exit_screen();
+ break;
+ }
}
+
+ return;
}
- return;
- }
+ // update GameObjects
+ for(size_t i = 0; i < game_objects.size(); ++i) {
+ GameObject* object = game_objects[i];
+ object->update(delta);
+ }
- // update GameObjects
- for(size_t i = 0; i < game_objects.size(); ++i) {
- GameObject* object = game_objects[i];
- object->update(delta);
- }
+ // remove old GameObjects
+ for(GameObjects::iterator i = game_objects.begin();
+ i != game_objects.end(); ) {
+ GameObject* object = *i;
+ if(!object->is_valid()) {
+ object->unref();
+ i = game_objects.erase(i);
+ } else {
+ ++i;
+ }
+ }
- // remove old GameObjects
- for(GameObjects::iterator i = game_objects.begin();
- i != game_objects.end(); ) {
- GameObject* object = *i;
- if(!object->is_valid()) {
- object->unref();
- i = game_objects.erase(i);
- } else {
- ++i;
+ // position "camera"
+ Vector tux_pos = tux->get_pos();
+ camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
+ camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
+
+ if (camera_offset.x < 0)
+ camera_offset.x = 0;
+ if (camera_offset.y < 0)
+ camera_offset.y = 0;
+
+ if (camera_offset.x > (int)solids->get_width()*32 - SCREEN_WIDTH)
+ camera_offset.x = (int)solids->get_width()*32 - SCREEN_WIDTH;
+ if (camera_offset.y > (int)solids->get_height()*32 - SCREEN_HEIGHT)
+ camera_offset.y = (int)solids->get_height()*32 - SCREEN_HEIGHT;
+
+ if (int(solids->get_width()*32) < SCREEN_WIDTH)
+ camera_offset.x = solids->get_width()*16.0 - SCREEN_WIDTH/2.0;
+ if (int(solids->get_height()*32) < SCREEN_HEIGHT)
+ camera_offset.y = solids->get_height()*16.0 - SCREEN_HEIGHT/2.0;
+
+ // handle input
+ bool enter_level = false;
+ if(main_controller->pressed(Controller::ACTION)
+ || main_controller->pressed(Controller::JUMP)
+ || main_controller->pressed(Controller::MENU_SELECT)) {
+ /* some people define UP and JUMP on the same key... */
+ if(!main_controller->pressed(Controller::UP))
+ enter_level = true;
+ }
+ if(main_controller->pressed(Controller::PAUSE_MENU))
+ on_escape_press();
+
+ // check for teleporters
+ Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
+ if (teleporter && (teleporter->automatic || (enter_level && (!tux->is_moving())))) {
+ enter_level = false;
+ if (teleporter->worldmap != "") {
+ change(teleporter->worldmap, teleporter->spawnpoint);
+ } else {
+ // TODO: an animation, camera scrolling or a fading would be a nice touch
+ sound_manager->play("sounds/warp.wav");
+ tux->back_direction = D_NONE;
+ move_to_spawnpoint(teleporter->spawnpoint);
+ }
}
- }
- // position "camera"
- Vector tux_pos = tux->get_pos();
- camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
- camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
-
- if (camera_offset.x < 0)
- camera_offset.x = 0;
- if (camera_offset.y < 0)
- camera_offset.y = 0;
-
- if (camera_offset.x > solids->get_width()*32 - SCREEN_WIDTH)
- camera_offset.x = solids->get_width()*32 - SCREEN_WIDTH;
- if (camera_offset.y > solids->get_height()*32 - SCREEN_HEIGHT)
- camera_offset.y = solids->get_height()*32 - SCREEN_HEIGHT;
-
- // handle input
- bool enter_level = false;
- if(main_controller->pressed(Controller::ACTION)
- || main_controller->pressed(Controller::JUMP)
- || main_controller->pressed(Controller::MENU_SELECT))
- enter_level = true;
- if(main_controller->pressed(Controller::PAUSE_MENU))
- on_escape_press();
-
- if (enter_level && !tux->is_moving())
- {
- /* Check special tile action */
- SpecialTile* special_tile = at_special_tile();
- if(special_tile)
- {
- if (special_tile->teleport_dest != Vector(-1,-1))
- {
- // TODO: an animation, camera scrolling or a fading would be a nice touch
- sound_manager->play("sounds/warp.wav");
- tux->back_direction = D_NONE;
- tux->set_tile_pos(special_tile->teleport_dest);
- SDL_Delay(1000);
+ // check for auto-play levels
+ LevelTile* level = at_level();
+ if (level && (level->auto_play) && (!level->solved) && (!tux->is_moving())) {
+ enter_level = true;
+ }
+
+ if (enter_level && !tux->is_moving())
+ {
+ /* Check level action */
+ LevelTile* level = at_level();
+ if (!level) {
+ //Respawn if player on a tile with no level and nowhere to go.
+ const Tile* tile = at(tux->get_tile_pos());
+ if(!( tile->getData() & ( Tile::WORLDMAP_NORTH | Tile::WORLDMAP_SOUTH | Tile::WORLDMAP_WEST | Tile::WORLDMAP_EAST ))){
+ log_warning << "Player at illegal position " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << " respawning." << std::endl;
+ move_to_spawnpoint("main");
+ return;
}
+ log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
+ return;
}
- /* Check level action */
- LevelTile* level = at_level();
- if (!level) {
- log_warning << "No level to enter at: " << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
- return;
- }
+ if (level->pos == tux->get_tile_pos()) {
+ try {
+ Vector shrinkpos = Vector(level->pos.x*32 + 16 - camera_offset.x,
+ level->pos.y*32 + 16 - camera_offset.y);
+ std::string levelfile = levels_path + level->get_name();
- if (level->pos == tux->get_tile_pos()) {
- try {
- Vector shrinkpos = Vector(level->pos.x*32 + 16 - camera_offset.x,
- level->pos.y*32 + 16 - camera_offset.y);
- std::string levelfile = levels_path + level->name;
- main_loop->push_screen(new GameSession(levelfile, &level->statistics),
- new ShrinkFade(shrinkpos, 0.5));
- } catch(std::exception& e) {
- log_fatal << "Couldn't load level: " << e.what() << std::endl;
+ // update state and savegame
+ save_state();
+
+ main_loop->push_screen(new GameSession(levelfile, &level->statistics),
+ new ShrinkFade(shrinkpos, 0.5));
+ in_level = true;
+ } catch(std::exception& e) {
+ log_fatal << "Couldn't load level: " << e.what() << std::endl;
+ }
}
}
- }
- else
- {
-// tux->set_direction(input_direction);
- }
+ else
+ {
+ // tux->set_direction(input_direction);
+ }
+ }
}
const Tile*
i != special_tiles.end(); ++i) {
SpecialTile* special_tile = *i;
if (special_tile->pos == tux->get_tile_pos())
- return special_tile;
+ return special_tile;
}
return NULL;
void
WorldMap::draw(DrawingContext& context)
{
+ if (int(solids->get_width()*32) < SCREEN_WIDTH || int(solids->get_height()*32) < SCREEN_HEIGHT)
+ context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
+ Color(0.0f, 0.0f, 0.0f, 1.0f), LAYER_BACKGROUND0);
+
+ context.set_ambient_color( ambient_light );
context.push_transform();
context.set_translation(camera_offset);
-
+
for(GameObjects::iterator i = game_objects.begin();
i != game_objects.end(); ++i) {
GameObject* object = *i;
object->draw(context);
}
-
+
draw_status(context);
context.pop_transform();
}
{
context.push_transform();
context.set_translation(Vector(0, 0));
-
+
player_status->draw(context);
if (!tux->is_moving()) {
for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
LevelTile* level = *i;
-
+
if (level->pos == tux->get_tile_pos()) {
if(level->title == "")
get_level_title(*level);
Vector(SCREEN_WIDTH/2,
SCREEN_HEIGHT - white_text->get_height() - 30),
CENTER_ALLIGN, LAYER_FOREGROUND1);
-
+
// if level is solved, draw level picture behind stats
/*
if (level->solved) {
}
}
*/
-
+
level->statistics.draw_worldmap_info(context);
break;
}
for(SpecialTiles::iterator i = special_tiles.begin();
i != special_tiles.end(); ++i) {
SpecialTile* special_tile = *i;
-
+
if (special_tile->pos == tux->get_tile_pos()) {
/* Display an in-map message in the map, if any as been selected */
if(!special_tile->map_message.empty() && !special_tile->passive_message)
- context.draw_text(gold_text, special_tile->map_message,
+ context.draw_text(gold_text, special_tile->map_message,
Vector(SCREEN_WIDTH/2,
SCREEN_HEIGHT - white_text->get_height() - 60),
CENTER_ALLIGN, LAYER_FOREGROUND1);
break;
}
}
+
+ // display teleporter messages
+ Teleporter* teleporter = at_teleporter(tux->get_tile_pos());
+ if (teleporter && (teleporter->message != "")) {
+ Vector pos = Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 30);
+ context.draw_text(white_text, teleporter->message, pos, CENTER_ALLIGN, LAYER_FOREGROUND1);
+ }
+
}
-
+
/* Display a passive message in the map, if needed */
if(passive_message_timer.started())
- context.draw_text(gold_text, passive_message,
+ context.draw_text(gold_text, passive_message,
Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
CENTER_ALLIGN, LAYER_FOREGROUND1);
current_ = this;
load_state();
- // if force_spawnpoint was set, move Tux there
- if (force_spawnpoint != "") move_to_spawnpoint(force_spawnpoint);
+ // if force_spawnpoint was set, move Tux there, then clear force_spawnpoint
+ if (force_spawnpoint != "") {
+ move_to_spawnpoint(force_spawnpoint);
+ force_spawnpoint = "";
+ }
+
+ tux->setup();
// register worldmap_table as worldmap in scripting
using namespace Scripting;
-
+
sq_pushroottable(global_vm);
sq_pushstring(global_vm, "worldmap", -1);
sq_pushobject(global_vm, worldmap_table);
msg << "Couldn't get float value for '" << name << "' from table";
throw Scripting::SquirrelError(vm, msg.str());
}
-
+
float result;
if(SQ_FAILED(sq_getfloat(vm, -1, &result))) {
std::ostringstream msg;
msg << "Couldn't get string value for '" << name << "' from table";
throw Scripting::SquirrelError(vm, msg.str());
}
-
+
const char* result;
if(SQ_FAILED(sq_getstring(vm, -1, &result))) {
std::ostringstream msg;
std::ostringstream msg;
msg << "Couldn't get bool value for '" << name << "' from table";
throw Scripting::SquirrelError(vm, msg.str());
- }
-
+ }
+
SQBool result;
if(SQ_FAILED(sq_getbool(vm, -1, &result))) {
std::ostringstream msg;
WorldMap::save_state()
{
using namespace Scripting;
-
+
HSQUIRRELVM vm = global_vm;
int oldtop = sq_gettop(vm);
if(SQ_FAILED(sq_get(vm, -2)))
throw Scripting::SquirrelError(vm, "Couldn't create.get state.worlds");
}
-
+
sq_pushstring(vm, map_filename.c_str(), map_filename.length());
if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse)))
sq_pop(vm, 1);
// store tux
sq_pushstring(vm, "tux", -1);
sq_newtable(vm);
-
+
store_float(vm, "x", tux->get_tile_pos().x);
store_float(vm, "y", tux->get_tile_pos().y);
store_string(vm, "back", direction_to_string(tux->back_direction));
sq_createslot(vm, -3);
-
+
// levels...
sq_pushstring(vm, "levels", -1);
sq_newtable(vm);
for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
LevelTile* level = *i;
-
- if (level->solved) {
- sq_pushstring(vm, level->name.c_str(), -1);
- sq_newtable(vm);
- store_bool(vm, "solved", true);
- // TODO write statistics
- // i->statistics.write(writer);
+ sq_pushstring(vm, level->get_name().c_str(), -1);
+ sq_newtable(vm);
- sq_createslot(vm, -3);
- }
+ store_bool(vm, "solved", level->solved);
+ // TODO write statistics
+ // i->statistics.write(writer);
+
+ sq_createslot(vm, -3);
}
-
+
sq_createslot(vm, -3);
// push world into worlds table
sq_createslot(vm, -3);
- } catch(std::exception& e) {
+ } catch(std::exception& ) {
sq_settop(vm, oldtop);
}
sq_settop(vm, oldtop);
+
+ if(World::current() != NULL)
+ World::current()->save_state();
}
void
WorldMap::load_state()
{
using namespace Scripting;
-
+
HSQUIRRELVM vm = global_vm;
int oldtop = sq_gettop(vm);
-
+
try {
// get state table
sq_pushroottable(vm);
// get table for our world
sq_pushstring(vm, map_filename.c_str(), map_filename.length());
if(SQ_FAILED(sq_get(vm, -2)))
- throw Scripting::SquirrelError(vm, "Couldn't get state.world.mapfilename");
+ throw Scripting::SquirrelError(vm, "Couldn't get state.worlds.mapfilename");
// load tux
sq_pushstring(vm, "tux", -1);
for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
LevelTile* level = *i;
- sq_pushstring(vm, level->name.c_str(), -1);
+ sq_pushstring(vm, level->get_name().c_str(), -1);
if(SQ_SUCCEEDED(sq_get(vm, -2))) {
level->solved = read_bool(vm, "solved");
level->sprite->set_action(level->solved ? "solved" : "default");
log_debug << "Not loading worldmap state: " << e.what() << std::endl;
}
sq_settop(vm, oldtop);
+
+ in_level = false;
}
size_t
size_t count = 0;
for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
LevelTile* level = *i;
-
+
if(level->solved)
count++;
}
return vm;
}
-
+
} // namespace WorldMapNS