Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/plehmannFS16/src/orxonox/controllers/NewScriptController.cc @ 11183

Last change on this file since 11183 was 11183, checked in by plehmann, 9 years ago

moved the task classes to the scriptTasks folder. modified the NewScriptController to execute tasks when their time has come modified the tasks so that they tell the controller when they are done

File size: 7.1 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 *      Paul Lehmann
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29 /*
30  * Currently available lua commands:
31  *
32  * IMPORTANT: ALL COMMANDS DO REQUIRE 7 PARAMETERS TO BE PROVIDED. FILL UP WITH ZEROES IN UNIMPORTANT PLACES.
33  *
34  * Command             | Abbreviation | Parameter 1          | '' 2     | '' 3    | '' 4                 | '' 5     | '' 6     | '' 7
35  *
36  * "Move And Look"     | mal          | GoTo X Coordinate    |  '' Y ''  | '' Z '' | LookAt X Coordinate  |  '' Y '' |  '' Y '' | Duration
37  * "Rotate And Look"   | ral          | GoTo X Coordinate    |  '' Y ''  | '' Z '' | Axis (1=x, 2=y, 3=z) |     -    |     -    | Duration
38  * "Spiral"            | spi          | GoTo X Coordinate    |  '' Y ''  | '' Z '' |          -           |     -    |     -    | Duration
39  * "Transition Look"   | chl          | From X Coordinate    |  '' Y ''  | '' Z '' | To X Coordinate      |  '' Y '' |  '' Y '' | Duration
40  * "rotate round X crd"| rotX         | anchor coordinate    | angle(rad)|    -    |                      |          |          | Duration
41  * "Idle (Do nothing)" | idle         | Duration
42  */
43
44#include "NewScriptController.h"
45
46#include "scriptTasks/DebugTask.h"
47#include "scriptTasks/stringOutTask.h"
48#include "scriptTasks/Task.h"
49#include "infos/PlayerInfo.h"
50#include "core/CoreIncludes.h"
51#include "worldentities/ControllableEntity.h"
52#include "core/LuaState.h"
53#include "util/Math.h"
54
55namespace orxonox
56{
57    RegisterClass(NewScriptController);
58
59    NewScriptController::NewScriptController(Context* context) : ArtificialController(context)
60    {
61        RegisterObject(NewScriptController);
62
63
64        // Set default values for all variables
65        // - pointers to zero
66        this->player_ = nullptr;
67        this->entity_ = nullptr;
68
69        // - times
70        this->scTime_ = 0.0f;
71
72        this->context_ = context;
73
74       
75    }
76
77
78
79    void NewScriptController::takeControl(int ctrlid)
80    {
81        // Output some debugging information
82        orxout(verbose) << "NewScriptController: Taking control" << endl;
83        orxout(verbose) << "This-pointer: " << this << endl; 
84
85
86        // Store the entity pointer in a private variable
87        this->entity_ = this->player_->getControllableEntity();
88        assert(this->entity_);
89
90        this->ctrlid_ = ctrlid;
91        if (this->ctrlid_ == 0)
92        {
93          this->ctrlid_ = 1;
94        }
95         
96        // Add the controller here to this entity. Apparently this still leaves
97        // any preexisting human controllers in place.
98         
99        this->entity_->setDestroyWhenPlayerLeft(false);
100        this->player_->stopTemporaryControl();
101        this->entity_->setController(this);
102        this->setControllableEntity(this->entity_);
103        this->entity_->mouseLook();
104        this->entity_->setVisible(false);
105       
106        // TODO take the human Controllers control  dont forget to give it back in the destructor
107    }
108
109
110    void NewScriptController::tick(float dt)
111    {
112        // Call the tick function of the classes we derive from
113        SUPER(NewScriptController, tick, dt);
114
115
116        // If this controller has no entity entry, do nothing
117        if( !(this->entity_) ) return;
118
119
120/*
121        if(taskQueue_.front()->getStartTime() <= scTime_)
122        {
123          activeTasks_.push_back(taskQueue_.front());
124          taskQueue_.pop();
125        }
126
127        for(Task* task : activeTasks_)
128        {
129          task->tick(dt);
130        }*/
131
132        if(this->taskList_.size() != 0)
133        {
134
135          orxout() << this->scTime_ << endl;
136
137          if(this->taskList_.front()->getStartTime() < this->scTime_)
138          {
139             activeTasks_.push_back(this->taskList_.front());
140             this->taskList_.pop_front();
141          }
142        }
143        else
144        {
145          //orxout() << "no tasks in taskList_" << endl;
146        }
147       
148        for (std::vector<Task*>::iterator it = activeTasks_.begin(); it != activeTasks_.end(); it++)
149        {
150          if( !((*it)->update(dt)) )
151          {
152            delete (*it); // delete the task that was created with new
153            activeTasks_.erase(it);
154            it--; // set back the iterator so we continue with the next element and not with the one after that
155          }
156        }
157
158
159        scTime_ += dt;
160    }
161
162
163    void NewScriptController::createAndAddTask(Task newTask)
164    {
165      //taskQueue_->push(newTask);
166    }
167
168    void NewScriptController::debugOut(float startTime)
169    {
170      DebugTask* task = new DebugTask(context_);
171      task->initialize(startTime);
172
173      if(taskList_.empty())
174      {
175        taskList_.push_front(task);
176      }
177
178      else
179      {
180        for (std::list<Task*>::iterator it = taskList_.begin(); it != taskList_.end(); it++) // insert sorted by starttime
181        {
182          orxout() << taskList_.empty() << endl;
183
184          if(task->getStartTime() < (*it)->getStartTime() )
185          {
186            taskList_.insert(it, task);
187          }
188        }
189      }
190    }
191
192    void NewScriptController::stringOut(float startTime, std::string output)
193    {
194      stringOutTask* task = new stringOutTask(context_);
195      task->initialize(startTime, output);
196
197      if(taskList_.empty())
198      {
199        taskList_.push_front(task);
200      }
201
202      else
203      {
204        for (std::list<Task*>::iterator it = taskList_.begin(); it != taskList_.end(); it++) // insert sorted by starttime
205        {
206          orxout() << taskList_.empty() << endl;
207
208          if(task->getStartTime() < (*it)->getStartTime() )
209          {
210            taskList_.insert(it, task);
211          }
212        }
213      }
214    }
215
216    NewScriptController* NewScriptController::getNewScriptController() 
217    {
218      /* Output a message that confirms this function was called */
219      orxout() << "Great success!" << std::endl;
220
221      /* Debugging: print all the scriptcontroller object pointers */
222      for(NewScriptController* controller : ObjectList<NewScriptController>())
223      { orxout() << "Have object in list: " << controller << endl; }
224
225      /* Find the first one with a nonzero ID */
226      for(NewScriptController* controller : ObjectList<NewScriptController>())
227      { 
228        // TODO: do some selection here. Currently just returns the first one
229        if( controller->getID() > 0 )
230        { orxout() << "Controller to return: " << controller << endl;
231          return controller;
232        }
233     
234      }
235      return nullptr;
236    }
237
238}
Note: See TracBrowser for help on using the repository browser.