Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/controllers/ScriptController.cc @ 10262

Last change on this file since 10262 was 10262, checked in by landauf, 9 years ago

eol-style native. no changes in code.

  • Property svn:eol-style set to native
File size: 8.4 KB
RevLine 
[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
36namespace orxonox
37{
38    RegisterClass(ScriptController);
39
[10028]40    ScriptController::ScriptController(Context* context) : ArtificialController(context)
[10014]41    {
42        RegisterObject(ScriptController);
[10065]43
44        /* By default, this controller has ID 0, which means it is not assigned
45         * to anything yet.
46         */
[10047]47        this->ctrlid_ = 0;
[10065]48
49        /* Set default values for all variables */
50        /* - pointers to zero */
51        this->player_ = NULL;
52        this->entity_ = NULL;
53
54        /* - times */
55        this->scTime = 0.0f;
56        this->eventTime = 0.0f;
57
58        /* - Points in space */
59        this->startpos = Vector3(0,0,0);
[10066]60        //this->lookAtPosition = Vector3(0,0,0);
[10065]61
62        /* - Processing flag */
63        this->processing = false;
64
65        /* - Counters */
66        this->eventno = 0;
67
[10014]68    }
69
[10047]70    void ScriptController::takeControl(int ctrlid)
[10014]71    {
[10065]72        /* Output some debugging information */
[10077]73        orxout(verbose) << "ScriptController: Taking control" << endl;
74        orxout(verbose) << "This-pointer: " << this << endl;
[10065]75
76        /* Set the controller ID (the argument here should be nonzero) */
[10047]77        this->ctrlid_ = ctrlid;
[10065]78
79        /* Store the entity pointer in a private variable */
[10047]80        this->entity_ = this->player_->getControllableEntity();
81        assert(this->entity_);
[10065]82         
83        /* Add the controller here to this entity. Apparently this still leaves
84         * any preexisting human controllers in place.
85         */
[10047]86        this->entity_->setDestroyWhenPlayerLeft(false);
87        this->player_->pauseControl();
88        this->entity_->setController(this);
89        this->setControllableEntity(this->entity_);
[10065]90        this->entity_->mouseLook();
91        this->entity_->setVisible(false);
[10014]92    }
93
[10034]94    const Vector3& ScriptController::getPosition()
[10020]95    {
[10065]96      return this->entity_->getPosition();
[10028]97    }
[10014]98
[10045]99    ScriptController* ScriptController::getScriptController()
100    {
[10046]101      /* Output a message that confirms this function was called */
[10077]102      orxout(verbose) << "Great success!" << std::endl;
[10046]103
[10047]104      /* Debugging: print all the scriptcontroller object pointers */
[10045]105      for(ObjectList<ScriptController>::iterator it = 
106        ObjectList<ScriptController>::begin(); 
107        it != ObjectList<ScriptController>::end(); ++it)
[10077]108      { orxout(verbose) << "Have object in list: " << *it << endl; }
[10047]109
110      /* Find the first one with a nonzero ID */
111      for(ObjectList<ScriptController>::iterator it = 
112        ObjectList<ScriptController>::begin(); 
113        it != ObjectList<ScriptController>::end(); ++it)
[10045]114      { 
115        // TODO: do some selection here. Currently just returns the first one
[10047]116        if( (*it)->getID() > 0 )
[10077]117        { orxout(verbose) << "Controller to return: " << *it << endl;
[10047]118          return *it; 
[10065]119        }
[10045]120     
121      }
122      return NULL;
123    }
124
[10048]125    void ScriptController::execute(event ev)
126    {
[10066]127        /* Debugging output */
128        //orxout() << "Executing event " << ev.fctName
129          //<< " with parameters:\n "
130          //<< ev.x1 << " " << ev.y1 << " " << ev.z1 << "\n"
131          //<< ev.x2 << " " << ev.y2 << " " << ev.z2 << "\n"
132          //<< ev.duration << endl;
[10057]133
[10066]134        /* Event is starting, hence set the time to 0 */
[10065]135        this->eventTime = 0.0f;
136        this->processing = true;
137
[10066]138        /* Copy the event into the currentEvent holder */
139        this->currentEvent = ev;
140
141        /* Store starting position */
142        this->startpos = this->entity_->getPosition();
[10048]143    }
144
145
[10047]146    void ScriptController::tick(float dt)
147    {
[10065]148        /* Call the tick function of the classes we derive from */
149        SUPER(ScriptController, tick, dt);
[10059]150
[10047]151        /* If this controller has no entity entry, do nothing */
[10066]152        if( !(this->entity_) ) return;
[10045]153
[10065]154        /* See if time has come for the next event to be run */
155        if(this->eventList.size() > 0 && this->eventList[0].eventTime <= scTime)
[10066]156        { /* Execute the next event on the list */
[10065]157          this->execute(this->eventList[0]);
158          this->eventList.erase(this->eventList.begin());
159          this->eventno -= 1;
160        }
[10048]161
[10065]162        /* Update the local timers in this object */
[10066]163        scTime += dt; eventTime += dt;
[10065]164
165        /* If we've arrived at the target, stop processing */
[10066]166        if( eventTime > currentEvent.duration && this->processing == true)
[10065]167        { this->processing = false;
168
[10066]169          /* If we reached the last event, also reenable the normal movement
170           * and make the model visible again
171           */
[10065]172          if( this->eventno == 0 )
173          {
174            this->entity_->mouseLook();
175            this->entity_->setVisible(true);
176          }
[10048]177        }
178
[10065]179        /* Get a variable that specifies how far along the trajectory
180         * we are
181         */
[10066]182        float dl = eventTime / currentEvent.duration; 
[10048]183
[10066]184        /* Depending  */
[10065]185        /* Do some moving */
186        if( this->processing )
187        { 
[10066]188          if( this->currentEvent.fctName == "mal" )
189          {
190            /* Set the position to the correct place in the trajectory */
191            this->entity_->setPosition( (1-dl)*startpos + dl * this->currentEvent.v1);
[10047]192
[10066]193            /* Look at the specified position */
194            this->entity_->lookAt(this->currentEvent.v2);
[10047]195
[10066]196            /* Update look at position */
197            //this->lookAtPosition = this->currentEvent.v2;
198          }
199          else if( this->currentEvent.fctName == "chl" )
200          {
201            /* Sweep the look from v1 to v2 */
202            this->entity_->lookAt( (1-dl)*this->currentEvent.v1 + 
203              dl * this->currentEvent.v2 );
204          }
205
206
[10065]207          /* Force mouse look */
208          if( this->entity_->isInMouseLook() == false )
209            this->entity_->mouseLook();
210        }
211    }
[10048]212
[10065]213    void ScriptController::eventScheduler(std::string instruction, 
214      float x1, float y1, float z1, 
215      float x2, float y2, float z2, 
216      float duration, float executionTime)
[10048]217    {
[10065]218      /* put data (from LUA) into time-sorted eventList*/ 
219      /* Nimmt den befehl und die argumente aus luascript und ertellt einen
220       * struct pro event, diese structs werden sortiert nach eventTime
221       */
222      struct event tmp;
[10057]223
[10065]224      /* Fill the structure with all the provided information */
225      tmp.fctName = instruction;
[10066]226      //tmp.x1 = x1; tmp.y1 = y1; tmp.z1 = z1;
227      //tmp.x2 = x2; tmp.y2 = y2; tmp.z2 = z2;
228      tmp.v1 = Vector3(x1,y1,z1);
229      tmp.v2 = Vector3(x2,y2,z2);
[10065]230      tmp.duration = duration;
231      tmp.eventTime = executionTime;
[10059]232
[10077]233      orxout(verbose) << tmp.fctName << endl;
[10047]234
[10065]235      /* Add the created event to the event list */
236      if(eventList.size()==0)
237      { /* The list is still empty, just add it */
[10077]238        orxout(verbose) << "eventList empty (01)" << endl;
[10065]239        eventList.insert(eventList.begin(), tmp);
240        this->eventno += 1;
241        return; /* Nothing more to do, the event was added */
242      }
[10059]243
[10065]244      /* Event was not added yet since the list was not empty. Walk through
245       * the list of events and add it so that the events are correctly in
246       * order.
247       */
248      for (std::vector<event>::iterator it=eventList.begin(); it<eventList.end(); it++)
249      { if(tmp.eventTime < it->eventTime)
250        { eventList.insert(it,tmp);
251          this->eventno += 1;
[10076]252          //orxout()<<"new event added"<<endl;
[10065]253          return;
[10059]254        }
[10065]255      }
[10034]256
[10065]257      /* If the event was still not added here, it belongs in the end of the list */
258      eventList.insert(eventList.end(), tmp);
259      this->eventno += 1;
[10059]260
[10048]261    }
[10014]262}
Note: See TracBrowser for help on using the repository browser.