Changeset 11854
- Timestamp:
- Apr 12, 2018, 1:40:36 PM (7 years ago)
- Location:
- code/branches/ScriptableController_HS17
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/ScriptableController_HS17/data/levels/scripts/scriptableControllerTest.lua
r11684 r11854 41 41 end 42 42 43 orxPrint("Script started!")44 43 44 45 46 47 orxPrint("Script started! OMG ROFL LOL WTF") 48 mytestfunction(3.0, 4.0) 45 49 registerAtAreaLeave(spawn_station, "Player", -300, -100, -100, 200, 200, 200) 46 50 registerAtPawnHit(rotate_faster, "Station") -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
r11673 r11854 22 22 if(lua == nullptr) 23 23 { 24 orxout(internal_warning) << "ScriptableController: Fa lied to load script " << file_path << std::endl;24 orxout(internal_warning) << "ScriptableController: Failed to load script " << file_path << std::endl; 25 25 return LUA_ERRMEM; 26 26 } -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
r11673 r11854 11 11 { 12 12 13 const double ScriptableControllerAPI::periodic_interval = 0.5; 14 15 ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller) 16 { 17 this->lua_ = lua; 18 this->controller_ = controller; 19 20 // Haven't found a shorter way yet to write that... We need C++17! 21 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::orxPrint)>::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint"); 22 23 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout"); 24 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject"); 25 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearPoint)>::registerFunction<&ScriptableControllerAPI::registerAtNearPoint>(this, lua, "registerAtNearPoint"); 26 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter"); 27 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaLeave)>::registerFunction<&ScriptableControllerAPI::registerAtAreaLeave>(this, lua, "registerAtAreaLeave"); 28 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnKilled)>::registerFunction<&ScriptableControllerAPI::registerAtPawnKilled>(this, lua, "registerAtPawnKilled"); 29 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnHit)>::registerFunction<&ScriptableControllerAPI::registerAtPawnHit>(this, lua, "registerAtPawnHit"); 30 31 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setPosition)>::registerFunction<&ScriptableControllerAPI::setPosition>(this, lua, "setPosition"); 32 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setOrientation)>::registerFunction<&ScriptableControllerAPI::setOrientation>(this, lua, "setOrientation"); 33 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setVelocity)>::registerFunction<&ScriptableControllerAPI::setVelocity>(this, lua, "setVelocity"); 34 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setAngularVelocity)>::registerFunction<&ScriptableControllerAPI::setAngularVelocity>(this, lua, "setAngularVelocity"); 35 36 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn"); 37 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::spawn)>::registerFunction<&ScriptableControllerAPI::spawn>(this, lua, "spawn"); 38 39 this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false); 13 const double ScriptableControllerAPI::periodic_interval = 0.5; 14 15 ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller) 16 { 17 this->lua_ = lua; 18 this->controller_ = controller; 19 20 // Haven't found a shorter way yet to write that... We need C++17! 21 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::orxPrint)>::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint"); 22 23 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout"); 24 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject"); 25 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearPoint)>::registerFunction<&ScriptableControllerAPI::registerAtNearPoint>(this, lua, "registerAtNearPoint"); 26 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter"); 27 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaLeave)>::registerFunction<&ScriptableControllerAPI::registerAtAreaLeave>(this, lua, "registerAtAreaLeave"); 28 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnKilled)>::registerFunction<&ScriptableControllerAPI::registerAtPawnKilled>(this, lua, "registerAtPawnKilled"); 29 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPawnHit)>::registerFunction<&ScriptableControllerAPI::registerAtPawnHit>(this, lua, "registerAtPawnHit"); 30 31 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setPosition)>::registerFunction<&ScriptableControllerAPI::setPosition>(this, lua, "setPosition"); 32 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setOrientation)>::registerFunction<&ScriptableControllerAPI::setOrientation>(this, lua, "setOrientation"); 33 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setVelocity)>::registerFunction<&ScriptableControllerAPI::setVelocity>(this, lua, "setVelocity"); 34 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::setAngularVelocity)>::registerFunction<&ScriptableControllerAPI::setAngularVelocity>(this, lua, "setAngularVelocity"); 35 36 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::killPawn)>::registerFunction<&ScriptableControllerAPI::killPawn>(this, lua, "killPawn"); 37 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::spawn)>::registerFunction<&ScriptableControllerAPI::spawn>(this, lua, "spawn"); 38 39 40 41 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::myTestFunction)>::registerFunction<&ScriptableControllerAPI::myTestFunction>(this, lua, "mytestfunction"); 42 43 44 45 this->periodicTimer.setTimer(ScriptableControllerAPI::periodic_interval, true, createExecutor(createFunctor(&ScriptableControllerAPI::periodic, this)), false); 46 } 47 48 ScriptableControllerAPI::~ScriptableControllerAPI() 49 { 50 lua_close(this->lua_); 51 } 52 53 void ScriptableControllerAPI::orxPrint(std::string msg) 54 { 55 orxout(user_info) << msg << std::endl; 56 } 57 58 void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout) 59 { 60 // Kills itself when the timer fires 61 new Timer(timeout, false, callback, true); 62 } 63 64 void ScriptableControllerAPI::registerAtNearObject(std::function<void (std::string, std::string)> callback, std::string id1, std::string id2, double distance) 65 { 66 WorldEntity *entity1 = this->controller_->getWorldEntityByID(id1); 67 WorldEntity *entity2 = this->controller_->getWorldEntityByID(id2); 68 69 if(entity1 != nullptr && entity2 != nullptr) 70 this->nearObjectHandlers_.push_front(NearObjectHandler(entity1, entity2, id1, id2, distance, callback)); 71 } 72 73 void ScriptableControllerAPI::registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance) 74 { 75 WorldEntity *entity = this->controller_->getWorldEntityByID(id); 76 77 if(entity != nullptr) 78 this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback)); 79 } 80 81 void ScriptableControllerAPI::registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz) 82 { 83 WorldEntity *entity = this->controller_->getWorldEntityByID(id); 84 85 if(entity != nullptr) 86 this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback)); 87 } 88 89 void ScriptableControllerAPI::registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz) 90 { 91 WorldEntity *entity = this->controller_->getWorldEntityByID(id); 92 93 if(entity != nullptr) 94 this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback)); 95 } 96 97 void ScriptableControllerAPI::registerAtPawnKilled(std::function<void (std::string)> callback, std::string id) 98 { 99 this->pawnDestroyedHandlers_[id].push_back(callback); 100 } 101 102 void ScriptableControllerAPI::registerAtPawnHit(std::function<void (std::string, std::string, double, double)> callback, std::string id) 103 { 104 this->pawnHitHandlers_[id].push_back(callback); 105 } 106 107 void ScriptableControllerAPI::killPawn(std::string id) 108 { 109 Pawn *pawn = this->controller_->getPawnByID(id); 110 if(pawn == nullptr) 111 orxout(user_warning) << "Trying to kill an unknown pawn" << std::endl; 112 else 113 pawn->kill(); 114 } 115 116 void ScriptableControllerAPI::spawn(std::string type, std::string id) 117 { 118 if(this->controller_->getWorldEntityByID(id) != nullptr) 119 { 120 orxout(user_warning) << "Script tried to spawn an object, but an object with the given ID exists already" << std::endl; 121 return; 122 } 123 124 Identifier *identifier = ClassByString(type); 125 if(!identifier) 126 { 127 orxout(user_error) << "Script tried to spawn unknown object" << std::endl; 128 return; 129 } 130 131 if(!identifier->isLoadable()) 132 { 133 orxout(user_error) << "Script tried to spawn unloadable object" << std::endl; 134 return; 135 } 136 137 WorldEntity *entity; 138 Identifiable *obj = identifier->fabricate(this->controller_->getWorldEntityByID("Player")->getContext()); 139 140 if(obj->isA(ClassIdentifier<WorldEntity>::getIdentifier())) 141 { 142 entity = orxonox_cast<WorldEntity*>(obj); 143 } 144 else if(obj->isA(ClassIdentifier<PlayerInfo>::getIdentifier())) 145 { 146 // TODO This does not work yet because somehow the controllable entity is not set 147 // yet at this stage. 148 // entity = orxonox_cast<PlayerInfo*>(obj)->getControllableEntity(); 149 return; 150 } 151 else 152 { 153 orxout(user_warning) << "Script tried to spawn an object that is neither a WorldEntity, nor a PlayerInfo" << std::endl; 154 return; 155 } 156 157 if(entity->isA(ClassIdentifier<MobileEntity>::getIdentifier())) 158 this->controller_->registerMobileEntity(id, orxonox_cast<MobileEntity*>(entity)); 159 160 if(entity->isA(ClassIdentifier<Pawn>::getIdentifier())) 161 this->controller_->registerPawn(id, orxonox_cast<Pawn*>(entity)); 162 163 this->controller_->registerWorldEntity(id, orxonox_cast<WorldEntity*>(entity)); 164 } 165 166 void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z) 167 { 168 WorldEntity *entity = this->controller_->getWorldEntityByID(id); 169 if(entity == nullptr) 170 { 171 orxout(user_warning) << "Trying to set position of an unknown object" << std::endl; 172 return; 173 } 174 175 const Vector3 &old = entity->getPosition(); 176 177 // If one of the values is NaN, don't change that value 178 x = std::isnan(x) ? old.x : x; 179 y = std::isnan(y) ? old.y : y; 180 z = std::isnan(z) ? old.z : z; 181 182 entity->setPosition(x, y, z); 183 } 184 185 void ScriptableControllerAPI::setOrientation(std::string id, double x, double y, double z, double angle) 186 { 187 WorldEntity *entity = this->controller_->getWorldEntityByID(id); 188 if(entity == nullptr) 189 { 190 orxout(user_warning) << "Trying to set orientation of an unknown object" << std::endl; 191 return; 192 } 193 194 Vector3 old_axis; 195 Degree old_angle; 196 197 entity->getOrientation().ToAngleAxis(old_angle, old_axis); 198 199 // If one of the values is NaN, don't change that value 200 x = std::isnan(x) ? old_axis.x : x; 201 y = std::isnan(y) ? old_axis.y : y; 202 z = std::isnan(z) ? old_axis.z : z; 203 angle = std::isnan(x) ? old_angle.valueDegrees() : angle; 204 205 206 entity->setOrientation(Vector3(x, y, z), Degree(angle)); 207 } 208 209 void ScriptableControllerAPI::setVelocity(std::string id, double x, double y, double z) 210 { 211 MobileEntity *entity = this->controller_->getMobileEntityByID(id); 212 if(entity == nullptr) 213 { 214 orxout(user_warning) << "Trying to set velocity of an unknown object" << std::endl; 215 return; 216 } 217 218 const Vector3 &old = entity->getVelocity(); 219 220 // If one of the values is NaN, don't change that value 221 x = std::isnan(x) ? old.x : x; 222 y = std::isnan(y) ? old.y : y; 223 z = std::isnan(z) ? old.z : z; 224 225 entity->setVelocity(x, y, z); 226 } 227 228 void ScriptableControllerAPI::setAngularVelocity(std::string id, double x, double y, double z) 229 { 230 MobileEntity *entity = this->controller_->getMobileEntityByID(id); 231 if(entity == nullptr) 232 { 233 orxout(user_warning) << "Trying to set angular velocity of an unknown object" << std::endl; 234 return; 235 } 236 237 const Vector3 &old = entity->getAngularVelocity(); 238 239 // If one of the values is NaN, don't change that value 240 x = std::isnan(x) ? old.x : x; 241 y = std::isnan(y) ? old.y : y; 242 z = std::isnan(z) ? old.z : z; 243 244 entity->setAngularVelocity(x, y, z); 245 } 246 247 void ScriptableControllerAPI::pawnKilled(std::string id, Pawn *pawn) 248 { 249 for(auto callback : this->pawnDestroyedHandlers_[id]) 250 callback(id); 251 252 this->pawnDestroyedHandlers_.erase(id); 253 254 // We need to delete those handlers as well, they're no longer valid 255 auto near_obj_handler = this->nearObjectHandlers_.begin(); 256 while(near_obj_handler != this->nearObjectHandlers_.end()) 257 { 258 if(near_obj_handler->entity1_ == pawn || near_obj_handler->entity2_ == pawn) 259 near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler); 260 else 261 near_obj_handler++; 262 } 263 264 auto near_point_handler = this->nearPointHandlers_.begin(); 265 while(near_point_handler != this->nearPointHandlers_.end()) 266 { 267 if(near_point_handler->entity_ == pawn) 268 near_point_handler = this->nearPointHandlers_.erase(near_point_handler); 269 else 270 near_point_handler++; 271 } 272 273 auto area_handler = this->areaHandlers_.begin(); 274 while(area_handler != this->areaHandlers_.end()) 275 { 276 if(area_handler->entity_ == pawn) 277 area_handler = this->areaHandlers_.erase(area_handler); 278 else 279 area_handler++; 280 } 281 } 282 283 void ScriptableControllerAPI::pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield) 284 { 285 for(auto callback : this->pawnHitHandlers_[target_id]) 286 callback(target_id, source_id, new_health, new_shield); 287 } 288 289 void ScriptableControllerAPI::periodic() 290 { 291 // Near object 292 auto near_obj_handler = this->nearObjectHandlers_.begin(); 293 while(near_obj_handler != this->nearObjectHandlers_.end()) 294 { 295 if((near_obj_handler->entity1_->getPosition() - near_obj_handler->entity2_->getPosition()).length() < near_obj_handler->distance_) 296 { 297 near_obj_handler->callback_(near_obj_handler->id1_, near_obj_handler->id2_); 298 near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler); 299 } 300 else 301 { 302 near_obj_handler++; 303 } 304 } 305 306 // Near point 307 auto near_point_handler = this->nearPointHandlers_.begin(); 308 while(near_point_handler != this->nearPointHandlers_.end()) 309 { 310 if((near_point_handler->entity_->getPosition() - near_point_handler->point_).length() < near_point_handler->distance_) 311 { 312 near_point_handler->callback_(near_point_handler->id_); 313 near_point_handler = this->nearPointHandlers_.erase(near_point_handler); 314 } 315 else 316 { 317 near_point_handler++; 318 } 319 } 320 321 // Areas 322 auto area_handler = this->areaHandlers_.begin(); 323 while(area_handler != this->areaHandlers_.end()) 324 { 325 if(area_handler->entity_->getPosition() > area_handler->start_point_ && 326 area_handler->entity_->getPosition() < area_handler->end_point_) 327 { 328 if(area_handler->atEnter_) 329 { 330 area_handler->callback_(area_handler->id_); 331 area_handler = this->areaHandlers_.erase(area_handler); 332 } 333 else 334 { 335 area_handler++; 336 } 337 } 338 else 339 { 340 if(!area_handler->atEnter_) 341 { 342 area_handler->callback_(area_handler->id_); 343 area_handler = this->areaHandlers_.erase(area_handler); 344 } 345 else 346 { 347 area_handler++; 348 } 349 } 350 } 351 352 } 353 354 355 //// TESTTESTTESTTESTTESTTEST 356 double ScriptableControllerAPI::myTestFunction(double x, double y) 357 { 358 double z = x + y; 359 orxout(user_info) << "Result = " << z << endl; 360 return z; 361 } 362 40 363 } 41 42 ScriptableControllerAPI::~ScriptableControllerAPI()43 {44 lua_close(this->lua_);45 }46 47 void ScriptableControllerAPI::orxPrint(std::string msg)48 {49 orxout(user_info) << msg << std::endl;50 }51 52 void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout)53 {54 // Kills itself when the timer fires55 new Timer(timeout, false, callback, true);56 }57 58 void ScriptableControllerAPI::registerAtNearObject(std::function<void (std::string, std::string)> callback, std::string id1, std::string id2, double distance)59 {60 WorldEntity *entity1 = this->controller_->getWorldEntityByID(id1);61 WorldEntity *entity2 = this->controller_->getWorldEntityByID(id2);62 63 if(entity1 != nullptr && entity2 != nullptr)64 this->nearObjectHandlers_.push_front(NearObjectHandler(entity1, entity2, id1, id2, distance, callback));65 }66 67 void ScriptableControllerAPI::registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance)68 {69 WorldEntity *entity = this->controller_->getWorldEntityByID(id);70 71 if(entity != nullptr)72 this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback));73 }74 75 void ScriptableControllerAPI::registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)76 {77 WorldEntity *entity = this->controller_->getWorldEntityByID(id);78 79 if(entity != nullptr)80 this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback));81 }82 83 void ScriptableControllerAPI::registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)84 {85 WorldEntity *entity = this->controller_->getWorldEntityByID(id);86 87 if(entity != nullptr)88 this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback));89 }90 91 void ScriptableControllerAPI::registerAtPawnKilled(std::function<void (std::string)> callback, std::string id)92 {93 this->pawnDestroyedHandlers_[id].push_back(callback);94 }95 96 void ScriptableControllerAPI::registerAtPawnHit(std::function<void (std::string, std::string, double, double)> callback, std::string id)97 {98 this->pawnHitHandlers_[id].push_back(callback);99 }100 101 void ScriptableControllerAPI::killPawn(std::string id)102 {103 Pawn *pawn = this->controller_->getPawnByID(id);104 if(pawn == nullptr)105 orxout(user_warning) << "Trying to kill an unknown pawn" << std::endl;106 else107 pawn->kill();108 }109 110 void ScriptableControllerAPI::spawn(std::string type, std::string id)111 {112 if(this->controller_->getWorldEntityByID(id) != nullptr)113 {114 orxout(user_warning) << "Script tried to spawn an object, but an object with the given ID exists already" << std::endl;115 return;116 }117 118 Identifier *identifier = ClassByString(type);119 if(!identifier)120 {121 orxout(user_error) << "Script tried to spawn unknown object" << std::endl;122 return;123 }124 125 if(!identifier->isLoadable())126 {127 orxout(user_error) << "Script tried to spawn unloadable object" << std::endl;128 return;129 }130 131 WorldEntity *entity;132 Identifiable *obj = identifier->fabricate(this->controller_->getWorldEntityByID("Player")->getContext());133 134 if(obj->isA(ClassIdentifier<WorldEntity>::getIdentifier()))135 {136 entity = orxonox_cast<WorldEntity*>(obj);137 }138 else if(obj->isA(ClassIdentifier<PlayerInfo>::getIdentifier()))139 {140 // TODO This does not work yet because somehow the controllable entity is not set141 // yet at this stage.142 // entity = orxonox_cast<PlayerInfo*>(obj)->getControllableEntity();143 return;144 }145 else146 {147 orxout(user_warning) << "Script tried to spawn an object that is neither a WorldEntity, nor a PlayerInfo" << std::endl;148 return;149 }150 151 if(entity->isA(ClassIdentifier<MobileEntity>::getIdentifier()))152 this->controller_->registerMobileEntity(id, orxonox_cast<MobileEntity*>(entity));153 154 if(entity->isA(ClassIdentifier<Pawn>::getIdentifier()))155 this->controller_->registerPawn(id, orxonox_cast<Pawn*>(entity));156 157 this->controller_->registerWorldEntity(id, orxonox_cast<WorldEntity*>(entity));158 }159 160 void ScriptableControllerAPI::setPosition(std::string id, double x, double y, double z)161 {162 WorldEntity *entity = this->controller_->getWorldEntityByID(id);163 if(entity == nullptr)164 {165 orxout(user_warning) << "Trying to set position of an unknown object" << std::endl;166 return;167 }168 169 const Vector3 &old = entity->getPosition();170 171 // If one of the values is NaN, don't change that value172 x = std::isnan(x) ? old.x : x;173 y = std::isnan(y) ? old.y : y;174 z = std::isnan(z) ? old.z : z;175 176 entity->setPosition(x, y, z);177 }178 179 void ScriptableControllerAPI::setOrientation(std::string id, double x, double y, double z, double angle)180 {181 WorldEntity *entity = this->controller_->getWorldEntityByID(id);182 if(entity == nullptr)183 {184 orxout(user_warning) << "Trying to set orientation of an unknown object" << std::endl;185 return;186 }187 188 Vector3 old_axis;189 Degree old_angle;190 191 entity->getOrientation().ToAngleAxis(old_angle, old_axis);192 193 // If one of the values is NaN, don't change that value194 x = std::isnan(x) ? old_axis.x : x;195 y = std::isnan(y) ? old_axis.y : y;196 z = std::isnan(z) ? old_axis.z : z;197 angle = std::isnan(x) ? old_angle.valueDegrees() : angle;198 199 200 entity->setOrientation(Vector3(x, y, z), Degree(angle));201 }202 203 void ScriptableControllerAPI::setVelocity(std::string id, double x, double y, double z)204 {205 MobileEntity *entity = this->controller_->getMobileEntityByID(id);206 if(entity == nullptr)207 {208 orxout(user_warning) << "Trying to set velocity of an unknown object" << std::endl;209 return;210 }211 212 const Vector3 &old = entity->getVelocity();213 214 // If one of the values is NaN, don't change that value215 x = std::isnan(x) ? old.x : x;216 y = std::isnan(y) ? old.y : y;217 z = std::isnan(z) ? old.z : z;218 219 entity->setVelocity(x, y, z);220 }221 222 void ScriptableControllerAPI::setAngularVelocity(std::string id, double x, double y, double z)223 {224 MobileEntity *entity = this->controller_->getMobileEntityByID(id);225 if(entity == nullptr)226 {227 orxout(user_warning) << "Trying to set angular velocity of an unknown object" << std::endl;228 return;229 }230 231 const Vector3 &old = entity->getAngularVelocity();232 233 // If one of the values is NaN, don't change that value234 x = std::isnan(x) ? old.x : x;235 y = std::isnan(y) ? old.y : y;236 z = std::isnan(z) ? old.z : z;237 238 entity->setAngularVelocity(x, y, z);239 }240 241 void ScriptableControllerAPI::pawnKilled(std::string id, Pawn *pawn)242 {243 for(auto callback : this->pawnDestroyedHandlers_[id])244 callback(id);245 246 this->pawnDestroyedHandlers_.erase(id);247 248 // We need to delete those handlers as well, they're no longer valid249 auto near_obj_handler = this->nearObjectHandlers_.begin();250 while(near_obj_handler != this->nearObjectHandlers_.end())251 {252 if(near_obj_handler->entity1_ == pawn || near_obj_handler->entity2_ == pawn)253 near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler);254 else255 near_obj_handler++;256 }257 258 auto near_point_handler = this->nearPointHandlers_.begin();259 while(near_point_handler != this->nearPointHandlers_.end())260 {261 if(near_point_handler->entity_ == pawn)262 near_point_handler = this->nearPointHandlers_.erase(near_point_handler);263 else264 near_point_handler++;265 }266 267 auto area_handler = this->areaHandlers_.begin();268 while(area_handler != this->areaHandlers_.end())269 {270 if(area_handler->entity_ == pawn)271 area_handler = this->areaHandlers_.erase(area_handler);272 else273 area_handler++;274 }275 }276 277 void ScriptableControllerAPI::pawnHit(std::string target_id, std::string source_id, double new_health, double new_shield)278 {279 for(auto callback : this->pawnHitHandlers_[target_id])280 callback(target_id, source_id, new_health, new_shield);281 }282 283 void ScriptableControllerAPI::periodic()284 {285 // Near object286 auto near_obj_handler = this->nearObjectHandlers_.begin();287 while(near_obj_handler != this->nearObjectHandlers_.end())288 {289 if((near_obj_handler->entity1_->getPosition() - near_obj_handler->entity2_->getPosition()).length() < near_obj_handler->distance_)290 {291 near_obj_handler->callback_(near_obj_handler->id1_, near_obj_handler->id2_);292 near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler);293 }294 else295 {296 near_obj_handler++;297 }298 }299 300 // Near point301 auto near_point_handler = this->nearPointHandlers_.begin();302 while(near_point_handler != this->nearPointHandlers_.end())303 {304 if((near_point_handler->entity_->getPosition() - near_point_handler->point_).length() < near_point_handler->distance_)305 {306 near_point_handler->callback_(near_point_handler->id_);307 near_point_handler = this->nearPointHandlers_.erase(near_point_handler);308 }309 else310 {311 near_point_handler++;312 }313 }314 315 // Areas316 auto area_handler = this->areaHandlers_.begin();317 while(area_handler != this->areaHandlers_.end())318 {319 if(area_handler->entity_->getPosition() > area_handler->start_point_ &&320 area_handler->entity_->getPosition() < area_handler->end_point_)321 {322 if(area_handler->atEnter_)323 {324 area_handler->callback_(area_handler->id_);325 area_handler = this->areaHandlers_.erase(area_handler);326 }327 else328 {329 area_handler++;330 }331 }332 else333 {334 if(!area_handler->atEnter_)335 {336 area_handler->callback_(area_handler->id_);337 area_handler = this->areaHandlers_.erase(area_handler);338 }339 else340 {341 area_handler++;342 }343 }344 }345 }346 347 } -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
r11673 r11854 196 196 void setAngularVelocity(std::string id, double x, double y, double z); 197 197 198 199 /** 200 * @brief Set the angular velocity of an object 201 * @param id The ID of the object 202 * @param x The rotation velocity around the x-axis 203 * @param y The rotation velocity around the y-axis 204 * @param z The rotation velocity around the z-axis 205 */ 206 double myTestFunction(double x, double y); 207 198 208 // ### API END ################################################################ 199 209
Note: See TracChangeset
for help on using the changeset viewer.