[10016] | 1 | /* |
---|
[10046] | 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. |
---|
[10016] | 5 | */ |
---|
| 6 | |
---|
[10024] | 7 | #include "ControllerDirector.h" |
---|
[11167] | 8 | #include "NewScriptController.h" |
---|
[10012] | 9 | #include "core/CoreIncludes.h" |
---|
| 10 | |
---|
[10024] | 11 | //#include "network/NetworkFunction.h" |
---|
| 12 | |
---|
| 13 | #include "infos/HumanPlayer.h" |
---|
| 14 | #include "interfaces/PlayerTrigger.h" |
---|
| 15 | #include "worldentities/pawns/Pawn.h" |
---|
[10035] | 16 | #include "core/LuaState.h" |
---|
[10024] | 17 | |
---|
[10026] | 18 | |
---|
[10016] | 19 | namespace orxonox |
---|
| 20 | { |
---|
[10024] | 21 | RegisterClass(ControllerDirector); |
---|
[10012] | 22 | |
---|
[10024] | 23 | ControllerDirector::ControllerDirector(Context* context) : ArtificialController(context) |
---|
[10016] | 24 | { |
---|
[10046] | 25 | // Register the object with the framework |
---|
[10024] | 26 | RegisterObject(ControllerDirector); |
---|
[10046] | 27 | |
---|
| 28 | // output a message to ensure we know the constructor was run |
---|
[10077] | 29 | orxout(verbose)<<"hello universe constructor blablub"<< endl; |
---|
[10012] | 30 | |
---|
[10057] | 31 | |
---|
| 32 | |
---|
[10046] | 33 | // Initialize member variables |
---|
[11071] | 34 | this->player_ = nullptr; |
---|
| 35 | this->entity_ = nullptr; |
---|
| 36 | this->pTrigger_ = nullptr; |
---|
[10047] | 37 | this->context_ = context; |
---|
[10016] | 38 | } |
---|
[10012] | 39 | |
---|
[10024] | 40 | void ControllerDirector::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[10016] | 41 | { |
---|
[10024] | 42 | SUPER(ControllerDirector, XMLPort, xmlelement, mode); |
---|
[10622] | 43 | XMLPortParam(ControllerDirector, "scriptname", setScriptName, getScriptName, xmlelement, mode).defaultValues("testscript"); |
---|
[10012] | 44 | |
---|
[10077] | 45 | orxout(verbose)<< "ControllerDirector::XMLPort " |
---|
[10046] | 46 | << "An instance of ControllerDirector has been created." << endl; |
---|
[10016] | 47 | } |
---|
| 48 | |
---|
[10024] | 49 | void ControllerDirector::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[10012] | 50 | { |
---|
[10046] | 51 | // Call the xmleventport functions of the classes we derive from |
---|
[10024] | 52 | SUPER(ControllerDirector, XMLEventPort, xmlelement, mode); |
---|
[10012] | 53 | |
---|
[10046] | 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); |
---|
[10024] | 58 | } |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | |
---|
[10046] | 63 | void ControllerDirector::takeControl(Controller * controller, BaseObject * trigger) |
---|
| 64 | { |
---|
| 65 | /* Output a message confirming that the function was called */ |
---|
[11167] | 66 | orxout()<<"test takecontrol."<< endl; |
---|
[10016] | 67 | |
---|
[10046] | 68 | /* First, we set up a new controller to attach to the unit that |
---|
| 69 | * triggered our event. |
---|
| 70 | */ |
---|
[10047] | 71 | static int ctrlid = 1; |
---|
| 72 | bool prepok = preparationToTakeControl(trigger); |
---|
| 73 | if( prepok == true) |
---|
| 74 | { |
---|
| 75 | /* Create a scriptcontroller object */ |
---|
[11167] | 76 | NewScriptController *newctrl = new NewScriptController(this->context_); |
---|
[10047] | 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; |
---|
[10622] | 86 | |
---|
[10046] | 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; |
---|
[10065] | 92 | tmp << "newctrlid = " << ctrlid << endl; |
---|
[10046] | 93 | std::string todo = tmp.str(); |
---|
[10059] | 94 | |
---|
[10046] | 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 | */ |
---|
[10622] | 101 | std::string scr = this->scriptname_ + ".lua"; |
---|
| 102 | ls->doFile(scr); |
---|
[10057] | 103 | |
---|
[10046] | 104 | /* Increase the controller ID so we have a different one for |
---|
| 105 | * the next time it is triggered */ |
---|
| 106 | ctrlid += 1; |
---|
[11172] | 107 | |
---|
| 108 | orxout()<<"swag"<< endl; |
---|
[10024] | 109 | } |
---|
| 110 | |
---|
[10047] | 111 | bool ControllerDirector::preparationToTakeControl(BaseObject * trigger) |
---|
| 112 | { |
---|
| 113 | this->pTrigger_ = orxonox_cast<PlayerTrigger*>(trigger); |
---|
[11071] | 114 | this->player_ = nullptr; |
---|
[10016] | 115 | |
---|
[11167] | 116 | orxout() << "Preparation to take Control!" << endl; |
---|
[10047] | 117 | |
---|
[10016] | 118 | // Check whether it is a player trigger and extract pawn from it |
---|
[11071] | 119 | if(this->pTrigger_ != nullptr) |
---|
[10016] | 120 | { |
---|
[10047] | 121 | // Get the object which triggered the event. |
---|
| 122 | this->player_ = this->pTrigger_->getTriggeringPlayer(); |
---|
| 123 | |
---|
| 124 | // Check if there actually was a player returned. |
---|
[11172] | 125 | if( this->player_ == nullptr) { |
---|
| 126 | orxout()<<"swag control"<< endl; |
---|
| 127 | |
---|
| 128 | return false; |
---|
| 129 | } |
---|
[10016] | 130 | } |
---|
| 131 | else |
---|
| 132 | { |
---|
[11167] | 133 | orxout() << "ControllerDirector::preparationToTakeControl " |
---|
[10047] | 134 | << "Not a player trigger, can't extract pawn from it.." << endl; |
---|
[10016] | 135 | return false; |
---|
| 136 | } |
---|
| 137 | |
---|
[10215] | 138 | this->entity_ = this->player_->getControllableEntity(); |
---|
| 139 | assert(this->entity_); |
---|
[10016] | 140 | |
---|
[10047] | 141 | return true; |
---|
[10016] | 142 | } |
---|
| 143 | |
---|
[10047] | 144 | /* // Currently unused |
---|
[10024] | 145 | void ControllerDirector::setNewController(Controller * controller) { |
---|
[10016] | 146 | |
---|
| 147 | |
---|
[10024] | 148 | orxout() << "New Controller is going to be set!" << endl; |
---|
[10016] | 149 | |
---|
[10024] | 150 | this->entity_->setDestroyWhenPlayerLeft(false); |
---|
| 151 | this->player_->pauseControl(); |
---|
| 152 | this->entity_->setController(controller); |
---|
[10047] | 153 | this->player_->startControl(this->entity_); |
---|
| 154 | //this->setControllableEntity(this->entity_); |
---|
[10024] | 155 | } |
---|
[10047] | 156 | */ |
---|
[10016] | 157 | |
---|
[10012] | 158 | |
---|
| 159 | |
---|
[10024] | 160 | } |
---|
[10016] | 161 | |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | |
---|