Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10057 was 10057, checked in by mkronig, 11 years ago
File size: 6.0 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;  /*initialize 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
120            moveToPosition_beta(ev.xCoord,ev.yCoord,ev.zCoord);
121        }
122    }
123
124
125    void ScriptController::tick(float dt)
126    {
127        /* If this controller has no entity entry, do nothing */
128        if( !(this->entity_) )
129          return;
130
131        //orxout() << "Rotating!" << endl;
132
133        //this->entity_->rotateYaw(-1.0f * 100.0f * dt);
134        //this->entity_->rotatePitch(0.8f * 100.0f);
135
136        if(eventList.size() > 0 && eventList[0].eventTime<=scTime)
137        {
138            /*TO DO: execute the function: eventList[0].fctName*/
139
140            execute(eventList[0]);
141            eventList.erase(eventList.begin());
142        }
143
144        SUPER(ScriptController, tick, dt);
145
146        scTime=scTime+dt;
147    }
148
149
150
151
152    void ScriptController::moveToPosition_beta(float x, float y, float z )
153    {
154        //const Vector3 local = this->getPosition();
155        const Vector3 target = Vector3(100*x,100*y,100*z);
156        //Vector3 way = target-local;
157        orxout() << "Moving This-pointer: " << this << endl;
158       
159       
160        this->entity_->lookAt(target);
161        this->entity_->moveFrontBack(-1000*target.length());     
162
163 
164        /* This works fine */
165        orxout()<<x<<"  "<<y<<"  "<<z<<endl;
166    }
167
168    void ScriptController::eventScheduler(std::string instruction, float x, float y, float z, float executionTime)
169    {
170
171        /*put data (from LUA) into time-sorted eventList*/ 
172        /*nimmt den befehl und die argumente aus luascript und ertellt einen struct pro event, diese structs werden sortiert nach eventTime*/
173        struct event tmp;
174        tmp.fctName=instruction;
175        tmp.xCoord=x;
176        tmp.yCoord=y;
177        tmp.zCoord=z;
178        tmp.eventTime=executionTime;
179
180        for(unsigned int i=0;i<eventList.size();i++)
181        {
182            if(tmp.eventTime<eventList[i].eventTime)
183            {
184                std::vector<event>::iterator it = eventList.begin();
185
186                eventList.insert(it+(i+1),tmp);
187                break;
188            }
189            if(i==eventList.size()-1)
190            {
191                std::vector<event>::iterator it = eventList.end();
192
193                eventList.insert(it,tmp);
194
195            }
196
197        }
198       
199       
200    }
201
202
203
204    /* TODO:    struct event erweitern um mehr funktionen benutzen zu koennen
205
206                mehr funktionen definieren (und dann in  execute if(...))
207                NB: viele noetige funktionen sind schon in artificial- bzw formationcontroller vorhanden */       
208
209
210
211}
Note: See TracBrowser for help on using the repository browser.