Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Figuring out when the different things are ready in orxonox

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.