美文网首页
concepts终极版,一个模拟游戏角色系统的应用

concepts终极版,一个模拟游戏角色系统的应用

作者: FredricZhu | 来源:发表于2022-04-12 13:40 被阅读0次

    本例是concepts应用的终极版,结合std::variant实现一个简易的模拟游戏角色系统的应用。
    程序代码如下,
    CMakeLists.txt

    cmake_minimum_required(VERSION 2.6)
    
    if(APPLE)
        message(STATUS "This is Apple, do nothing.")
        set(CMAKE_MACOSX_RPATH 1)
        set(CMAKE_PREFIX_PATH /Users/aabjfzhu/software/vcpkg/ports/cppwork/vcpkg_installed/x64-osx/share )
    elseif(UNIX)
        message(STATUS "This is linux, set CMAKE_PREFIX_PATH.")
        set(CMAKE_PREFIX_PATH /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/share)
    endif(APPLE)
    
    project(game_engine)
    
    set(CMAKE_CXX_STANDARD 20)
    
    add_definitions(-g)
    
    find_package(ZLIB)
    
    find_package(glog REQUIRED)
    
    find_package(OpenCV REQUIRED )
    
    find_package(OpenSSL REQUIRED)
    
    find_package(restclient-cpp REQUIRED)
    
    find_package(Boost REQUIRED COMPONENTS
        system
        filesystem
        serialization
        program_options
        thread
        )
    
    find_package(DataFrame REQUIRED)
    
    if(APPLE)
        MESSAGE(STATUS "This is APPLE, set INCLUDE_DIRS")
    set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include /usr/local/iODBC/include /opt/snowflake/snowflakeodbc/include/ ${CMAKE_CURRENT_SOURCE_DIR}/../../include/ ${CMAKE_CURRENT_SOURCE_DIR}/include/ ${CMAKE_CURRENT_SOURCE_DIR}/)
    elseif(UNIX)
        MESSAGE(STATUS "This is linux, set INCLUDE_DIRS")
        set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include ${CMAKE_CURRENT_SOURCE_DIR}/../../include/ ${CMAKE_CURRENT_SOURCE_DIR}/include/ ${CMAKE_CURRENT_SOURCE_DIR}/)
    endif(APPLE)
    
    
    if(APPLE)
        MESSAGE(STATUS "This is APPLE, set LINK_DIRS")
        set(LINK_DIRS /usr/local/lib /usr/local/iODBC/lib /opt/snowflake/snowflakeodbc/lib/universal)
    elseif(UNIX)
        MESSAGE(STATUS "This is linux, set LINK_DIRS")
        set(LINK_DIRS ${Boost_INCLUDE_DIRS} /usr/local/lib /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/lib)
    endif(APPLE)
    
    if(APPLE)
        MESSAGE(STATUS "This is APPLE, set ODBC_LIBS")
        set(ODBC_LIBS iodbc iodbcinst)
    elseif(UNIX)
        MESSAGE(STATUS "This is linux, set LINK_DIRS")
        set(ODBC_LIBS odbc odbcinst ltdl)
    endif(APPLE)
    
    include_directories(${INCLUDE_DIRS})
    LINK_DIRECTORIES(${LINK_DIRS})
    
    file( GLOB main_file_list ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) 
    
    file( GLOB APP_SOURCES  ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/utils/*.h ${CMAKE_CURRENT_SOURCE_DIR}/utils/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/http1/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/yaml/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/df/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/death_handler/impl/*.cpp  ${CMAKE_CURRENT_SOURCE_DIR}/include/utils/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/utils/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/impl/utils/*.cpp)
    
    add_library(${PROJECT_NAME}_lib SHARED ${APP_SOURCES})
    target_link_libraries(${PROJECT_NAME}_lib ${Boost_LIBRARIES} ZLIB::ZLIB glog::glog DataFrame::DataFrame ${OpenCV_LIBS})
    target_link_libraries(${PROJECT_NAME}_lib  OpenSSL::SSL OpenSSL::Crypto restclient-cpp libgtest.a pystring libyaml-cpp.a libgmock.a ${ODBC_LIBS} libnanodbc.a pthread dl)
    
    foreach( main_file ${main_file_list} )
        file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${main_file})
        string(REPLACE ".cpp" "" file ${filename})
        add_executable(${file}  ${main_file})
        target_link_libraries(${file} ${PROJECT_NAME}_lib)
    endforeach( main_file ${main_file_list})
    

    main.cpp

    #include <utility>
    #include <iostream>
    #include <variant>
    #include <vector>
    #include <typeinfo>
    
    namespace game_engine {
        struct rendering {};
    };
    
    namespace game::entity::concepts {
        template <typename T>
        concept movable = requires (T value) {
            value.velocity;
            value.position;
        };
    
        template <typename T>
        concept renderable = requires (const T value) {
            value.render_to(std::declval<game_engine::rendering&>());
        };
    
        template <typename T>
        concept has_behavior = requires (T value) {
            value.behave();
        };
    };
    
    namespace game::entity::implementations {
        struct character { // renderable movable has_behavior
            void render_to(game_engine::rendering&) const {}
            void behave() {}
            const int velocity = 2;
            const std::pair<int, int> position = {0, 0};
        };
    
        struct tree { // renderable
            void render_to(game_engine::rendering&) const {}
            const std::pair<int, int> position = {0, 0};
        };
    
        struct hidden_magic_spike {  // has_behavior
            void behave() {}
        };
    };
    
    // usage
    // 用于std::variant遍历的operator操作符重载
    // Refer to https://www.cppstories.com/2019/02/2lines3featuresoverload.html/
    template <typename ... Ts>
    struct overload: Ts... {
        using Ts::operator()...;
    };
    
    // 根据 overload(Ts..) 推导出 lambda表达式类型
    template <typename ... Ts>
    overload(Ts...) -> overload<Ts...>;
    
    auto main(int argc, char** argv) -> int {
        using namespace game::entity::concepts;
        using namespace game::entity::implementations;
    
        // entities 
        using entity_type = std::variant<character, tree, hidden_magic_spike>;
        using entity_collection = std::vector<entity_type>;
    
        auto entities = entity_collection {
            character{},
            tree{},
            hidden_magic_spike{}
        }; 
    
        // visitors: 
        const auto move_visitor = overload {
            [](movable auto arg) {
                const auto [x, y] = arg.position;
                std::cout << "-" << typeid(decltype(arg)).name() << "\n" 
                    << "    with velocity=" << arg.velocity <<
                    " position={" << x << "," << y << "}\n";
            },
            [](auto) {/* default case */}
        };
    
        const auto rendering_visitor = overload {
            [](renderable auto arg) {
                std::cout << "-" << typeid(decltype(arg)).name() << "\n";
            },
            [](auto) {/* default case */}
        };
    
        const auto behave_visitor = overload {
            [](has_behavior auto arg) {
                std::cout << "-" << typeid(decltype(arg)).name() << "\n";
            },
            [](auto) {/* default case */}
        };
    
        // usage:
        std::cout << "move: \n";
        for(auto& value: entities) {
            std::visit(move_visitor, value);
        }
    
        std::cout << "render: \n";
        for(auto& value: entities) {
            std::visit(rendering_visitor, value);
        }
    
        std::cout << "behave: \n";
        for(auto& value: entities) {
            std::visit(behave_visitor, value);
        }
        return EXIT_SUCCESS;
    }
    

    程序输出如下,


    image.png

    相关文章

      网友评论

          本文标题:concepts终极版,一个模拟游戏角色系统的应用

          本文链接:https://www.haomeiwen.com/subject/afzgsrtx.html