Changeset 11662 for code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
- Timestamp:
- Dec 11, 2017, 4:35:38 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
r11638 r11662 4 4 #include "scriptable_controller.h" 5 5 #include "tools/Timer.h" 6 #include "worldentities/pawns/Pawn.h" 7 #include "infos/Bot.h" 8 #include "worldentities/pawns/ModularSpaceShip.h" 6 9 7 10 namespace orxonox … … 32 35 33 36 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn"); 37 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::spawn)>::registerFunction<&ScriptableControllerAPI::spawn>(this, lua, "spawn"); 34 38 35 39 this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false); … … 103 107 } 104 108 109 void ScriptableControllerAPI::spawn(std::string type, std::string id) 110 { 111 if(this->controller_->getWorldEntityByID(id) != nullptr) 112 { 113 orxout(user_warning) << "Script tried to spawn an object, but an object with the given ID exists already" << std::endl; 114 return; 115 } 116 117 Identifier *identifier = ClassByString(type); 118 if(!identifier) 119 { 120 orxout(user_error) << "Script tried to spawn unknown object" << std::endl; 121 return; 122 } 123 124 if(!identifier->isLoadable()) 125 { 126 orxout(user_error) << "Script tried to spawn unloadable object" << std::endl; 127 return; 128 } 129 130 WorldEntity *entity; 131 Identifiable *obj = identifier->fabricate(this->controller_->getWorldEntityByID("Player")->getContext()); 132 133 if(obj->isA(ClassIdentifier<WorldEntity>::getIdentifier())) 134 { 135 entity = orxonox_cast<WorldEntity*>(obj); 136 } 137 else if(obj->isA(ClassIdentifier<PlayerInfo>::getIdentifier())) 138 { 139 // TODO This does not work yet because somehow the controllable entity is not set 140 // yet at this stage. 141 // entity = orxonox_cast<PlayerInfo*>(obj)->getControllableEntity(); 142 return; 143 } 144 else 145 { 146 orxout(user_warning) << "Script tried to spawn an object that is neither a WorldEntity, nor a PlayerInfo" << std::endl; 147 return; 148 } 149 150 if(entity->isA(ClassIdentifier<MobileEntity>::getIdentifier())) 151 this->controller_->registerMobileEntity(id, orxonox_cast<MobileEntity*>(entity)); 152 153 if(entity->isA(ClassIdentifier<Pawn>::getIdentifier())) 154 this->controller_->registerPawn(id, orxonox_cast<Pawn*>(entity)); 155 156 this->controller_->registerWorldEntity(id, orxonox_cast<WorldEntity*>(entity)); 157 } 158 105 159 void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z) 106 160 { … … 179 233 } 180 234 181 void ScriptableControllerAPI::pawnKilled(std::string id )235 void ScriptableControllerAPI::pawnKilled(std::string id, Pawn *pawn) 182 236 { 183 237 for(auto callback : this->pawnDestroyedHandlers_[id]) … … 185 239 186 240 this->pawnDestroyedHandlers_.erase(id); 241 242 // We need to delete those handlers as well, they're no longer valid 243 auto near_obj_handler = this->nearObjectHandlers_.begin(); 244 while(near_obj_handler != this->nearObjectHandlers_.end()) 245 { 246 if(near_obj_handler->entity1_ == pawn || near_obj_handler->entity2_ == pawn) 247 near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler); 248 else 249 near_obj_handler++; 250 } 187 251 } 188 252 … … 257 321 258 322 // Pawns to kill 323 // TODO Possible race condidtion when the player destroys the pawn 324 // between the callback and the next periodic call. 259 325 for(auto &pawn : this->pawnsToKill_) 260 326 this->controller_->killPawn(pawn);
Note: See TracChangeset
for help on using the changeset viewer.