Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 20, 2017, 4:48:03 PM (7 years ago)
Author:
kohlia
Message:

Near object, near point and at area work!

File:
1 edited

Legend:

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

    r11562 r11583  
    1717    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout");
    1818    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject");
     19    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearPoint)>::registerFunction<&ScriptableControllerAPI::registerAtNearPoint>(this, lua, "registerAtNearPoint");
    1920    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter");
    2021    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaLeave)>::registerFunction<&ScriptableControllerAPI::registerAtAreaLeave>(this, lua, "registerAtAreaLeave");
    21     LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtObjectDestroyed)>::registerFunction<&ScriptableControllerAPI::registerAtObjectDestroyed>(this, lua, "registerAtObjectDestroyed");
    22     LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPickup)>::registerFunction<&ScriptableControllerAPI::registerAtPickup>(this, lua, "registerAtPickup");
     22//    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtObjectDestroyed)>::registerFunction<&ScriptableControllerAPI::registerAtObjectDestroyed>(this, lua, "registerAtObjectDestroyed");
     23//    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPickup)>::registerFunction<&ScriptableControllerAPI::registerAtPickup>(this, lua, "registerAtPickup");
     24
     25    // Checks for area enter, area leave and near object events
     26    this->areaCheckTimer.setTimer(0.5, true, createExecutor(createFunctor(&ScriptableControllerAPI::checkAreas, this)), false);
    2327}
    2428
     
    3943}
    4044
    41 int ScriptableControllerAPI::registerAtNearObject(std::function<void (int, int)> callback, int obj1, int obj2, double distance)
     45void ScriptableControllerAPI::registerAtNearObject(std::function<void (std::string, std::string)> callback, std::string id1, std::string id2, double distance)
    4246{
    43     //this->controller_->addNearObjectHandler(obj1, obj1, distance, callback);
     47    WorldEntity *entity1 = this->controller_->getWorldEntityByID(id1);
     48    WorldEntity *entity2 = this->controller_->getWorldEntityByID(id2);
     49
     50    if(entity1 != nullptr && entity2 != nullptr)
     51        this->nearObjectHandlers_.push_front(NearObjectHandler(entity1, entity2, id1, id2, distance, callback));
    4452}
    4553
    46 int ScriptableControllerAPI::registerAtAreaEnter(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz)
     54void ScriptableControllerAPI::registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance)
    4755{
     56    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
    4857
     58    if(entity != nullptr)
     59        this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback));
    4960}
    5061
    51 int ScriptableControllerAPI::registerAtAreaLeave(std::function<void (int)> callback, int obj, int x, int y, int z, int dx, int dy, int dz)
     62void ScriptableControllerAPI::registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
    5263{
     64    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
    5365
     66    if(entity != nullptr)
     67        this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback));
    5468}
    5569
    56 int ScriptableControllerAPI::registerAtObjectDestroyed(std::function<void (int)> callback, int obj)
     70void ScriptableControllerAPI::registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
    5771{
     72    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
    5873
     74    if(entity != nullptr)
     75        this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback));
    5976}
    6077
    61 int ScriptableControllerAPI::registerAtPickup(std::function<void (int)> callback, int pickup_id)
     78void ScriptableControllerAPI::checkAreas()
    6279{
     80    // Near object
     81    auto near_obj_handler = this->nearObjectHandlers_.begin();
     82    while(near_obj_handler != this->nearObjectHandlers_.end())
     83    {
     84        if((near_obj_handler->entity1_->getPosition() - near_obj_handler->entity2_->getPosition()).length() < near_obj_handler->distance_)
     85        {
     86            near_obj_handler->callback_(near_obj_handler->id1_, near_obj_handler->id2_);
     87            near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler);
     88        }
     89        else
     90        {
     91            near_obj_handler++;
     92        }
     93    }
    6394
     95    // Near point
     96    auto near_point_handler = this->nearPointHandlers_.begin();
     97    while(near_point_handler != this->nearPointHandlers_.end())
     98    {
     99        if((near_point_handler->entity_->getPosition() - near_point_handler->point_).length() < near_point_handler->distance_)
     100        {
     101            near_point_handler->callback_(near_point_handler->id_);
     102            near_point_handler = this->nearPointHandlers_.erase(near_point_handler);
     103        }
     104        else
     105        {
     106            near_point_handler++;
     107        }
     108    }
     109
     110    // Areas
     111    auto area_handler = this->areaHandlers_.begin();
     112    while(area_handler != this->areaHandlers_.end())
     113    {
     114        if(area_handler->entity_->getPosition() > area_handler->start_point_ &&
     115           area_handler->entity_->getPosition() < area_handler->end_point_)
     116        {
     117            if(area_handler->atEnter_)
     118            {
     119                area_handler->callback_(area_handler->id_);
     120                area_handler = this->areaHandlers_.erase(area_handler);
     121            }
     122            else
     123            {
     124                area_handler++;
     125            }
     126        }
     127        else
     128        {
     129            if(!area_handler->atEnter_)
     130            {
     131                area_handler->callback_(area_handler->id_);
     132                area_handler = this->areaHandlers_.erase(area_handler);
     133            }
     134            else
     135            {
     136                area_handler++;
     137            }
     138        }
     139    }
    64140}
    65141
Note: See TracChangeset for help on using the changeset viewer.