1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
void Bird::live(){
std::vector<Perception*> perceptions = getPerceptions();
lua_getglobal(m_state, "live");
if(!lua_isfunction(m_state, -1)){
Log::i("BIRD") << "Lua function 'live' not found!";
lua_pop(m_state, 1);
wander();
}else{
if(perceptions.size() > 0){
lua_newtable(m_state);
for(unsigned int i = 0; i < perceptions.size(); ++i){
lua_pushnumber(m_state, i+1);
lua_newtable(m_state);
lua_pushstring(m_state, "type");
lua_pushstring(m_state, "bird");
lua_rawset(m_state, -3);
lua_pushstring(m_state, "coord");
lua_newtable(m_state);
lua_pushstring(m_state, "x");
lua_pushnumber(m_state, perceptions.at(i)->getObject()->getPosition().x);
lua_rawset(m_state, -3);
lua_pushstring(m_state, "y");
lua_pushnumber(m_state, perceptions.at(i)->getObject()->getPosition().y);
lua_rawset(m_state, -3);
lua_rawset(m_state, -3);
lua_rawset(m_state, -3);
}
}else{
lua_pushnil(m_state);
}
lua_call(m_state, 1, 3);
if(!lua_isnumber(m_state, -1) || !lua_isnumber(m_state, -2) || !lua_isnumber(m_state, -3)){
Log::e("BIRD") << "Live fonction is not returning numbers";
}
float x = 0, y = 0, theta = 0;
theta = lua_tonumber(m_state, -1);
y = lua_tonumber(m_state, -2);
x = lua_tonumber(m_state, -3);
lua_pop(m_state, 3);
m_body->setInfluence(Vect3<float>(x, y, 0), theta);
}
} |
Partager