Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 20, 2017, 4:48:03 PM (6 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.cc

    r11562 r11583  
    33#include "luatb.h"
    44#include "infos/PlayerInfo.h"
     5#include "core/command/Executor.h"
    56
    67namespace orxonox
     
    1112{
    1213    int ret;
     14
     15    // Not having a script specified at all is not an error
     16    if(file_path.empty())
     17        return 0;
    1318
    1419    // Create a lua object
     
    4550    this->apis_.push_back(std::unique_ptr<ScriptableControllerAPI>(api));
    4651
    47     // Only the caller knows, when the end-of-life of the script is reached,
    48     // so we give him control over when to destroy it.
    4952    return 0;
    5053}
     
    5558}
    5659
    57 void ScriptableController::registerWorldEntity(int id, WorldEntity *obj)
     60void ScriptableController::registerWorldEntity(std::string id, WorldEntity *entity)
    5861{
    59     orxout(user_info) << "WorldEntity registered (id: " << id << ")" << std::endl;
    60     this->worldEntities_[id] = obj;
     62    this->worldEntities_[id] = entity;
    6163}
    6264
    63 void ScriptableController::registerControllableEntity(int id, ControllableEntity *obj)
     65void ScriptableController::registerControllableEntity(std::string id, ControllableEntity *entity)
    6466{
    65     orxout(user_info) << "ControllableEntity registered (id: " << id << ")" << std::endl;
    66     this->worldEntities_[id] = obj;
     67    this->worldEntities_[id] = entity;
    6768}
    6869
    69 WorldEntity *ScriptableController::getWorldEntityByID(int id) const
     70WorldEntity *ScriptableController::getWorldEntityByID(std::string id) const
    7071{
    71     if(id == 0)
     72    if(id == "player" || id == "Player" || id == "PLAYER")
    7273        return this->player_->getControllableEntity();
    7374
    74     auto obj = this->worldEntities_.find(id);
    75     return obj != this->worldEntities_.end() ? obj->second : nullptr;
     75    auto entity = this->worldEntities_.find(id);
     76    return entity != this->worldEntities_.end() ? entity->second : nullptr;
    7677}
    7778
    78 ControllableEntity *ScriptableController::getControllableEntityByID(int id) const
     79ControllableEntity *ScriptableController::getControllableEntityByID(std::string id) const
    7980{
    80     if(id == 0)
     81    if(id == "player" || id == "Player" || id == "PLAYER")
    8182        return this->player_->getControllableEntity();
    8283
    83     auto obj = this->controllabelEntities_.find(id);
    84     return obj != this->controllabelEntities_.end() ? obj->second : nullptr;
    85 }
    86 
    87 void ScriptableController::addNearObjectHandler(int obj1, int obj2, double distance, std::function<void (int,int)> callback)
    88 {
    89     orxout(user_info) << "NearObject registering (id 1: " << obj1 << ", id 2: " << obj2 << ")" << std::endl;
    90 
    91     WorldEntity *entity1 = this->getWorldEntityByID(obj1);
    92     WorldEntity *entity2 = this->getWorldEntityByID(obj2);
    93 
    94     NearObjectHandler *handler1 = new NearObjectHandler(entity2, distance, callback);
    95     NearObjectHandler *handler2 = new NearObjectHandler(entity1, distance, callback);
    96 
    97     this->nearObjectHandlers_[entity1].push_front(std::unique_ptr<NearObjectHandler>(handler1));
    98     this->nearObjectHandlers_[entity2].push_front(std::unique_ptr<NearObjectHandler>(handler2));
    99 
    100     handler1->otherHandler_ = this->nearObjectHandlers_[entity2].begin();
    101     handler2->otherHandler_ = this->nearObjectHandlers_[entity1].begin();
    102 }
    103 
    104 void ScriptableController::objectMoved(WorldEntity *obj)
    105 {
    106     // Temp
    107     return;
    108 
    109     auto &nearObjectHandlers = this->nearObjectHandlers_[obj];
    110     auto handler = nearObjectHandlers.begin();
    111     while(handler != nearObjectHandlers.end())
    112     {
    113         WorldEntity *other = (*handler)->otherObject_;
    114         if((obj->getPosition() - other->getPosition()).length() < (*handler)->distance_)
    115         {
    116             (*handler)->callback_(obj->getID(), other->getID());
    117 
    118             auto otherHandler = (*handler)->otherHandler_;
    119             handler = nearObjectHandlers.erase(handler);
    120             if(nearObjectHandlers.empty())
    121                 this->nearObjectHandlers_.erase(obj);
    122 
    123             this->nearObjectHandlers_[other].erase(otherHandler);
    124             if(this->nearObjectHandlers_[other].empty())
    125                 this->nearObjectHandlers_.erase(other);
    126         }
    127         else
    128         {
    129             handler++;
    130         }
    131     }
     84    auto entity = this->controllabelEntities_.find(id);
     85    return entity != this->controllabelEntities_.end() ? entity->second : nullptr;
    13286}
    13387
Note: See TracChangeset for help on using the changeset viewer.