/** * Center map on a certain position. * @param mapPos Position to center on. * @param redraw Redraw map or not. */ void Camera::centerOnPosition(const Position &mapPos, bool redraw) { Position screenPos; _center = mapPos; minMaxInt(&_center.x, -1, _mapLength); minMaxInt(&_center.y, -1, _mapWidth); convertMapToScreen(_center, &screenPos); _mapOffset.x = -(screenPos.x - (_screenWidth / 2)); _mapOffset.y = -(screenPos.y - (_visibleMapHeight / 2)); _mapOffset.z = _center.z; if (redraw) _map->draw(); }
/** * Converts screen coordinates to map coordinates. * @param screenX screen x position * @param screenY screen y position * @param mapX map x position * @param mapY map y position */ void Camera::convertScreenToMap(int screenX, int screenY, int *mapX, int *mapY) const { // add half a tileheight to the mouseposition per layer we are above the floor screenY += (-_spriteWidth/2) + (_mapOffset.z) * ((_spriteHeight + _spriteWidth / 4) / 2); // calculate the actual x/y pixelposition on a diamond shaped map // taking the view offset into account *mapY = - screenX + _mapOffset.x + 2 * screenY - 2 * _mapOffset.y; *mapX = screenY - _mapOffset.y - *mapY / 4 - (_spriteWidth/4); // to get the row&col itself, divide by the size of a tile *mapX /= (_spriteWidth / 4); *mapY /= _spriteWidth; minMaxInt(mapX, -1, _mapLength); minMaxInt(mapY, -1, _mapWidth); }
/** * Set viewheight. * @param viewheight */ void Camera::setViewHeight(int viewheight) { _mapOffset.z = viewheight; minMaxInt(&_mapOffset.z, 0, _mapHeight-1); _map->draw(); }
/** * Sets the view level. * @param viewlevel New view level. */ void Camera::setViewLevel(int viewlevel) { _mapOffset.z = viewlevel; minMaxInt(&_mapOffset.z, 0, _mapsize_z-1); _map->draw(); }