Esempio n. 1
0
    void StageSelectStateConfig::extractValuesFromJson(const Json::Value & config) {
        if(!config.isNull()) {

            musicName = config.get("music", "None").asString();
            backgroundImage = config.get("background", "None").asString();
            foregroundImage = config.get("foreground", "None").asString();

            // Extract portraits
            portraits = extractPortraits(config.get("portraits", Json::Value()));

            // Extract sprites
            if(config.isMember("sprites")) {
                const auto & spritesJson = config.get("sprites", Json::Value());

                if(spritesJson.isMember("cursor")) {
                    const auto & cursorSpriteJson = spritesJson.get("cursor", Json::Value());

                    cursorInfo.first = cursorSpriteJson.get("animationSet", "").asString();
                    cursorInfo.second = cursorSpriteJson.get("animationName", "").asString();
                }

                if(spritesJson.isMember("eye")) {
                    const auto & eyeSpriteJson = spritesJson.get("eye", Json::Value());

                    eyeInfo.first = eyeSpriteJson.get("animationSet", "").asString();
                    eyeInfo.second = eyeSpriteJson.get("animationName", "").asString();
                }
            }

            // Extract eye positions
            if(config.isMember("eyePositions")) {
                const auto & positionsJson = config.get("eyePositions", Json::Value());

                for(unsigned int i = 0, length = positionsJson.size(); i < length; ++i) {
                    const auto & positionPairJson = positionsJson[i]; // an array of two objects
                    const auto & positionRightJson = positionPairJson[0u]; // the first object is the right eye
                    const auto & positionLeftJson = positionPairJson[1u]; // the second object is the left eye

                    float rightEyeX = positionRightJson.get("x", 0).asFloat();
                    float rightEyeY = positionRightJson.get("y", 0).asFloat();
                    float leftEyeX = positionLeftJson.get("x", 0).asFloat();
                    float leftEyeY = positionLeftJson.get("y", 0).asFloat();

                    Point2D<float> rightEyePosition(rightEyeX, rightEyeY);
                    Point2D<float> leftEyePosition(leftEyeX, leftEyeY);

                    eyePositions.push_back(std::make_pair(rightEyePosition, leftEyePosition));
                }
            }

            HIKARI_LOG(debug4) << "Found " << portraits.size() << " portrait(s).";
        }
    }
Esempio n. 2
0
    StageSelectState::StageSelectState(const std::string &name, const Json::Value &params, GameController & controller, ServiceLocator &services)
        : name(name)
        , controller(controller)
        , guiService(services.locateService<GuiService>(Services::GUISERVICE))
        , audioService(services.locateService<AudioService>(Services::AUDIO))
        , gameProgress(services.locateService<GameProgress>(Services::GAMEPROGRESS))
        , taskQueue()
        , guiContainer(new gcn::Container())
        , guiSelectedCellLabel(new gcn::Label())
        , guiCursorIcon()
        , guiForeground()
        , guiBackground()
        , guiLeftEye()
        , guiRightEye()
        , cursorRow(DEFAULT_CURSOR_ROW)
        , cursorColumn(DEFAULT_CURSOR_COLUMN)
        , enableCursorMovement(false)
    {
        std::weak_ptr<ImageCache> imageCache = services.locateService<ImageCache>(Services::IMAGECACHE);

        // Load sprites from config
        // TODO: This needs to be refactored to be safer and things like that
        // TODO: Need a utility method to load sf:Sprite from JSON
        if(auto imageCachePtr = imageCache.lock()) {
            background.setTexture(*imageCachePtr->get(params[PROPERTY_BACKGROUND].asString()).get());
            foreground.setTexture(*imageCachePtr->get(params[PROPERTY_FOREGROUND].asString()).get());
            leftEye.setTexture(*imageCachePtr->get(params[PROPERTY_EYE_SPRITE].asString()).get());
            rightEye.setTexture(*imageCachePtr->get(params[PROPERTY_EYE_SPRITE].asString()).get());
        }

        guiCursorIcon.reset(new gcn::Icon(params[PROPERTY_CURSOR_SPRITE].asString()));

        guiCursorIcon->setX(-100);
        guiCursorIcon->setY(-100);

        // Load eye positions
        // There are 9 positions; 18 points total (one for left, one for right eye)
        const Json::Value &jsonEyePositions = params[PROPERTY_EYE_POSITIONS];
        if(static_cast<int>(jsonEyePositions.size()) == NUM_OF_PORTRAITS) {
            for(int i = 0; i < NUM_OF_PORTRAITS; ++i) {
                const Json::Value &jsonEyePosition = jsonEyePositions[i];
                float leftEyeX = static_cast<float>(jsonEyePosition[0u].get(PROPERTY_X, 0).asInt());
                float leftEyeY = static_cast<float>(jsonEyePosition[0u].get(PROPERTY_Y, 0).asInt());
                float rightEyeX = static_cast<float>(jsonEyePosition[1u].get(PROPERTY_X, 0).asInt());
                float rightEyeY = static_cast<float>(jsonEyePosition[1u].get(PROPERTY_Y, 0).asInt());

                Point2D<float> leftEyePosition(leftEyeX, leftEyeY);
                Point2D<float> rightEyePosition(rightEyeX, rightEyeY);

                eyePositions.push_back(std::make_pair(leftEyePosition, rightEyePosition));
            }
        }

        // Load cursor positions
        const Json::Value &jsonCursorPositions = params[PROPERTY_CURSOR_POSITIONS];
        if(static_cast<int>(jsonCursorPositions.size()) == NUM_OF_PORTRAITS) {
            for(int i = 0; i < NUM_OF_PORTRAITS; ++i) {
                const Json::Value &jsonCursorPosition = jsonCursorPositions[i];
                float cursorX = static_cast<float>(jsonCursorPosition.get(PROPERTY_X, 0).asInt());
                float cursorY = static_cast<float>(jsonCursorPosition.get(PROPERTY_Y, 0).asInt());

                Point2D<float> position(cursorX, cursorY);

                cursorPositions.push_back(position);
            }
        }

        buildGui();
    }