Changeset 11583 for code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
- Timestamp:
- Nov 20, 2017, 4:48:03 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
r11562 r11583 3 3 #include "luatb.h" 4 4 #include "infos/PlayerInfo.h" 5 #include "core/command/Executor.h" 5 6 6 7 namespace orxonox … … 11 12 { 12 13 int ret; 14 15 // Not having a script specified at all is not an error 16 if(file_path.empty()) 17 return 0; 13 18 14 19 // Create a lua object … … 45 50 this->apis_.push_back(std::unique_ptr<ScriptableControllerAPI>(api)); 46 51 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.49 52 return 0; 50 53 } … … 55 58 } 56 59 57 void ScriptableController::registerWorldEntity( int id, WorldEntity *obj)60 void ScriptableController::registerWorldEntity(std::string id, WorldEntity *entity) 58 61 { 59 orxout(user_info) << "WorldEntity registered (id: " << id << ")" << std::endl; 60 this->worldEntities_[id] = obj; 62 this->worldEntities_[id] = entity; 61 63 } 62 64 63 void ScriptableController::registerControllableEntity( int id, ControllableEntity *obj)65 void ScriptableController::registerControllableEntity(std::string id, ControllableEntity *entity) 64 66 { 65 orxout(user_info) << "ControllableEntity registered (id: " << id << ")" << std::endl; 66 this->worldEntities_[id] = obj; 67 this->worldEntities_[id] = entity; 67 68 } 68 69 69 WorldEntity *ScriptableController::getWorldEntityByID( intid) const70 WorldEntity *ScriptableController::getWorldEntityByID(std::string id) const 70 71 { 71 if(id == 0)72 if(id == "player" || id == "Player" || id == "PLAYER") 72 73 return this->player_->getControllableEntity(); 73 74 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; 76 77 } 77 78 78 ControllableEntity *ScriptableController::getControllableEntityByID( intid) const79 ControllableEntity *ScriptableController::getControllableEntityByID(std::string id) const 79 80 { 80 if(id == 0)81 if(id == "player" || id == "Player" || id == "PLAYER") 81 82 return this->player_->getControllableEntity(); 82 83 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; 132 86 } 133 87
Note: See TracChangeset
for help on using the changeset viewer.