示例#1
0
void Sky::CreateSkyboxNode()
{
    if (world_.Expired() || urhoNode_)
        return;

    Urho3D::Scene* urhoScene = world_->UrhoScene();

    urhoNode_ = urhoScene->CreateChild("Skybox");
    Urho3D::Skybox* skybox = urhoNode_->CreateComponent<Urho3D::Skybox>();

    Urho3D::ResourceCache* cache = GetSubsystem<Urho3D::ResourceCache>();
    skybox->SetModel(cache->GetResource<Urho3D::Model>("Models/Box.mdl"));
}
示例#2
0
void MainGameState::Start() {
    std::cout << "starting main game " << std::endl;
    Urho3D::ResourceCache *cache = GetSubsystem<Urho3D::ResourceCache>();
    // We will be needing to load resources.
    // All the resources used in this example comes with Urho3D.
    // If the engine can't find them, check the ResourcePrefixPath.

    // Seems like the mouse must be in cursor mode before creating the UI or it won't work.

    // Let's use the default style that comes with Urho3D.
    // GetSubsystem<Urho3D::UI>()->GetRoot()->AddChild(texts["FPS_text"]);
    // Let's create some text to display.
    // Add a button, just as an interactive UI sample.
    // Note, must be part of the UI system before SetSize calls!

    GetSubsystem<Urho3D::UI>()->GetRoot()->AddChild(ui_elements["side_bar"]);

    // Now we can change the mouse mode.

    // Let's setup a scene to render.
    scene_ = new Urho3D::Scene(context_);
    // Let the scene have an Octree component!
    scene_->CreateComponent<Urho3D::Octree>();
    // Let's add an additional scene component for fun.
    scene_->CreateComponent<Urho3D::DebugRenderer>();

    // Let's put some sky in there.
    // Again, if the engine can't find these resources you need to check
    // the "ResourcePrefixPath". These files come with Urho3D.
    Urho3D::Node *skyNode = scene_->CreateChild("Sky");
    skyNode->SetScale(500.0f); // The scale actually does not matter
    Urho3D::Skybox *skybox = skyNode->CreateComponent<Urho3D::Skybox>();
    skybox->SetModel(cache->GetResource<Urho3D::Model>("Models/Box.mdl"));
    skybox->SetMaterial(cache->GetResource<Urho3D::Material>("Materials/Skybox.xml"));
    int worldsize = 40;
    Urho3D::SharedPtr<Urho3D::Node> bed_rock(new Urho3D::Node(context_));
    for (int x = -worldsize; x < worldsize; x++) {
        for (int y = -worldsize; y < worldsize; y++) {
            Urho3D::Vector3 pos = Urho3D::Vector3(x, y, 0);
            EntityNode *boxNode_ = new EntityNode(context_, &m_entity_manager);
            bed_rock->AddChild(boxNode_);
            boxNode_->SetPosition(pos);
            Urho3D::StaticModel *boxObject = boxNode_->CreateComponent<Urho3D::StaticModel>();
            boxObject->SetModel(cache->GetResource<Urho3D::Model>("Models/Box.mdl"));

            boxObject->SetMaterial(cache->GetResource<Urho3D::Material>("Materials/Mushroom.xml"));
            boxObject->SetCastShadows(true);
            boxNode_->assign<terrain_type>("Stone", STONE);
        }
    }
    z_levels.push_back(bed_rock);
    for (int z = 1; z <= m_world_height; z++) {

        Urho3D::SharedPtr<Urho3D::Node> cur_level(new Urho3D::Node(context_));
        for (int x = -worldsize; x < worldsize; x++)
            for (int y = -worldsize; y < worldsize; y++) {
                /*  if (sqrt(x * x + y * y) / 30 <
                      20 +
                          4 * sin(5 * sin(6 * (glm::dot(glm::normalize(glm::vec2(x, y)),
                                                        glm::vec2(0, 1))))))           */

                double noise = m_noise_gen.noise_3D(0.02 * x, 0.002 * y, 0.02 * z) +
                               m_noise_gen.noise_3D(x, 4 * y, z);

                std::cout << 2 - ((z) / (float)m_world_height) << " "
                          << m_noise_gen.noise_2D(x, y) + 1 << std::endl;
                if ((z / (float)m_world_height) <
                    0.1 * m_noise_gen.noise_2D(0.1 * x, y * 0.1) + 1) {
                    if (noise > 0.2) {

                        Urho3D::Vector3 pos = Urho3D::Vector3(x, y, z);
                        auto lol = new TerrainFactory(context_, &m_entity_manager,cache );
                    
                        cur_level->AddChild(lol->create_stone_terrain(pos));
                         
                    } else if (true) {
                        Urho3D::Vector3 pos = Urho3D::Vector3(x, y, z);
                        EntityNode *boxNode_ = new EntityNode(context_, &m_entity_manager);
                        cur_level->AddChild(boxNode_);
                        boxNode_->SetPosition(pos);
                        Urho3D::StaticModel *boxObject =
                            boxNode_->CreateComponent<Urho3D::StaticModel>();
                        boxObject->SetModel(cache->GetResource<Urho3D::Model>("Models/Box.mdl"));

                        boxObject->SetMaterial(cache->GetResource<Urho3D::Material>(
                            "Materials/NinjaSnowWar/Snow.xml"));
                        boxObject->SetCastShadows(true);
                        boxNode_->assign<terrain_type>("Stone", STONE);
                    }
                }
            }
        z_levels.push_back(cur_level);
        if (z > m_world_height - m_world_render_depth)
            scene_->AddChild(cur_level);
    }
    std::cout << z_levels.size() << std::endl;
    // We need a camera from which the viewport can render.
    cameraNode_ = scene_->CreateChild("Camera");
    m_camera = cameraNode_->CreateComponent<Urho3D::Camera>();
    m_camera->SetFarClip(2000);
    cameraNode_->Pitch(45);
    cameraNode_->Yaw(180);
    cameraNode_->SetPosition(Urho3D::Vector3(0, 0, m_world_height + m_world_render_depth));

    // Create two lights
    {
        Urho3D::Node *lightNode = scene_->CreateChild("Light");
        lightNode->SetPosition(Urho3D::Vector3(-5, 0, m_world_height + 20));
        Urho3D::Light *light = lightNode->CreateComponent<Urho3D::Light>();
        light->SetLightType(Urho3D::LIGHT_POINT);
        light->SetRange(50);
        light->SetBrightness(1.2);
        light->SetColor(Urho3D::Color(1, .5, .8, 1));
        light->SetCastShadows(true);
    }
    // add one to the camera node as well
    // Now we setup the viewport. Ofcourse, you can have more than one!
    Urho3D::Renderer *renderer = GetSubsystem<Urho3D::Renderer>();
    Urho3D::SharedPtr<Urho3D::Viewport> viewport(
        new Urho3D::Viewport(context_, scene_, cameraNode_->GetComponent<Urho3D::Camera>()));
    renderer->SetViewport(0, viewport);

    // We subscribe to the events we'd like to handle.
    // In this example we will be showing what most of them do,
    // but in reality you would only subscribe to the events
    // you really need to handle.
    // These are sort of subscribed in the order in which the engine
    // would send the events. Read each handler method's comment for
    // details.
    std::cout << "start main done" << std::endl;
}