Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fixed some more things. What works now:

  • Setting an ID for the scriptcontroller to be used from C++ in lua
  • Running a script from the controllerdirector triggered by an event
  • Getting an instance of a ScriptController object pointer from lua, and also calling a function of this object with parameters.

Next steps will be implementing different IDs per controller
object and actually swapping out controllers.

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