Skip to content

scene_loader::load_scene()

Paweł Waligóra edited this page May 22, 2024 · 3 revisions

Code

void scene_loader::load_scene(const std::string& file_name) {
    //un load previous scene
    un_load_scene();

    // load new scene from file
    std::ifstream file(file_name); // open file
    nlohmann::json json;
    file >> json;
    for (auto& entry : json["scripts"])
    {
        // create script instance
        nlohmann::json args = entry["args"];
        if (entry["type"] == "gameplay_manager") { scripts_system::scripts.push_back(new game::gameplay_manager()); }
        else if (entry["type"] == "player") { scripts_system::scripts.push_back(new game::player(glm::vec3(args["x"], args["y"], args["z"]), args["rot_y"])); }
        else if (entry["type"] == "fly_cam") { scripts_system::scripts.push_back(new game::fly_cam()); }
        else if (entry["type"] == "wall") { scripts_system::scripts.push_back(new game::wall(
            glm::vec3(args["position"]["x"], args["position"]["y"], args["position"]["z"]), 
            glm::vec3(args["rotation"]["x"], args["rotation"]["y"], args["rotation"]["z"]), 
            glm::vec3(args["size"]["x"], args["size"]["y"], args["size"]["z"]))); }

        else if (entry["type"] == "collision_test_script") { scripts_system::scripts.push_back(new physics::collision_test_script()); }

        // name the script
        scripts_system::scripts.back()->name = entry["name"];
    }
    file.close(); // close file
    printf("===scene_loaded===\n");
    scripts_system::call_events(SCRIPTS_START);
}

Description

Function un_load currently loaded scene and loads a new one from file.

Can be used in a script. Simply call: scene_loader::load_scene("scene-name.json"); and that's it.

file_name is a path to a file. The rules of how file should look are presented on scene_file page.

Home

Git Workflow

Issues

Coding Rules

Skrypty-Tutorial

Useful resources

Game Structure

Code Structure

Clone this wiki locally