Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc @ 11583

Last change on this file since 11583 was 11583, checked in by kohlia, 6 years ago

Near object, near point and at area work!

File size: 5.9 KB
Line 
1
2#include "scriptable_controller_api.h"
3#include "luatb.h"
4#include "scriptable_controller.h"
5#include "tools/Timer.h"
6
7namespace orxonox
8{
9
10ScriptableControllerAPI::ScriptableControllerAPI(lua_State *lua, ScriptableController *controller)
11{
12    this->lua_ = lua;
13    this->controller_ = controller;
14
15    // Haven't found a shorter way yet to write that...
16    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::orxPrint)>::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint");
17    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout");
18    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject");
19    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearPoint)>::registerFunction<&ScriptableControllerAPI::registerAtNearPoint>(this, lua, "registerAtNearPoint");
20    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaEnter)>::registerFunction<&ScriptableControllerAPI::registerAtAreaEnter>(this, lua, "registerAtAreaEnter");
21    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtAreaLeave)>::registerFunction<&ScriptableControllerAPI::registerAtAreaLeave>(this, lua, "registerAtAreaLeave");
22//    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtObjectDestroyed)>::registerFunction<&ScriptableControllerAPI::registerAtObjectDestroyed>(this, lua, "registerAtObjectDestroyed");
23//    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtPickup)>::registerFunction<&ScriptableControllerAPI::registerAtPickup>(this, lua, "registerAtPickup");
24
25    // Checks for area enter, area leave and near object events
26    this->areaCheckTimer.setTimer(0.5, true, createExecutor(createFunctor(&ScriptableControllerAPI::checkAreas, this)), false);
27}
28
29ScriptableControllerAPI::~ScriptableControllerAPI()
30{
31    lua_close(this->lua_);
32}
33
34void ScriptableControllerAPI::orxPrint(std::string msg)
35{
36    orxout(user_info) << msg << std::endl;
37}
38
39void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout)
40{
41    // Kills itself when the timer fires
42    new Timer(timeout, false, callback, true);
43}
44
45void ScriptableControllerAPI::registerAtNearObject(std::function<void (std::string, std::string)> callback, std::string id1, std::string id2, double distance)
46{
47    WorldEntity *entity1 = this->controller_->getWorldEntityByID(id1);
48    WorldEntity *entity2 = this->controller_->getWorldEntityByID(id2);
49
50    if(entity1 != nullptr && entity2 != nullptr)
51        this->nearObjectHandlers_.push_front(NearObjectHandler(entity1, entity2, id1, id2, distance, callback));
52}
53
54void ScriptableControllerAPI::registerAtNearPoint(std::function<void (std::string)> callback, std::string id, double x, double y, double z, double distance)
55{
56    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
57
58    if(entity != nullptr)
59        this->nearPointHandlers_.push_front(NearPointHandler(entity, id, x, y, z, distance, callback));
60}
61
62void ScriptableControllerAPI::registerAtAreaEnter(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
63{
64    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
65
66    if(entity != nullptr)
67        this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, true, callback));
68}
69
70void ScriptableControllerAPI::registerAtAreaLeave(std::function<void (std::string)> callback, std::string id, int x, int y, int z, int dx, int dy, int dz)
71{
72    WorldEntity *entity = this->controller_->getWorldEntityByID(id);
73
74    if(entity != nullptr)
75        this->areaHandlers_.push_front(AreaHandler(entity, id, x, y, z, dx, dy, dz, false, callback));
76}
77
78void ScriptableControllerAPI::checkAreas()
79{
80    // Near object
81    auto near_obj_handler = this->nearObjectHandlers_.begin();
82    while(near_obj_handler != this->nearObjectHandlers_.end())
83    {
84        if((near_obj_handler->entity1_->getPosition() - near_obj_handler->entity2_->getPosition()).length() < near_obj_handler->distance_)
85        {
86            near_obj_handler->callback_(near_obj_handler->id1_, near_obj_handler->id2_);
87            near_obj_handler = this->nearObjectHandlers_.erase(near_obj_handler);
88        }
89        else
90        {
91            near_obj_handler++;
92        }
93    }
94
95    // Near point
96    auto near_point_handler = this->nearPointHandlers_.begin();
97    while(near_point_handler != this->nearPointHandlers_.end())
98    {
99        if((near_point_handler->entity_->getPosition() - near_point_handler->point_).length() < near_point_handler->distance_)
100        {
101            near_point_handler->callback_(near_point_handler->id_);
102            near_point_handler = this->nearPointHandlers_.erase(near_point_handler);
103        }
104        else
105        {
106            near_point_handler++;
107        }
108    }
109
110    // Areas
111    auto area_handler = this->areaHandlers_.begin();
112    while(area_handler != this->areaHandlers_.end())
113    {
114        if(area_handler->entity_->getPosition() > area_handler->start_point_ &&
115           area_handler->entity_->getPosition() < area_handler->end_point_)
116        {
117            if(area_handler->atEnter_)
118            {
119                area_handler->callback_(area_handler->id_);
120                area_handler = this->areaHandlers_.erase(area_handler);
121            }
122            else
123            {
124                area_handler++;
125            }
126        }
127        else
128        {
129            if(!area_handler->atEnter_)
130            {
131                area_handler->callback_(area_handler->id_);
132                area_handler = this->areaHandlers_.erase(area_handler);
133            }
134            else
135            {
136                area_handler++;
137            }
138        }
139    }
140}
141
142}
Note: See TracBrowser for help on using the repository browser.