[10014] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "ScriptController.h" |
---|
[10047] | 30 | #include "infos/PlayerInfo.h" |
---|
[10014] | 31 | #include "core/CoreIncludes.h" |
---|
[10028] | 32 | #include "worldentities/ControllableEntity.h" |
---|
[10045] | 33 | #include "core/LuaState.h" |
---|
[10034] | 34 | #include <cmath> |
---|
[10014] | 35 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
[10048] | 38 | float scTime=0; /*initialise time, to coordinate eventTime*/ |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | std::vector<event> eventList; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
[10014] | 48 | RegisterClass(ScriptController); |
---|
| 49 | |
---|
[10028] | 50 | //ScriptController::ScriptController(Context* context, ControllableEntity* CE) : ArtificialController(context) |
---|
| 51 | ScriptController::ScriptController(Context* context) : ArtificialController(context) |
---|
[10014] | 52 | { |
---|
| 53 | RegisterObject(ScriptController); |
---|
[10028] | 54 | //set_controlled(CE); |
---|
[10047] | 55 | this->ctrlid_ = 0; |
---|
[10014] | 56 | } |
---|
| 57 | |
---|
[10047] | 58 | void ScriptController::takeControl(int ctrlid) |
---|
[10014] | 59 | { |
---|
[10047] | 60 | orxout() << "ScriptController: Taking control" << endl; |
---|
| 61 | orxout() << "This-pointer: " << this << endl; |
---|
| 62 | this->ctrlid_ = ctrlid; |
---|
| 63 | this->entity_ = this->player_->getControllableEntity(); |
---|
| 64 | assert(this->entity_); |
---|
| 65 | |
---|
| 66 | this->entity_->setDestroyWhenPlayerLeft(false); |
---|
| 67 | this->player_->pauseControl(); |
---|
| 68 | this->entity_->setController(this); |
---|
| 69 | this->setControllableEntity(this->entity_); |
---|
[10014] | 70 | } |
---|
| 71 | |
---|
[10047] | 72 | /* Yet to be implemented and tested */ |
---|
| 73 | //void ScriptController::yieldControl() |
---|
| 74 | //{ |
---|
| 75 | //this->player_->startControl(this->entity_); |
---|
| 76 | //this->setActive(false); |
---|
| 77 | //this->controllableEntity_ = NULL; |
---|
| 78 | //} |
---|
| 79 | |
---|
[10014] | 80 | void ScriptController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 81 | { |
---|
[10028] | 82 | //XMLPortParam(ScriptController, BaseObject, "lsrc", set_luasrc, xmlelement, mode); |
---|
[10014] | 83 | |
---|
| 84 | } |
---|
| 85 | |
---|
[10034] | 86 | const Vector3& ScriptController::getPosition() |
---|
[10020] | 87 | { |
---|
[10047] | 88 | return this->entity_->getPosition(); |
---|
[10028] | 89 | } |
---|
[10014] | 90 | |
---|
[10045] | 91 | ScriptController* ScriptController::getScriptController() |
---|
| 92 | { |
---|
[10046] | 93 | /* Output a message that confirms this function was called */ |
---|
[10045] | 94 | orxout() << "Great success!" << std::endl; |
---|
[10046] | 95 | |
---|
[10047] | 96 | /* Debugging: print all the scriptcontroller object pointers */ |
---|
[10045] | 97 | for(ObjectList<ScriptController>::iterator it = |
---|
| 98 | ObjectList<ScriptController>::begin(); |
---|
| 99 | it != ObjectList<ScriptController>::end(); ++it) |
---|
[10047] | 100 | { orxout() << "Have object in list: " << *it << endl; } |
---|
| 101 | |
---|
| 102 | /* Find the first one with a nonzero ID */ |
---|
| 103 | for(ObjectList<ScriptController>::iterator it = |
---|
| 104 | ObjectList<ScriptController>::begin(); |
---|
| 105 | it != ObjectList<ScriptController>::end(); ++it) |
---|
[10045] | 106 | { |
---|
| 107 | // TODO: do some selection here. Currently just returns the first one |
---|
[10047] | 108 | if( (*it)->getID() > 0 ) |
---|
| 109 | return *it; |
---|
[10045] | 110 | |
---|
| 111 | } |
---|
| 112 | return NULL; |
---|
| 113 | } |
---|
| 114 | |
---|
[10048] | 115 | void ScriptController::execute(event ev) |
---|
| 116 | { |
---|
| 117 | if(ev.fctName=="moveToPosition_beta") |
---|
| 118 | { |
---|
| 119 | moveToPosition_beta(ev.xCoord,ev.yCoord,ev.zCoord); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | |
---|
[10047] | 124 | void ScriptController::tick(float dt) |
---|
| 125 | { |
---|
| 126 | /* If this controller has no entity entry, do nothing */ |
---|
| 127 | if( !(this->entity_) ) |
---|
| 128 | return; |
---|
[10045] | 129 | |
---|
[10047] | 130 | //orxout() << "Rotating!" << endl; |
---|
[10045] | 131 | |
---|
[10047] | 132 | //this->entity_->rotateYaw(-1.0f * 100.0f * dt); |
---|
| 133 | //this->entity_->rotatePitch(0.8f * 100.0f); |
---|
| 134 | |
---|
[10048] | 135 | if(eventList[0].eventTime<=scTime) |
---|
| 136 | { |
---|
| 137 | /*TO DO: execute the function: eventList[0].fctName*/ |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | eventList.erase(eventList.begin()); |
---|
| 141 | } |
---|
| 142 | |
---|
[10047] | 143 | SUPER(ScriptController, tick, dt); |
---|
[10048] | 144 | |
---|
| 145 | scTime=scTime+dt; |
---|
[10047] | 146 | } |
---|
| 147 | |
---|
| 148 | |
---|
[10048] | 149 | |
---|
| 150 | |
---|
[10034] | 151 | void ScriptController::moveToPosition_beta(float x, float y, float z ) |
---|
| 152 | { |
---|
[10047] | 153 | //const Vector3 local = this->getPosition(); |
---|
| 154 | const Vector3 target = Vector3(100*x,100*y,100*z); |
---|
| 155 | //Vector3 way = target-local; |
---|
| 156 | orxout() << "Moving This-pointer: " << this << endl; |
---|
[10038] | 157 | |
---|
[10046] | 158 | |
---|
[10047] | 159 | this->entity_->lookAt(target); |
---|
| 160 | this->entity_->moveFrontBack(-1000*target.length()); |
---|
[10034] | 161 | |
---|
[10046] | 162 | |
---|
| 163 | /* This works fine */ |
---|
| 164 | orxout()<<x<<" "<<y<<" "<<z<<endl; |
---|
[10034] | 165 | } |
---|
| 166 | |
---|
[10048] | 167 | void ScriptController::eventScheduler(std::string instruction, float x, float y, float z, float executionTime) |
---|
| 168 | { |
---|
| 169 | /*put data (from LUA) into time-sorted eventList*/ |
---|
| 170 | /*nimmt den befehl und die argumente aus luascript und ertellt einen struct pro event, diese structs werden sortiert nach eventTime*/ |
---|
| 171 | struct event tmp; |
---|
| 172 | tmp.fctName=instruction; |
---|
| 173 | tmp.xCoord=x; |
---|
| 174 | tmp.yCoord=y; |
---|
| 175 | tmp.zCoord=z; |
---|
| 176 | tmp.eventTime=executionTime; |
---|
[10047] | 177 | |
---|
[10048] | 178 | for(unsigned int i=0;i<eventList.size();i++) |
---|
| 179 | { |
---|
| 180 | if(tmp.eventTime<eventList[i].eventTime) |
---|
| 181 | { |
---|
| 182 | std::vector<event>::iterator it = eventList.begin(); |
---|
[10034] | 183 | |
---|
[10048] | 184 | eventList.insert(it+(i+1),tmp); |
---|
| 185 | break; |
---|
| 186 | } |
---|
| 187 | if(i==eventList.size()-1) |
---|
| 188 | { |
---|
| 189 | std::vector<event>::iterator it = eventList.end(); |
---|
[10014] | 190 | |
---|
[10048] | 191 | eventList.insert(it,tmp); |
---|
[10028] | 192 | |
---|
[10048] | 193 | } |
---|
[10028] | 194 | |
---|
[10048] | 195 | } |
---|
| 196 | |
---|
| 197 | } |
---|
[10028] | 198 | |
---|
[10048] | 199 | |
---|
| 200 | |
---|
| 201 | /* TODO: struct event erweitern um mehr funktionen benutzen zu koennen |
---|
| 202 | |
---|
| 203 | mehr funktionen definieren (und dann in execute if(...)) |
---|
| 204 | NB: viele noetige funktionen sind schon in artificial- bzw formationcontroller vorhanden */ |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | |
---|
[10014] | 208 | } |
---|