namespace WorldMapNS {
+Direction reverse_dir(Direction direction)
+{
+ switch(direction)
+ {
+ case WEST:
+ return EAST;
+ case EAST:
+ return WEST;
+ case NORTH:
+ return SOUTH;
+ case SOUTH:
+ return NORTH;
+ case NONE:
+ return NONE;
+ }
+ return NONE;
+}
+
+std::string
+direction_to_string(Direction direction)
+{
+ switch(direction)
+ {
+ case WEST:
+ return "west";
+ case EAST:
+ return "east";
+ case NORTH:
+ return "north";
+ case SOUTH:
+ return "south";
+ default:
+ return "none";
+ }
+}
+
+Direction
+string_to_direction(const std::string& directory)
+{
+ if (directory == "west")
+ return WEST;
+ else if (directory == "east")
+ return EAST;
+ else if (directory == "north")
+ return NORTH;
+ else if (directory == "south")
+ return SOUTH;
+ else
+ return NONE;
+}
+
TileManager* TileManager::instance_ = 0;
TileManager::TileManager()
if (!moving)
{
if (input_direction != NONE)
- { // We got a new direction, so lets start walking when possible
+ {
+ WorldMap::Level* level = worldmap->at_level();
+
+ // We got a new direction, so lets start walking when possible
Point next_tile;
- if (worldmap->path_ok(input_direction, tile_pos, &next_tile))
+ if ((!level || level->solved)
+ && worldmap->path_ok(input_direction, tile_pos, &next_tile))
{
tile_pos = next_tile;
moving = true;
direction = input_direction;
+ back_direction = reverse_dir(direction);
+ }
+ else if (input_direction == back_direction)
+ {
+ std::cout << "Back triggered" << std::endl;
+ moving = true;
+ direction = input_direction;
+ tile_pos = worldmap->get_next_tile(tile_pos, direction);
+ back_direction = reverse_dir(direction);
}
}
}
}
else
{
+ // Walk automatically to the next tile
Point next_tile;
if (worldmap->path_ok(direction, tile_pos, &next_tile))
{
Level level;
LispReader reader(lisp_cdr(element));
level.solved = false;
+
+ level.north = true;
+ level.east = true;
+ level.south = true;
+ level.west = true;
+
reader.read_string("name", &level.name);
reader.read_int("x", &level.x);
reader.read_int("y", &level.y);
&& p.x < width
&& p.y >= 0
&& p.y < height);
+
return TileManager::instance()->get(tilemap[width * p.y + p.x]);
}
-bool
+WorldMap::Level*
WorldMap::at_level()
{
for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
{
if (i->x == tux->get_tile_pos().x &&
i->y == tux->get_tile_pos().y)
- return true;
+ return &*i;
}
- return false;
+ return 0;
}
<< " (lives " << player_status.lives << ")\n"
<< " (score " << player_status.score << ")\n"
<< " (distros " << player_status.distros << ")\n"
- << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << "))\n"
+ << " (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")"
+ << " (back \"" << direction_to_string(tux->back_direction) << "\"))\n"
<< " (levels\n";
for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
{
std::string name;
bool solved = false;
-
+ std::string back_str ="";
+ Direction back = NONE;
+
LispReader level_reader(data);
level_reader.read_string("name", &name);
level_reader.read_bool("solved", &solved);
+ if (level_reader.read_string("back", &back_str))
+ back = string_to_direction(back_str);
std::cout << "Name: " << name << " " << solved << std::endl;
enum Direction { NONE, WEST, EAST, NORTH, SOUTH };
+std::string direction_to_string(Direction d);
+Direction string_to_direction(const std::string& d);
+Direction reverse_dir(Direction d);
+
class WorldMap;
class Tux
{
+public:
+ Direction back_direction;
private:
WorldMap* worldmap;
Surface* sprite;
int width;
int height;
+public:
struct Level
{
int x;
int y;
std::string name;
bool solved;
+
+ // Directions which are walkable from this level
+ bool north;
+ bool east;
+ bool south;
+ bool west;
};
+private:
typedef std::vector<Level> Levels;
Levels levels;
Point get_next_tile(Point pos, Direction direction);
Tile* at(Point pos);
- bool at_level();
+ WorldMap::Level* at_level();
/** Check if it is possible to walk from \a pos into \a direction,
if possible, write the new position to \a new_pos */