Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 11562


Ignore:
Timestamp:
Nov 13, 2017, 5:25:09 PM (7 years ago)
Author:
kohlia
Message:

Figuring out when the different things are ready in orxonox

Location:
code/branches/ScriptableController_HS17/src
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • code/branches/ScriptableController_HS17/src/libraries/tools/Timer.cc

    r11071 r11562  
    158158    }
    159159
     160    Timer::Timer(float interval, bool bLoop, std::function<void ()> func, bool bKillAfterCall)
     161    {
     162        this->init();
     163        RegisterObject(Timer);
     164
     165        this->setTimer(interval, bLoop, func, bKillAfterCall);
     166    }
     167
    160168    /**
    161169        @brief Initializes the Timer
     
    193201        this->executor_ = executor;
    194202        this->bActive_ = true;
     203        this->isStdFunction_ = false;
    195204
    196205        this->time_ = this->interval_;
    197206        this->bKillAfterCall_ = bKillAfterCall;
    198207
    199         executor->getFunctor()->setSafeMode(true);
     208        if(executor != nullptr)
     209            executor->getFunctor()->setSafeMode(true);
     210    }
     211
     212    void Timer::setTimer(float interval, bool bLoop, std::function<void ()> func, bool bKillAfterCall)
     213    {
     214        // Without the cast, the call would be ambiguous, because nullptr is castable to
     215        // both, ExecutorPtr and std::function.
     216        this->setTimer(interval, bLoop, static_cast<ExecutorPtr>(nullptr), bKillAfterCall);
     217        this->function_ = func;
     218        this->isStdFunction_ = true;
    200219    }
    201220
     
    207226        bool temp = this->bKillAfterCall_; // to avoid errors with bKillAfterCall_=false and an exutors which destroy the timer
    208227
    209         (*this->executor_)();
     228        if(this->isStdFunction_)
     229            this->function_();
     230        else
     231            (*this->executor_)();
    210232
    211233        if (temp)
  • code/branches/ScriptableController_HS17/src/libraries/tools/Timer.h

    r11071 r11562  
    108108
    109109            Timer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
     110            Timer(float interval, bool bLoop, std::function<void (void)> func, bool bKillAfterCall = false);
    110111
    111112            void setTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
     113            void setTimer(float interval, bool bLoop, std::function<void (void)> func, bool bKillAfterCall = false);
    112114
    113115            void run();
     
    153155
    154156            ExecutorPtr executor_;  //!< The executor of the function that will be called when the time expires
     157            std::function<void (void)> function_;
    155158
     159            bool isStdFunction_;
    156160            long long interval_;    //!< The time-interval in micro seconds
    157161            bool bLoop_;            //!< If true, the executor gets called every @a interval seconds
  • code/branches/ScriptableController_HS17/src/orxonox/Level.cc

    r11549 r11562  
    5757        this->xmlfilename_ = this->getFilename();
    5858        this->xmlfile_ = nullptr;
     59        this->controller_.reset(new ScriptableController());
    5960    }
    6061
     
    8485        XMLPortObject(Level, MeshLodInformation, "lodinformation", addLodInfo, getLodInfo, xmlelement, mode);
    8586        XMLPortObjectExtended(Level, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
    86 
    87         if(this->level_script_ != "")
    88         {
    89             this->controller_ = new ScriptableController(this->getContext());
    90             this->controller_->runScript(this->level_script_);
    91         }
    9287    }
    9388
     
    177172    {
    178173        this->objects_.push_back(object);
    179         if(this->controller_ != nullptr)
    180             object->registerToScriptableController(this->controller_);
     174        object->registerToScriptableController(this->controller_.get());
    181175    }
    182176
     
    214208        orxout(internal_info) << "player entered level (id: " << player->getClientID() << ", name: " << player->getName() << ')' << endl;
    215209        player->switchGametype(this->getGametype());
     210
     211        if(player->isHumanPlayer() && player->isLocalPlayer())
     212        {
     213            this->getScriptableController()->setPlayer(player);
     214            this->controller_->runScript(this->level_script_);
     215        }
    216216    }
    217217
  • code/branches/ScriptableController_HS17/src/orxonox/Level.h

    r11549 r11562  
    5757            MeshLodInformation* getLodInfo(std::string meshName) const;
    5858
     59            inline ScriptableController *getScriptableController(void)
     60                { return this->controller_.get(); }
     61
    5962
    6063        private:
     
    9295            std::map<std::string,MeshLodInformation*>  lodInformation_;
    9396
    94             ScriptableController* controller_;
     97            std::unique_ptr<ScriptableController> controller_;
    9598            std::string           level_script_;
    9699    };
  • code/branches/ScriptableController_HS17/src/orxonox/infos/HumanPlayer.cc

    r11071 r11562  
    3838#include "gametypes/Gametype.h"
    3939#include "overlays/OverlayGroup.h"
     40#include "Level.h"
     41#include "scriptablecontroller/scriptable_controller.h"
    4042
    4143namespace orxonox
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.cc

    r11552 r11562  
    22#include "scriptable_controller.h"
    33#include "luatb.h"
     4#include "infos/PlayerInfo.h"
    45
    56namespace orxonox
    67{
    78
    8 RegisterUnloadableClass(ScriptableController);
    9 
    109// 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 
    1710int ScriptableController::runScript(const std::string &file_path)
    1811{
     
    5750}
    5851
     52void ScriptableController::setPlayer(PlayerInfo *player)
     53{
     54    this->player_ = player;
     55}
     56
    5957void ScriptableController::registerWorldEntity(int id, WorldEntity *obj)
    6058{
     59    orxout(user_info) << "WorldEntity registered (id: " << id << ")" << std::endl;
    6160    this->worldEntities_[id] = obj;
    6261}
     
    6463void ScriptableController::registerControllableEntity(int id, ControllableEntity *obj)
    6564{
     65    orxout(user_info) << "ControllableEntity registered (id: " << id << ")" << std::endl;
    6666    this->worldEntities_[id] = obj;
    6767}
     
    6969WorldEntity *ScriptableController::getWorldEntityByID(int id) const
    7070{
     71    if(id == 0)
     72        return this->player_->getControllableEntity();
     73
    7174    auto obj = this->worldEntities_.find(id);
    7275    return obj != this->worldEntities_.end() ? obj->second : nullptr;
     
    7578ControllableEntity *ScriptableController::getControllableEntityByID(int id) const
    7679{
     80    if(id == 0)
     81        return this->player_->getControllableEntity();
     82
    7783    auto obj = this->controllabelEntities_.find(id);
    7884    return obj != this->controllabelEntities_.end() ? obj->second : nullptr;
    7985}
    8086
    81 void ScriptableController::registerTimeout(std::function<void ()> callback, double timeout)
     87void ScriptableController::addNearObjectHandler(int obj1, int obj2, double distance, std::function<void (int,int)> callback)
    8288{
    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();
    84102}
    85103
    86 void ScriptableController::tick(float dt)
     104void ScriptableController::objectMoved(WorldEntity *obj)
    87105{
    88     auto timeout = this->timeouts.begin();
     106    // Temp
     107    return;
    89108
    90     while(timeout != this->timeouts.end())
     109    auto &nearObjectHandlers = this->nearObjectHandlers_[obj];
     110    auto handler = nearObjectHandlers.begin();
     111    while(handler != nearObjectHandlers.end())
    91112    {
    92         timeout->second -= dt;
    93         if(timeout->second <= 0)
     113        WorldEntity *other = (*handler)->otherObject_;
     114        if((obj->getPosition() - other->getPosition()).length() < (*handler)->distance_)
    94115        {
    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);
    97126        }
    98127        else
    99128        {
    100             timeout++;
     129            handler++;
    101130        }
    102131    }
    103 
    104     Tickable::tick(dt);
    105132}
    106133
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller.h

    r11549 r11562  
    1717{
    1818
    19 class ScriptableController : public BaseObject, public Tickable
     19class ScriptableController
    2020{
    2121public:
    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    };
    2333
    2434    int runScript(const std::string &file_path);
    2535
     36    void setPlayer(PlayerInfo *player);
    2637    void registerWorldEntity(int id, WorldEntity *obj);
    2738    void registerControllableEntity(int id, ControllableEntity *obj);
     
    3041    ControllableEntity *getControllableEntityByID(int id) const;
    3142
    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);
    3344
    34     virtual void tick(float dt) override;
     45    void objectMoved(WorldEntity *obj);
    3546
    3647private:
    3748    std::list<std::unique_ptr<ScriptableControllerAPI> > apis_;
    38     ControllableEntity *player_; // TODO
     49    PlayerInfo *player_;
    3950    std::map<int, WorldEntity*> worldEntities_;
    4051    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_;
    4254
    4355    void printLuaError(lua_State *lua);
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.cc

    r11552 r11562  
    33#include "luatb.h"
    44#include "scriptable_controller.h"
     5#include "tools/Timer.h"
    56
    67namespace orxonox
     
    1314
    1415    // Haven't found a shorter way yet to write that...
     16    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::orxPrint)>::registerFunction<&ScriptableControllerAPI::orxPrint>(this, lua, "orxPrint");
    1517    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAfterTimeout)>::registerFunction<&ScriptableControllerAPI::registerAfterTimeout>(this, lua, "registerAfterTimeout");
    1618    LuaTB<ScriptableControllerAPI, decltype(&ScriptableControllerAPI::registerAtNearObject)>::registerFunction<&ScriptableControllerAPI::registerAtNearObject>(this, lua, "registerAtNearObject");
     
    2628}
    2729
     30void ScriptableControllerAPI::orxPrint(std::string msg)
     31{
     32    orxout(user_info) << msg << std::endl;
     33}
     34
    2835void ScriptableControllerAPI::registerAfterTimeout(std::function<void (void)> callback, double timeout)
    2936{
    30     // TODO Extend timer class to accept std::function
    31     this->controller_->registerTimeout(callback, timeout);
     37    // Kills itself when the timer fires
     38    new Timer(timeout, false, callback, true);
    3239}
    3340
    3441int ScriptableControllerAPI::registerAtNearObject(std::function<void (int, int)> callback, int obj1, int obj2, double distance)
    3542{
    36 
     43    //this->controller_->addNearObjectHandler(obj1, obj1, distance, callback);
    3744}
    3845
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/scriptable_controller_api.h

    r11549 r11562  
    2222    void testOutput(std::function<void(std::string)> callback);
    2323
     24    void orxPrint(std::string msg);
    2425    void registerAfterTimeout(std::function<void (void)> callback, double timeout);
    2526    int registerAtNearObject(std::function<void(int, int)> callback, int obj1, int obj2, double distance);
  • code/branches/ScriptableController_HS17/src/orxonox/worldentities/MobileEntity.cc

    r11071 r11562  
    3636
    3737#include "Scene.h"
     38#include "Level.h"
     39#include "scriptablecontroller/scriptable_controller.h"
    3840
    3941namespace orxonox
     
    113115
    114116        this->node_->setPosition(position);
     117        this->getLevel()->getScriptableController()->objectMoved(this);
    115118    }
    116119
  • code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.cc

    r11518 r11562  
    184184        // Attached collision shapes
    185185        XMLPortObject(WorldEntity, CollisionShape, "collisionShapes", attachCollisionShape, getAttachedCollisionShape, xmlelement, mode);
     186
     187        orxout(user_info) << "ID loaded" << std::endl;
    186188    }
    187189
     
    280282    void WorldEntity::registerToScriptableController(ScriptableController *controller)
    281283    {
     284        orxout(user_info) << "Registering object to SC (" << this->id_ << ")" << std::endl;
    282285        controller->registerWorldEntity(this->id_, this);
    283286    }
  • code/branches/ScriptableController_HS17/src/orxonox/worldentities/WorldEntity.h

    r11518 r11562  
    110110            virtual void changedActivity(void) override;
    111111            virtual void changedVisibility(void) override;
     112
     113            inline int getID(void)
     114                { return this->id_; }
    112115
    113116            inline void setID(int id)
Note: See TracChangeset for help on using the changeset viewer.