// Checks is the given movement will result in a collision.
bool Player::CausesCollision(sf::Vector2f movement, Level& level)
{
	// Get the tiles that the four corners other player are overlapping with.
	Tile* overlappingTiles[4];
	sf::Vector2f newPosition = m_position + movement;

	// Top left.
	overlappingTiles[0] = level.GetTile(sf::Vector2f(newPosition.x - 14.f, newPosition.y - 14.f));

	// Top right.
	overlappingTiles[1] = level.GetTile(sf::Vector2f(newPosition.x + 14.f, newPosition.y - 14.f));

	// Bottom left.
	overlappingTiles[2] = level.GetTile(sf::Vector2f(newPosition.x - 14.f, newPosition.y + 14.f));

	// Bottom right.
	overlappingTiles[3] = level.GetTile(sf::Vector2f(newPosition.x + 14.f, newPosition.y + 14.f));

	// If any of the overlapping tiles are solid there was a collision.
	for (int i = 0; i < 4; i++)
	{
		if (level.IsSolid(overlappingTiles[i]->columnIndex, overlappingTiles[i]->rowIndex))
			return true;
	}

	// If we've not returned yet no collisions were found.
	return false;
}
Ejemplo n.º 2
0
Vec2 FindTargetPos(Vec2 bot_pos, Vec2 radar_coord, const ScreenGrabberPtr& screen, const ScreenAreaPtr& radar, const Level& level, int mapzoom) {
    float per_pix = GetRadarPerPixel(radar, mapzoom);
    Vec2 rpos = GetBotRadarPos(bot_pos, radar, mapzoom);

    double rdx = -(rpos.x - radar_coord.x);
    double rdy = -(rpos.y - radar_coord.y);

    Vec2 target(bot_pos.x + rdx * per_pix, bot_pos.y + rdy * per_pix);

    if (level.IsSolid((int)target.x, (int)target.y)) {
        for (const Vec2& dir : directions) {
            if (!level.IsSolid((int)(target.x + dir.x), (int)(target.y + dir.y))) {
                target = Vec2(target.x + dir.x, target.y + dir.y);
                break;
            }
        }
    }

    return target;
}
Ejemplo n.º 3
0
bool FitsOnMap(int x, int y, int radius, const Level& level) {
    int startTileX = (x - radius) >> 4;
    int endTileX = (x + radius) >> 4;

    int startTileY = (y - radius) >> 4;
    int endTileY = (y + radius) >> 4;

    for (int x_ = startTileX; x_ <= endTileX; ++x_) {
        for (int y_ = startTileY; y_ <= endTileY; ++y_) {
            if (level.IsSolid(x_, y_))
                return false;
        }
    }

    return true;
}