[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 |
---|
[10016] | 29 | orxout()<<"hello universe constructor"<< endl; |
---|
[10012] | 30 | |
---|
[10046] | 31 | // Initialize member variables |
---|
| 32 | this->player_ = NULL; |
---|
| 33 | this->entity_ = NULL; |
---|
| 34 | this->pTrigger_ = NULL; |
---|
[10047] | 35 | this->context_ = context; |
---|
[10016] | 36 | } |
---|
[10012] | 37 | |
---|
[10024] | 38 | void ControllerDirector::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[10016] | 39 | { |
---|
[10024] | 40 | SUPER(ControllerDirector, XMLPort, xmlelement, mode); |
---|
[10012] | 41 | |
---|
[10046] | 42 | orxout()<< "ControllerDirector::XMLPort " |
---|
| 43 | << "An instance of ControllerDirector has been created." << endl; |
---|
[10016] | 44 | } |
---|
| 45 | |
---|
[10024] | 46 | void ControllerDirector::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[10012] | 47 | { |
---|
[10046] | 48 | // Call the xmleventport functions of the classes we derive from |
---|
[10024] | 49 | SUPER(ControllerDirector, XMLEventPort, xmlelement, mode); |
---|
[10012] | 50 | |
---|
[10046] | 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); |
---|
[10024] | 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | |
---|
[10046] | 60 | void ControllerDirector::takeControl(Controller * controller, BaseObject * trigger) |
---|
| 61 | { |
---|
| 62 | /* Output a message confirming that the function was called */ |
---|
| 63 | orxout()<<"test takecontrol."<< endl; |
---|
[10016] | 64 | |
---|
[10046] | 65 | /* First, we set up a new controller to attach to the unit that |
---|
| 66 | * triggered our event. |
---|
| 67 | */ |
---|
[10047] | 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; |
---|
[10046] | 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; |
---|
[10024] | 103 | } |
---|
| 104 | |
---|
[10016] | 105 | |
---|
[10047] | 106 | bool ControllerDirector::preparationToTakeControl(BaseObject * trigger) |
---|
| 107 | { |
---|
| 108 | this->pTrigger_ = orxonox_cast<PlayerTrigger*>(trigger); |
---|
[10016] | 109 | this->player_ = NULL; |
---|
| 110 | |
---|
[10024] | 111 | orxout() << "Preparation to take Control!" << endl; |
---|
[10047] | 112 | |
---|
[10016] | 113 | // Check whether it is a player trigger and extract pawn from it |
---|
[10024] | 114 | if(this->pTrigger_ != NULL) |
---|
[10016] | 115 | { |
---|
[10047] | 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; |
---|
[10016] | 121 | } |
---|
| 122 | else |
---|
| 123 | { |
---|
[10047] | 124 | orxout() << "ControllerDirector::preparationToTakeControl " |
---|
| 125 | << "Not a player trigger, can't extract pawn from it.." << endl; |
---|
[10016] | 126 | return false; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | this->entity_ = this->player_->getControllableEntity(); |
---|
| 130 | assert(this->entity_); |
---|
| 131 | |
---|
[10047] | 132 | return true; |
---|
[10016] | 133 | } |
---|
| 134 | |
---|
[10047] | 135 | /* // Currently unused |
---|
[10024] | 136 | void ControllerDirector::setNewController(Controller * controller) { |
---|
[10016] | 137 | |
---|
| 138 | |
---|
[10024] | 139 | orxout() << "New Controller is going to be set!" << endl; |
---|
[10016] | 140 | |
---|
[10024] | 141 | this->entity_->setDestroyWhenPlayerLeft(false); |
---|
| 142 | this->player_->pauseControl(); |
---|
| 143 | this->entity_->setController(controller); |
---|
[10047] | 144 | this->player_->startControl(this->entity_); |
---|
| 145 | //this->setControllableEntity(this->entity_); |
---|
[10024] | 146 | } |
---|
[10047] | 147 | */ |
---|
[10016] | 148 | |
---|
[10012] | 149 | |
---|
| 150 | |
---|
[10024] | 151 | } |
---|
[10016] | 152 | |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | |
---|