Program Listing for File World.hpp

Return to documentation for file (engine/include/Cacao/World.hpp)

#pragma once

#include "Cacao/Cubemap.hpp"
#include "DllHelper.hpp"
#include "Camera.hpp"
#include "Actor.hpp"
#include "Resource.hpp"

#include "libcacaoformats.hpp"

#include <memory>

namespace Cacao {
    class CACAO_API World : public Resource {
      public:
        static std::shared_ptr<World> Create(libcacaoformats::World&& world, const std::string& addr) {
            return std::shared_ptr<World>(new World(std::move(world), addr));
        }

        static std::shared_ptr<World> Create(const std::string& addr) {
            libcacaoformats::World w;
            return Create(std::move(w), addr);
        }

        std::shared_ptr<Camera> cam;

        std::shared_ptr<Cubemap> skyboxTex;

        void ReparentToRoot(ActorHandle actor);

        std::vector<ActorHandle> GetRootChildren() const;

        template<typename P>
            requires std::is_invocable_r_v<bool, P, ActorHandle>
        std::optional<ActorHandle> FindActor(P predicate) const {
            //Search for the object
            return actorSearchRunner(root->GetAllChildren(), predicate);
        }

        ~World();

      private:
        World(libcacaoformats::World&& world, const std::string& addr);

        ActorHandle root;

        //Recursive function for actually running a actor search
        template<typename P>
        std::optional<ActorHandle> actorSearchRunner(std::vector<ActorHandle> target, P predicate) const {
            //Iterate through all children
            for(auto child : target) {
                //Does this child pass the predicate?
                if(predicate(child)) {
                    return std::optional<ActorHandle>(child);
                }

                //Search through children
                std::optional<ActorHandle> found = actorSearchRunner(child->GetAllChildren(), predicate);
                if(found.has_value()) {
                    return found;
                }
            }
            //Nothing was found, return null option
            return std::nullopt;
        }

        friend class ResourceManager;
        friend class Actor;
    };
}