+Tux::Tux(WorldMap* worldmap_)
+ : worldmap(worldmap_)
+{
+ sprite = new Surface(datadir + "/images/worldmap/tux.png", USE_ALPHA);
+ offset = 0;
+ moving = false;
+ tile_pos.x = 5;
+ tile_pos.y = 5;
+ direction = NONE;
+ input_direction = NONE;
+}
+
+void
+Tux::draw(const Point& offset)
+{
+ Point pos = get_pos();
+ sprite->draw(pos.x + offset.x,
+ pos.y + offset.y);
+}
+
+
+Point
+Tux::get_pos()
+{
+ float x = tile_pos.x * 32;
+ float y = tile_pos.y * 32;
+
+ switch(direction)
+ {
+ case WEST:
+ x -= offset - 32;
+ break;
+ case EAST:
+ x += offset - 32;
+ break;
+ case NORTH:
+ y -= offset - 32;
+ break;
+ case SOUTH:
+ y += offset - 32;
+ break;
+ case NONE:
+ break;
+ }
+
+ return Point((int)x, (int)y);
+}
+
+void
+Tux::stop()
+{
+ offset = 0;
+ direction = NONE;
+ moving = false;
+}
+
+void
+Tux::update(float delta)
+{
+ if (!moving)
+ {
+ if (input_direction != NONE)
+ {
+ WorldMap::Level* level = worldmap->at_level();
+
+ // We got a new direction, so lets start walking when possible
+ Point 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
+ {
+ // Let tux walk a few pixels (20 pixel/sec)
+ offset += 20.0f * delta;
+
+ if (offset > 32)
+ { // We reached the next tile, so we check what to do now
+ offset -= 32;
+
+ if (worldmap->at(tile_pos)->stop || worldmap->at_level())
+ {
+ stop();
+ }
+ else
+ {
+ // Walk automatically to the next tile
+ Point next_tile;
+ if (worldmap->path_ok(direction, tile_pos, &next_tile))
+ {
+ tile_pos = next_tile;
+ }
+ else
+ {
+ puts("Tilemap data is buggy");
+ stop();
+ }
+ }
+ }
+ }
+}
+