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