Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc @ 10052

Last change on this file since 10052 was 10048, checked in by samuezu, 11 years ago

created struct event, an eventlist and the functions eventscheduler and execute, modified tick function

File size: 5.9 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    float scTime=0;  /*initialise time, to coordinate eventTime*/
39
40
41
42    std::vector<event> eventList;
43
44   
45
46
47
48    RegisterClass(ScriptController);
49
50    //ScriptController::ScriptController(Context* context, ControllableEntity* CE) : ArtificialController(context)
51    ScriptController::ScriptController(Context* context) : ArtificialController(context)
52    {
53        RegisterObject(ScriptController);
54        //set_controlled(CE);
55        this->ctrlid_ = 0;
56    }
57
58    void ScriptController::takeControl(int ctrlid)
59    {
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_);
70    }
71
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
80    void ScriptController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
81    {
82        //XMLPortParam(ScriptController, BaseObject, "lsrc", set_luasrc, xmlelement, mode);
83
84    }
85
86    const Vector3& ScriptController::getPosition()
87    {
88        return this->entity_->getPosition();
89    }
90
91    ScriptController* ScriptController::getScriptController()
92    {
93      /* Output a message that confirms this function was called */
94      orxout() << "Great success!" << std::endl;
95
96      /* Debugging: print all the scriptcontroller object pointers */
97      for(ObjectList<ScriptController>::iterator it = 
98        ObjectList<ScriptController>::begin(); 
99        it != ObjectList<ScriptController>::end(); ++it)
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)
106      { 
107        // TODO: do some selection here. Currently just returns the first one
108        if( (*it)->getID() > 0 )
109          return *it; 
110     
111      }
112      return NULL;
113    }
114
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
124    void ScriptController::tick(float dt)
125    {
126        /* If this controller has no entity entry, do nothing */
127        if( !(this->entity_) )
128          return;
129
130        //orxout() << "Rotating!" << endl;
131
132        //this->entity_->rotateYaw(-1.0f * 100.0f * dt);
133        //this->entity_->rotatePitch(0.8f * 100.0f);
134
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
143        SUPER(ScriptController, tick, dt);
144
145        scTime=scTime+dt;
146    }
147
148
149
150
151    void ScriptController::moveToPosition_beta(float x, float y, float z )
152    {
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;
157       
158       
159        this->entity_->lookAt(target);
160        this->entity_->moveFrontBack(-1000*target.length());     
161
162 
163        /* This works fine */
164        orxout()<<x<<"  "<<y<<"  "<<z<<endl;
165    }
166
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;
177
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();
183
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();
190
191                eventList.insert(it,tmp);
192
193            }
194
195        }
196       
197    }
198
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
208}
Note: See TracBrowser for help on using the repository browser.