Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

eol-style native. no changes in code.

  • Property svn:eol-style set to native
File size: 8.4 KB
Line 
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"
30#include "infos/PlayerInfo.h"
31#include "core/CoreIncludes.h"
32#include "worldentities/ControllableEntity.h"
33#include "core/LuaState.h"
34#include <cmath>
35
36namespace orxonox
37{
38    RegisterClass(ScriptController);
39
40    ScriptController::ScriptController(Context* context) : ArtificialController(context)
41    {
42        RegisterObject(ScriptController);
43
44        /* By default, this controller has ID 0, which means it is not assigned
45         * to anything yet.
46         */
47        this->ctrlid_ = 0;
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);
60        //this->lookAtPosition = Vector3(0,0,0);
61
62        /* - Processing flag */
63        this->processing = false;
64
65        /* - Counters */
66        this->eventno = 0;
67
68    }
69
70    void ScriptController::takeControl(int ctrlid)
71    {
72        /* Output some debugging information */
73        orxout(verbose) << "ScriptController: Taking control" << endl;
74        orxout(verbose) << "This-pointer: " << this << endl;
75
76        /* Set the controller ID (the argument here should be nonzero) */
77        this->ctrlid_ = ctrlid;
78
79        /* Store the entity pointer in a private variable */
80        this->entity_ = this->player_->getControllableEntity();
81        assert(this->entity_);
82         
83        /* Add the controller here to this entity. Apparently this still leaves
84         * any preexisting human controllers in place.
85         */
86        this->entity_->setDestroyWhenPlayerLeft(false);
87        this->player_->pauseControl();
88        this->entity_->setController(this);
89        this->setControllableEntity(this->entity_);
90        this->entity_->mouseLook();
91        this->entity_->setVisible(false);
92    }
93
94    const Vector3& ScriptController::getPosition()
95    {
96      return this->entity_->getPosition();
97    }
98
99    ScriptController* ScriptController::getScriptController()
100    {
101      /* Output a message that confirms this function was called */
102      orxout(verbose) << "Great success!" << std::endl;
103
104      /* Debugging: print all the scriptcontroller object pointers */
105      for(ObjectList<ScriptController>::iterator it = 
106        ObjectList<ScriptController>::begin(); 
107        it != ObjectList<ScriptController>::end(); ++it)
108      { orxout(verbose) << "Have object in list: " << *it << endl; }
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)
114      { 
115        // TODO: do some selection here. Currently just returns the first one
116        if( (*it)->getID() > 0 )
117        { orxout(verbose) << "Controller to return: " << *it << endl;
118          return *it; 
119        }
120     
121      }
122      return NULL;
123    }
124
125    void ScriptController::execute(event ev)
126    {
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;
133
134        /* Event is starting, hence set the time to 0 */
135        this->eventTime = 0.0f;
136        this->processing = true;
137
138        /* Copy the event into the currentEvent holder */
139        this->currentEvent = ev;
140
141        /* Store starting position */
142        this->startpos = this->entity_->getPosition();
143    }
144
145
146    void ScriptController::tick(float dt)
147    {
148        /* Call the tick function of the classes we derive from */
149        SUPER(ScriptController, tick, dt);
150
151        /* If this controller has no entity entry, do nothing */
152        if( !(this->entity_) ) return;
153
154        /* See if time has come for the next event to be run */
155        if(this->eventList.size() > 0 && this->eventList[0].eventTime <= scTime)
156        { /* Execute the next event on the list */
157          this->execute(this->eventList[0]);
158          this->eventList.erase(this->eventList.begin());
159          this->eventno -= 1;
160        }
161
162        /* Update the local timers in this object */
163        scTime += dt; eventTime += dt;
164
165        /* If we've arrived at the target, stop processing */
166        if( eventTime > currentEvent.duration && this->processing == true)
167        { this->processing = false;
168
169          /* If we reached the last event, also reenable the normal movement
170           * and make the model visible again
171           */
172          if( this->eventno == 0 )
173          {
174            this->entity_->mouseLook();
175            this->entity_->setVisible(true);
176          }
177        }
178
179        /* Get a variable that specifies how far along the trajectory
180         * we are
181         */
182        float dl = eventTime / currentEvent.duration; 
183
184        /* Depending  */
185        /* Do some moving */
186        if( this->processing )
187        { 
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);
192
193            /* Look at the specified position */
194            this->entity_->lookAt(this->currentEvent.v2);
195
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
207          /* Force mouse look */
208          if( this->entity_->isInMouseLook() == false )
209            this->entity_->mouseLook();
210        }
211    }
212
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)
217    {
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;
223
224      /* Fill the structure with all the provided information */
225      tmp.fctName = instruction;
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);
230      tmp.duration = duration;
231      tmp.eventTime = executionTime;
232
233      orxout(verbose) << tmp.fctName << endl;
234
235      /* Add the created event to the event list */
236      if(eventList.size()==0)
237      { /* The list is still empty, just add it */
238        orxout(verbose) << "eventList empty (01)" << endl;
239        eventList.insert(eventList.begin(), tmp);
240        this->eventno += 1;
241        return; /* Nothing more to do, the event was added */
242      }
243
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;
252          //orxout()<<"new event added"<<endl;
253          return;
254        }
255      }
256
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;
260
261    }
262}
Note: See TracBrowser for help on using the repository browser.