Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc @ 10052

Last change on this file since 10052 was 10047, checked in by smerkli, 11 years ago

Controller switching works now, however lua script
execution is blocking, which means we can only schedule
stuff in them, not leave them running in realtime

File size: 4.6 KB
Line 
1/*
2 * First try of a ControllerDirector. Target: An event occurs in the levelTry.oxw
3 * file, which is "heard" by an object of the type of this class. It then SHOULD
4 * (because it is not working) execute the party function.
5 */
6
7#include "ControllerDirector.h"
8#include "ScriptController.h"
9#include "core/CoreIncludes.h"
10
11//#include "network/NetworkFunction.h"
12
13#include "infos/HumanPlayer.h"
14#include "interfaces/PlayerTrigger.h"
15#include "worldentities/pawns/Pawn.h"
16#include "core/LuaState.h"
17
18
19namespace orxonox
20{
21    RegisterClass(ControllerDirector);
22
23    ControllerDirector::ControllerDirector(Context* context) : ArtificialController(context)
24    {
25        // Register the object with the framework
26        RegisterObject(ControllerDirector);
27
28        // output a message to ensure we know the constructor was run
29        orxout()<<"hello universe constructor"<< endl;
30
31        // Initialize member variables
32        this->player_ = NULL;
33        this->entity_ = NULL;
34        this->pTrigger_ = NULL;
35        this->context_ = context;
36    }
37
38    void ControllerDirector::XMLPort(Element& xmlelement, XMLPort::Mode mode)
39    {
40        SUPER(ControllerDirector, XMLPort, xmlelement, mode);
41
42        orxout()<< "ControllerDirector::XMLPort " 
43          << "An instance of ControllerDirector has been created." << endl;
44    }
45
46    void ControllerDirector::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
47    {
48        // Call the xmleventport functions of the classes we derive from
49        SUPER(ControllerDirector, XMLEventPort, xmlelement, mode);
50
51        // Add an event sink for a "takeControl" event, which leads to the
52        // function takeControl() being called.
53        XMLPortEventSink(ControllerDirector, BaseObject, "takeControl", 
54          takeControl, xmlelement, mode);
55    }
56
57
58
59
60    void ControllerDirector::takeControl(Controller * controller, BaseObject * trigger) 
61    {
62       /* Output a message confirming that the function was called */
63       orxout()<<"test takecontrol."<< endl;
64
65       /* First, we set up a new controller to attach to the unit that
66        * triggered our event.
67        */
68       static int ctrlid = 1;
69       bool prepok = preparationToTakeControl(trigger);
70       if( prepok == true) 
71       {
72         /* Create a scriptcontroller object */
73         ScriptController *newctrl = new ScriptController(this->context_);
74
75         /* Make the player we were given its slave */
76         newctrl->setPlayer(this->player_);
77
78         /* Start controlling that object */
79         newctrl->takeControl(ctrlid);
80       }
81       else
82         return;
83       
84       /* Set up a luastate to use for running the scripts */
85       LuaState * ls = new LuaState();
86       
87       /* Assemble a string to define a controller id variable in lua space */
88       std::stringstream tmp;
89       tmp << "newctrlid = " << ctrlid;
90       std::string todo = tmp.str();
91
92       /* Run the string using the luastate created earlier */
93       ls->doString(todo);
94
95       /* Now run the script on this controller. This will still have the above
96        * variable "newctrlid" defined, which means it can make use of it.
97        */
98       ls->doFile("testscript.lua");
99
100       /* Increase the controller ID so we have a different one for
101        * the next time it is triggered */
102       ctrlid += 1;
103    } 
104
105       
106    bool ControllerDirector::preparationToTakeControl(BaseObject * trigger) 
107    {
108        this->pTrigger_ = orxonox_cast<PlayerTrigger*>(trigger);
109        this->player_ = NULL;
110
111        orxout() << "Preparation to take Control!" << endl; 
112
113        // Check whether it is a player trigger and extract pawn from it
114        if(this->pTrigger_ != NULL)
115        {
116            // Get the object which triggered the event.
117            this->player_ = this->pTrigger_->getTriggeringPlayer(); 
118
119            // Check if there actually was a player returned.
120            if( this->player_ == NULL) return false;
121        }
122        else
123        {
124            orxout() << "ControllerDirector::preparationToTakeControl " 
125              << "Not a player trigger, can't extract pawn from it.." << endl;
126            return false;
127        }
128
129        this->entity_ = this->player_->getControllableEntity();
130        assert(this->entity_);
131
132        return true;
133    }
134
135    /* // Currently unused
136    void ControllerDirector::setNewController(Controller * controller) {
137
138
139        orxout() << "New Controller is going to be set!" << endl;
140
141        this->entity_->setDestroyWhenPlayerLeft(false);
142        this->player_->pauseControl();
143        this->entity_->setController(controller);
144        this->player_->startControl(this->entity_);
145        //this->setControllableEntity(this->entity_);
146    }
147    */
148       
149   
150
151}
152
153
154
155
Note: See TracBrowser for help on using the repository browser.