#include "main.hpp"
#include "spawn_point.hpp"
#include "file_system.hpp"
+#include "tile_manager.hpp"
+#include "tile_set.hpp"
#include "gui/menu.hpp"
#include "gui/mousecursor.hpp"
#include "control/joystickkeyboardcontroller.hpp"
#include "worldmap/tux.hpp"
#include "worldmap/sprite_change.hpp"
+static const float CAMERA_PAN_SPEED = 5.0;
+
namespace WorldMapNS {
enum WorldMapMenuIDs {
//---------------------------------------------------------------------------
WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpoint)
- : tux(0), ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), force_spawnpoint(force_spawnpoint), in_level(false)
+ : tux(0), tileset(NULL), free_tileset(false),
+ ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), force_spawnpoint(force_spawnpoint),
+ in_level(false), panning(false)
{
- tile_manager.reset(new TileManager("images/worldmap.strf"));
-
tux = new Tux(this);
add_object(tux);
sq_addref(global_vm, &worldmap_table);
sq_pop(global_vm, 1);
+
+ sound_manager->preload("sounds/warp.wav");
// load worldmap objects
load(filename);
{
using namespace Scripting;
- save_state();
+ if(free_tileset)
+ delete tileset;
for(GameObjects::iterator i = game_objects.begin();
i != game_objects.end(); ++i) {
}
void
-WorldMap::move_to_spawnpoint(const std::string& spawnpoint)
+WorldMap::move_to_spawnpoint(const std::string& spawnpoint, bool pan)
{
for(SpawnPoints::iterator i = spawn_points.begin(); i != spawn_points.end(); ++i) {
SpawnPoint* sp = *i;
Vector p = sp->pos;
tux->set_tile_pos(p);
tux->set_direction(sp->auto_dir);
+ if(pan) {
+ panning = true;
+ pan_pos = get_camera_pos_for_tux();
+ clamp_camera_position(pan_pos);
+ }
return;
}
}
lisp::Parser parser;
const lisp::Lisp* root = parser.parse(map_filename);
- const lisp::Lisp* lisp = root->get_lisp("supertux-level");
- if(!lisp)
+ const lisp::Lisp* level = root->get_lisp("supertux-level");
+ if(level == NULL)
throw std::runtime_error("file isn't a supertux-level file.");
- lisp->get("name", name);
+ level->get("name", name);
- const lisp::Lisp* sector = lisp->get_lisp("sector");
+ const lisp::Lisp* sector = level->get_lisp("sector");
if(!sector)
throw std::runtime_error("No sector sepcified in worldmap file.");
+ const lisp::Lisp* tilesets_lisp = level->get_lisp("tilesets");
+ if(tilesets_lisp != NULL) {
+ tileset = tile_manager->parse_tileset_definition(*tilesets_lisp);
+ free_tileset = true;
+ }
+ std::string tileset_name;
+ if(level->get("tileset", tileset_name)) {
+ if(tileset != NULL) {
+ log_warning << "multiple tilesets specified in level" << std::endl;
+ } else {
+ tileset = tile_manager->get_tileset(tileset_name);
+ }
+ }
+ /* load default tileset */
+ if(tileset == NULL) {
+ tileset = tile_manager->get_tileset("images/worldmap.strf");
+ }
+ current_tileset = tileset;
+
lisp::ListIterator iter(sector);
while(iter.next()) {
if(iter.item() == "tilemap") {
- add_object(new TileMap(*(iter.lisp()), tile_manager.get()));
+ add_object(new TileMap(*(iter.lisp())));
} else if(iter.item() == "background") {
add_object(new Background(*(iter.lisp())));
} else if(iter.item() == "music") {
log_warning << "Unknown token '" << iter.item() << "' in worldmap" << std::endl;
}
}
+ current_tileset = NULL;
+
if(solid_tilemaps.size() == 0)
throw std::runtime_error("No solid tilemap specified");
void WorldMap::calculate_total_stats()
{
- total_stats.reset();
+ total_stats.zero();
for(LevelTiles::iterator i = levels.begin(); i != levels.end(); ++i) {
LevelTile* level = *i;
if (level->solved) {
}
}
+Vector
+WorldMap::get_camera_pos_for_tux() {
+ Vector camera_offset;
+ Vector tux_pos = tux->get_pos();
+ camera_offset.x = tux_pos.x - SCREEN_WIDTH/2;
+ camera_offset.y = tux_pos.y - SCREEN_HEIGHT/2;
+ return camera_offset;
+}
+
+void
+WorldMap::clamp_camera_position(Vector& c) {
+ if (c.x < 0)
+ c.x = 0;
+ if (c.y < 0)
+ c.y = 0;
+
+ if (c.x > (int)get_width()*32 - SCREEN_WIDTH)
+ c.x = (int)get_width()*32 - SCREEN_WIDTH;
+ if (c.y > (int)get_height()*32 - SCREEN_HEIGHT)
+ c.y = (int)get_height()*32 - SCREEN_HEIGHT;
+
+ if (int(get_width()*32) < SCREEN_WIDTH)
+ c.x = get_width()*16.0 - SCREEN_WIDTH/2.0;
+ if (int(get_height()*32) < SCREEN_HEIGHT)
+ c.y = get_height()*16.0 - SCREEN_HEIGHT/2.0;
+}
+
void
WorldMap::update(float delta)
{
// update GameObjects
for(size_t i = 0; i < game_objects.size(); ++i) {
GameObject* object = game_objects[i];
- object->update(delta);
+ if(!panning || object != tux) {
+ object->update(delta);
+ }
}
// remove old GameObjects
if (tm->is_solid()) solid_tilemaps.push_back(tm);
}
- // 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;
+ Vector requested_pos;
- if (camera_offset.x < 0)
- camera_offset.x = 0;
- if (camera_offset.y < 0)
- camera_offset.y = 0;
+ // position "camera"
+ if(!panning) {
+ camera_offset = get_camera_pos_for_tux();
+ } else {
+ Vector delta = pan_pos - camera_offset;
+ float mag = delta.norm();
+ if(mag > CAMERA_PAN_SPEED) {
+ delta *= CAMERA_PAN_SPEED/mag;
+ }
+ camera_offset += delta;
+ if(camera_offset == pan_pos) {
+ panning = false;
+ }
+ }
- if (camera_offset.x > (int)get_width()*32 - SCREEN_WIDTH)
- camera_offset.x = (int)get_width()*32 - SCREEN_WIDTH;
- if (camera_offset.y > (int)get_height()*32 - SCREEN_HEIGHT)
- camera_offset.y = (int)get_height()*32 - SCREEN_HEIGHT;
+ requested_pos = camera_offset;
+ clamp_camera_position(camera_offset);
- if (int(get_width()*32) < SCREEN_WIDTH)
- camera_offset.x = get_width()*16.0 - SCREEN_WIDTH/2.0;
- if (int(get_height()*32) < SCREEN_HEIGHT)
- camera_offset.y = get_height()*16.0 - SCREEN_HEIGHT/2.0;
+ if(panning) {
+ if(requested_pos.x != camera_offset.x) {
+ pan_pos.x = camera_offset.x;
+ }
+ if(requested_pos.y != camera_offset.y) {
+ pan_pos.y = camera_offset.y;
+ }
+ }
// handle input
bool enter_level = false;
// 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);
+ move_to_spawnpoint(teleporter->spawnpoint, true);
}
}
for(GameObjects::iterator i = game_objects.begin();
i != game_objects.end(); ++i) {
GameObject* object = *i;
- object->draw(context);
+ if(!panning || object != tux) {
+ object->draw(context);
+ }
}
/*
void
WorldMap::leave()
{
- // remove worldmap_table from roottable
using namespace Scripting;
+ // save state of world and player
+ save_state();
+
+ // remove worldmap_table from roottable
sq_pushroottable(global_vm);
sq_pushstring(global_vm, "worldmap", -1);
if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
sq_pop(global_vm, 1);
}
-static void store_float(HSQUIRRELVM vm, const char* name, float val)
-{
- sq_pushstring(vm, name, -1);
- sq_pushfloat(vm, val);
- if(SQ_FAILED(sq_createslot(vm, -3)))
- throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
-}
-
-/*
-static void store_int(HSQUIRRELVM vm, const char* name, int val)
-{
- sq_pushstring(vm, name, -1);
- sq_pushinteger(vm, val);
- if(SQ_FAILED(sq_createslot(vm, -3)))
- throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
-}
-*/
-
-static void store_string(HSQUIRRELVM vm, const char* name, const std::string& val)
-{
- sq_pushstring(vm, name, -1);
- sq_pushstring(vm, val.c_str(), val.length());
- if(SQ_FAILED(sq_createslot(vm, -3)))
- throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
-}
-
-static void store_bool(HSQUIRRELVM vm, const char* name, bool val)
-{
- sq_pushstring(vm, name, -1);
- sq_pushbool(vm, val ? SQTrue : SQFalse);
- if(SQ_FAILED(sq_createslot(vm, -3)))
- throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
-}
-
-static float read_float(HSQUIRRELVM vm, const char* name)
-{
- sq_pushstring(vm, name, -1);
- if(SQ_FAILED(sq_get(vm, -2))) {
- std::ostringstream msg;
- 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 float value for '" << name << "' from table";
- throw Scripting::SquirrelError(vm, msg.str());
- }
- sq_pop(vm, 1);
-
- return result;
-}
-
-static std::string read_string(HSQUIRRELVM vm, const char* name)
-{
- sq_pushstring(vm, name, -1);
- if(SQ_FAILED(sq_get(vm, -2))) {
- 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;
- msg << "Couldn't get string value for '" << name << "' from table";
- throw Scripting::SquirrelError(vm, msg.str());
- }
- sq_pop(vm, 1);
-
- return std::string(result);
-}
-
-static bool read_bool(HSQUIRRELVM vm, const char* name)
-{
- sq_pushstring(vm, name, -1);
- if(SQ_FAILED(sq_get(vm, -2))) {
- 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;
- msg << "Couldn't get bool value for '" << name << "' from table";
- throw Scripting::SquirrelError(vm, msg.str());
- }
- sq_pop(vm, 1);
-
- return result == SQTrue;
-}
-
void
WorldMap::save_state()
{
sq_newtable(vm);
store_bool(vm, "solved", level->solved);
- // TODO write statistics
- // i->statistics.write(writer);
+ level->statistics.serialize_to_squirrel(vm);
sq_createslot(vm, -3);
}
sq_createslot(vm, -3);
+ // overall statistics...
+ total_stats.serialize_to_squirrel(vm);
+
// push world into worlds table
sq_createslot(vm, -3);
} catch(std::exception& ) {
if(SQ_SUCCEEDED(sq_get(vm, -2))) {
level->solved = read_bool(vm, "solved");
level->sprite->set_action(level->solved ? "solved" : "default");
- // i->statistics.parse(*level);
+ level->statistics.unserialize_from_squirrel(vm);
sq_pop(vm, 1);
}
}
+
+ // leave state table
sq_pop(vm, 1);
+ // load overall statistics
+ total_stats.unserialize_from_squirrel(vm);
+
} catch(std::exception& e) {
log_debug << "Not loading worldmap state: " << e.what() << std::endl;
}