Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 27, 2017, 4:38:50 PM (7 years ago)
Author:
kohlia
Message:

Pawn killing works too now

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc

    r11583 r11606  
    88{
    99
     10const double ScriptableControllerAPI::periodic_interval = 0.5;
     11
    1012ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller)
    1113{
     
    1517    // Haven't found a shorter way yet to write that...
    1618    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::orxPrint)>::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint");
     19
    1720    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout");
    1821    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject");
     
    2023    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter");
    2124    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaLeave)>::registerFunction<&ScriptableControllerAPI::registerAtAreaLeave>(this, lua, "registerAtAreaLeave");
    22 //    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtObjectDestroyed)>::registerFunction<&ScriptableControllerAPI::registerAtObjectDestroyed>(this, lua, "registerAtObjectDestroyed");
    23 //    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPickup)>::registerFunction<&ScriptableControllerAPI::registerAtPickup>(this, lua, "registerAtPickup");
     25    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnKilled)>::registerFunction<&ScriptableControllerAPI::registerAtPawnKilled>(this, lua, "registerAtPawnKilled");
     26    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnHit)>::registerFunction<&ScriptableControllerAPI::registerAtPawnHit>(this, lua, "registerAtPawnHit");
    2427
    25     // Checks for area enter, area leave and near object events
    26     this->areaCheckTimer.setTimer(0.5, true, createExecutor(createFunctor(&ScriptableControllerAPI::checkAreas, this)), false);
     28    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn");
     29
     30    this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false);
    2731}
    2832
     
    7680}
    7781
    78 void ScriptableControllerAPI::checkAreas()
     82void ScriptableControllerAPI::registerAtPawnKilled(std::function<void (std::string)> callback, std::string id)
     83{
     84    this->pawnDestroyedHandlers_[id].push_back(callback);
     85}
     86
     87void ScriptableControllerAPI::registerAtPawnHit(std::function<void (std::string, std::string, double, double)> callback, std::string id)
     88{
     89    this->pawnHitHandlers_[id].push_back(callback);
     90}
     91
     92void ScriptableControllerAPI::killPawn(std::string id)
     93{
     94    // We don't kill the pawn here directly, because this function is called from LUA and thus
     95    // runs in a different thread. So we schedule the kill for later in the main thread
     96    // (in 'periodic').
     97    this->pawnsToKill_.push_back(id);
     98}
     99
     100void ScriptableControllerAPI::pawnKilled(std::string id)
     101{
     102    for(auto callback : this->pawnDestroyedHandlers_[id])
     103        callback(id);
     104
     105    this->pawnDestroyedHandlers_.erase(id);
     106}
     107
     108void ScriptableControllerAPI::pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield)
     109{
     110    for(auto callback : this->pawnHitHandlers_[target_id])
     111        callback(target_id, source_id, new_health, new_shield);
     112}
     113
     114void ScriptableControllerAPI::periodic()
    79115{
    80116    // Near object
     
    138174        }
    139175    }
     176
     177    // Pawns to kill
     178    for(auto &pawn : this->pawnsToKill_)
     179        this->controller_->killPawn(pawn);
     180
     181    this->pawnsToKill_.clear();
    140182}
    141183
Note: See TracChangeset for help on using the changeset viewer.