示例#1
0
int main(int argc, char* argv[])
{
    // create a new Lua state.
    lua_State* L = luaL_newstate();

    // load Lua libraries
    std::vector<luaL_Reg> lualibs =
        { {"base", luaopen_base} ,
          {"io", luaopen_io} };
    for(auto& it : lualibs)
    {
        // load the required lua libs and store it in the global space.
        luaL_requiref(L, it.name, it.func, 1);
        // clear the stack in case there is some remaining stuffs there.
        lua_settop(L, 0);
    }

    // load the script
    int status = luaL_loadfile(L, "function.lua");
    if(status == 0) // okay 
    {
        std::cout << "[C++] script loaded" << std::endl;
    }
    else
    {
        std::cout << "[C++] error loading script" << std::endl;
        return 1;
    }

    int result = result = lua_pcall(L, 0, LUA_MULTRET, 0);
    if(status == LUA_OK)
    {
    }
    else
    {
        std::cout << "[C++] Could not run the script." << std::endl;
        return 1;
    }

    Monster snake1("Snake1", 4, DamageFunction(L, "snake_damage_func"));
    Monster snake2("Snake2", 5, DamageFunction(L, "snake_damage_func"));
    Monster bear1("Bear1", 5, DamageFunction(L, "bear_damage_func"));

    std::cout << snake1.name << " : Str Value [" << snake1.strength << "] Dmg Value [" << snake1.getDamage()<< "]"<< std::endl;
    std::cout << snake2.name << " : Str Value [" << snake2.strength << "] Dmg Value [" << snake2.getDamage()<< "]"<< std::endl;
    std::cout << bear1.name << " : Str Value [" << bear1.strength << "] Dmg Value [" << bear1.getDamage()<< "]"<< std::endl;

    lua_close(L);
    return 0;
}
uint16_t snake1oAllForward(uint16_t tick, uint32_t color) {
  return snake1(tick, color, NUM_LEDS, 1);
}
uint16_t snake1o8reverse(uint16_t tick, uint32_t color) {
  return snake1(tick, color, 8, 7);
}
uint16_t snake1o8forward(uint16_t tick, uint32_t color) {
  return snake1(tick, color, 8, 1);
}
uint16_t snake1o4reverse(uint16_t tick, uint32_t color) {
  return snake1(tick, color, 4, 3);
}
uint16_t snake1oAllReverse(uint16_t tick, uint32_t color) {
  return snake1(tick, color, NUM_LEDS, NUM_LEDS-1);
}