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 | |
---|
19 | namespace 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(verbose)<<"hello universe constructor blablub"<< endl; |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | // Initialize member variables |
---|
34 | this->player_ = nullptr; |
---|
35 | this->entity_ = nullptr; |
---|
36 | this->pTrigger_ = nullptr; |
---|
37 | this->context_ = context; |
---|
38 | } |
---|
39 | |
---|
40 | void ControllerDirector::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
41 | { |
---|
42 | SUPER(ControllerDirector, XMLPort, xmlelement, mode); |
---|
43 | XMLPortParam(ControllerDirector, "scriptname", setScriptName, getScriptName, xmlelement, mode).defaultValues("testscript"); |
---|
44 | |
---|
45 | orxout(verbose)<< "ControllerDirector::XMLPort " |
---|
46 | << "An instance of ControllerDirector has been created." << endl; |
---|
47 | } |
---|
48 | |
---|
49 | void ControllerDirector::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
50 | { |
---|
51 | // Call the xmleventport functions of the classes we derive from |
---|
52 | SUPER(ControllerDirector, XMLEventPort, xmlelement, mode); |
---|
53 | |
---|
54 | // Add an event sink for a "takeControl" event, which leads to the |
---|
55 | // function takeControl() being called. |
---|
56 | XMLPortEventSink(ControllerDirector, BaseObject, "takeControl", |
---|
57 | takeControl, xmlelement, mode); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | void ControllerDirector::takeControl(Controller * controller, BaseObject * trigger) |
---|
64 | { |
---|
65 | /* Output a message confirming that the function was called */ |
---|
66 | orxout(verbose)<<"test takecontrol."<< endl; |
---|
67 | |
---|
68 | /* First, we set up a new controller to attach to the unit that |
---|
69 | * triggered our event. |
---|
70 | */ |
---|
71 | static int ctrlid = 1; |
---|
72 | bool prepok = preparationToTakeControl(trigger); |
---|
73 | if( prepok == true) |
---|
74 | { |
---|
75 | /* Create a scriptcontroller object */ |
---|
76 | ScriptController *newctrl = new ScriptController(this->context_); |
---|
77 | |
---|
78 | /* Make the player we were given its slave */ |
---|
79 | newctrl->setPlayer(this->player_); |
---|
80 | |
---|
81 | /* Start controlling that object */ |
---|
82 | newctrl->takeControl(ctrlid); |
---|
83 | } |
---|
84 | else |
---|
85 | return; |
---|
86 | |
---|
87 | /* Set up a luastate to use for running the scripts */ |
---|
88 | LuaState * ls = new LuaState(); |
---|
89 | |
---|
90 | /* Assemble a string to define a controller id variable in lua space */ |
---|
91 | std::stringstream tmp; |
---|
92 | tmp << "newctrlid = " << ctrlid << endl; |
---|
93 | std::string todo = tmp.str(); |
---|
94 | |
---|
95 | /* Run the string using the luastate created earlier */ |
---|
96 | ls->doString(todo); |
---|
97 | |
---|
98 | /* Now run the script on this controller. This will still have the above |
---|
99 | * variable "newctrlid" defined, which means it can make use of it. |
---|
100 | */ |
---|
101 | std::string scr = this->scriptname_ + ".lua"; |
---|
102 | ls->doFile(scr); |
---|
103 | |
---|
104 | /* Increase the controller ID so we have a different one for |
---|
105 | * the next time it is triggered */ |
---|
106 | ctrlid += 1; |
---|
107 | } |
---|
108 | |
---|
109 | bool ControllerDirector::preparationToTakeControl(BaseObject * trigger) |
---|
110 | { |
---|
111 | this->pTrigger_ = orxonox_cast<PlayerTrigger*>(trigger); |
---|
112 | this->player_ = nullptr; |
---|
113 | |
---|
114 | orxout(verbose) << "Preparation to take Control!" << endl; |
---|
115 | |
---|
116 | // Check whether it is a player trigger and extract pawn from it |
---|
117 | if(this->pTrigger_ != nullptr) |
---|
118 | { |
---|
119 | // Get the object which triggered the event. |
---|
120 | this->player_ = this->pTrigger_->getTriggeringPlayer(); |
---|
121 | |
---|
122 | // Check if there actually was a player returned. |
---|
123 | if( this->player_ == nullptr) return false; |
---|
124 | } |
---|
125 | else |
---|
126 | { |
---|
127 | orxout(verbose) << "ControllerDirector::preparationToTakeControl " |
---|
128 | << "Not a player trigger, can't extract pawn from it.." << endl; |
---|
129 | return false; |
---|
130 | } |
---|
131 | |
---|
132 | this->entity_ = this->player_->getControllableEntity(); |
---|
133 | assert(this->entity_); |
---|
134 | |
---|
135 | return true; |
---|
136 | } |
---|
137 | |
---|
138 | /* // Currently unused |
---|
139 | void ControllerDirector::setNewController(Controller * controller) { |
---|
140 | |
---|
141 | |
---|
142 | orxout() << "New Controller is going to be set!" << endl; |
---|
143 | |
---|
144 | this->entity_->setDestroyWhenPlayerLeft(false); |
---|
145 | this->player_->pauseControl(); |
---|
146 | this->entity_->setController(controller); |
---|
147 | this->player_->startControl(this->entity_); |
---|
148 | //this->setControllableEntity(this->entity_); |
---|
149 | } |
---|
150 | */ |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | |
---|