- Timestamp:
- Nov 13, 2017, 5:25:09 PM (7 years ago)
- Location:
- code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc
r11552 r11562 2 2 #include "scriptable_controller.h" 3 3 #include "luatb.h" 4 #include "infos/PlayerInfo.h" 4 5 5 6 namespace orxonox 6 7 { 7 8 8 RegisterUnloadableClass(ScriptableController);9 10 9 // Used https://csl.name/post/lua-and-cpp/ as a reference 11 ScriptableController::ScriptableController(Context* context)12 : BaseObject(context)13 {14 RegisterObject(ScriptableController);15 }16 17 10 int ScriptableController::runScript(const std::string &file_path) 18 11 { … … 57 50 } 58 51 52 void ScriptableController::setPlayer(PlayerInfo *player) 53 { 54 this->player_ = player; 55 } 56 59 57 void ScriptableController::registerWorldEntity(int id, WorldEntity *obj) 60 58 { 59 orxout(user_info) << "WorldEntity registered (id: " << id << ")" << std::endl; 61 60 this->worldEntities_[id] = obj; 62 61 } … … 64 63 void ScriptableController::registerControllableEntity(int id, ControllableEntity *obj) 65 64 { 65 orxout(user_info) << "ControllableEntity registered (id: " << id << ")" << std::endl; 66 66 this->worldEntities_[id] = obj; 67 67 } … … 69 69 WorldEntity *ScriptableController::getWorldEntityByID(int id) const 70 70 { 71 if(id == 0) 72 return this->player_->getControllableEntity(); 73 71 74 auto obj = this->worldEntities_.find(id); 72 75 return obj != this->worldEntities_.end() ? obj->second : nullptr; … … 75 78 ControllableEntity *ScriptableController::getControllableEntityByID(int id) const 76 79 { 80 if(id == 0) 81 return this->player_->getControllableEntity(); 82 77 83 auto obj = this->controllabelEntities_.find(id); 78 84 return obj != this->controllabelEntities_.end() ? obj->second : nullptr; 79 85 } 80 86 81 void ScriptableController:: registerTimeout(std::function<void ()> callback, double timeout)87 void ScriptableController::addNearObjectHandler(int obj1, int obj2, double distance, std::function<void (int,int)> callback) 82 88 { 83 this->timeouts.push_back(std::make_pair(callback, static_cast<float>(timeout))); 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(); 84 102 } 85 103 86 void ScriptableController:: tick(float dt)104 void ScriptableController::objectMoved(WorldEntity *obj) 87 105 { 88 auto timeout = this->timeouts.begin(); 106 // Temp 107 return; 89 108 90 while(timeout != this->timeouts.end()) 109 auto &nearObjectHandlers = this->nearObjectHandlers_[obj]; 110 auto handler = nearObjectHandlers.begin(); 111 while(handler != nearObjectHandlers.end()) 91 112 { 92 timeout->second -= dt;93 if( timeout->second <= 0)113 WorldEntity *other = (*handler)->otherObject_; 114 if((obj->getPosition() - other->getPosition()).length() < (*handler)->distance_) 94 115 { 95 timeout->first(); 96 timeout = this->timeouts.erase(timeout); 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); 97 126 } 98 127 else 99 128 { 100 timeout++;129 handler++; 101 130 } 102 131 } 103 104 Tickable::tick(dt);105 132 } 106 133 -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h
r11549 r11562 17 17 { 18 18 19 class ScriptableController : public BaseObject, public Tickable19 class ScriptableController 20 20 { 21 21 public: 22 explicit ScriptableController(Context *context); 22 struct NearObjectHandler 23 { 24 NearObjectHandler(WorldEntity *otherObject, double distance, std::function<void (int, int)> callback) 25 : otherObject_(otherObject), distance_(distance), callback_(callback) 26 {} 27 28 WorldEntity *otherObject_; 29 double distance_; 30 std::function<void (int, int)> callback_; 31 std::list<std::unique_ptr<NearObjectHandler> >::iterator otherHandler_; 32 }; 23 33 24 34 int runScript(const std::string &file_path); 25 35 36 void setPlayer(PlayerInfo *player); 26 37 void registerWorldEntity(int id, WorldEntity *obj); 27 38 void registerControllableEntity(int id, ControllableEntity *obj); … … 30 41 ControllableEntity *getControllableEntityByID(int id) const; 31 42 32 void registerTimeout(std::function<void (void)> callback, double timeout);43 void addNearObjectHandler(int obj1, int obj2, double distance, std::function<void (int, int)> callback); 33 44 34 v irtual void tick(float dt) override;45 void objectMoved(WorldEntity *obj); 35 46 36 47 private: 37 48 std::list<std::unique_ptr<ScriptableControllerAPI> > apis_; 38 ControllableEntity *player_; // TODO49 PlayerInfo *player_; 39 50 std::map<int, WorldEntity*> worldEntities_; 40 51 std::map<int, ControllableEntity*> controllabelEntities_; 41 std::list<std::pair<std::function<void (void)>, float> > timeouts; 52 53 std::map<WorldEntity*, std::list<std::unique_ptr<NearObjectHandler> > > nearObjectHandlers_; 42 54 43 55 void printLuaError(lua_State *lua); -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc
r11552 r11562 3 3 #include "luatb.h" 4 4 #include "scriptable_controller.h" 5 #include "tools/Timer.h" 5 6 6 7 namespace orxonox … … 13 14 14 15 // Haven't found a shorter way yet to write that... 16 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::orxPrint)>::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint"); 15 17 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout"); 16 18 LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject"); … … 26 28 } 27 29 30 void ScriptableControllerAPI::orxPrint(std::string msg) 31 { 32 orxout(user_info) << msg << std::endl; 33 } 34 28 35 void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout) 29 36 { 30 // TODO Extend timer class to accept std::function31 this->controller_->registerTimeout(callback, timeout);37 // Kills itself when the timer fires 38 new Timer(timeout, false, callback, true); 32 39 } 33 40 34 41 int ScriptableControllerAPI::registerAtNearObject(std::function<void (int, int)> callback, int obj1, int obj2, double distance) 35 42 { 36 43 //this->controller_->addNearObjectHandler(obj1, obj1, distance, callback); 37 44 } 38 45 -
code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h
r11549 r11562 22 22 void testOutput(std::function<void(std::string)> callback); 23 23 24 void orxPrint(std::string msg); 24 25 void registerAfterTimeout(std::function<void (void)> callback, double timeout); 25 26 int registerAtNearObject(std::function<void(int, int)> callback, int obj1, int obj2, double distance);
Note: See TracChangeset
for help on using the changeset viewer.