Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/towerdefense/TowerDefenseController.cc @ 12190

Last change on this file since 12190 was 12190, checked in by merholzl, 5 years ago

TD merge

File size: 5.3 KB
RevLine 
[12126]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 *      Michael Baumgartner
24 *   Co-authors:
25 *      Fabian 'x3n' Landau
26 *
27 */
28
29#include "TowerDefenseController.h"
30
31#include "core/CoreIncludes.h"
32#include "items/Engine.h"
33#include "worldentities/ControllableEntity.h"
34#include "worldentities/pawns/SpaceShip.h"
35#include "TowerDefense.h"
36#include "TowerDefenseTower.h"
37#include "TowerDefenseCenterpoint.h"
38#include "worldentities/SpawnPoint.h"
39#include "graphics/Model.h"
40#include "infos/PlayerInfo.h"
41#include "chat/ChatManager.h"
42#include "Highscore.h"
43#include <math.h>
44
45namespace orxonox
46{
47    RegisterClass(TowerDefenseController);
48
49   
50    //Offset fuer die Position der Entities auf der Map
51    Vector3 offset_ = Vector3(0,0,10);
52
53    TowerDefenseController::TowerDefenseController(Context* context) : ArtificialController(context)
54    {
55        RegisterObject(TowerDefenseController);
56    }
57
58    TowerDefenseController::~TowerDefenseController() //Destructor, ehrlich gesagt keine Ahnung was er genau loescht (nehme an den Controller, falls man am letzten Waypoint angekommen ist.)
59    {
60    }
61
62    void TowerDefenseController::tick(float dt)
63    {
64        if (!this->isActive())
65            return;
66
67        SpaceShip* ship = dynamic_cast<SpaceShip*>(this->getControllableEntity());
68       
69        if (this->waypoints_.size() == 0 || !ship)
70            return;
71
72        //Bewegung entlang der Verbindungslinie zweier Waypoints, bis die Distanz zwischen Ihnen zurueckgelegt
73        //wurde, dann eine Drehung in Richtung des Verbindungsvektors zum naechsten Vektor, und Bewegung in diese Richtung. Iterationsfehler werden behoben, indem man zuweit zurueck-
74        //gelegte Wege direkt in die neue Richtung uebernimmt. Dafuer wird in der while-Schleife zuerst berechnet, in welchem Abschnitt man sich befindet, und anschliessend, welche Distanz
75        //man in diesem Abschnitt zurueckgelegt hat. Dann wird die Position des Schiffes einfach auf den entsprechenden Ort gelegt, und geschaut, dass es in die richtige Richtung zeigt.
76
77        //Die Position  kann iterativ bestimmt werden. this->waypoints ist der Array bestehend aus den Wegpunkten, sprich allen Ecken wobei an der Position 0 der SpawnPoint steht.
78
79        // Geschwindigkeit auslesen
80        float speed;
81        Engine* engine = ship->getEngine(0);
82        if(engine != nullptr) {
[12153]83            speed = engine->getMaxSpeedFront(); //Geschwindigkeit sollte ausgelesen werden, wird momentan aber noch NICHT -> ein Fehler mit den Engines
84
[12126]85        } else {
[12153]86            speed = 200; //Momentan wird hiermit die Geschwindigkeit ALLER Schiffe gesetzt!
[12126]87        }
88
89        //Zurueckgelegte Gesamtdistanz aktualisieren.
90        totalDistance += speed * dt;
91
92        float currentDistance = 0;
93        int waypointIdx = 0;
94        bool atEnd = true;
95        while (waypointIdx + 1 < this->waypoints_.size()) {
96            Vector3 prevWaypoint = this->waypoints_[waypointIdx]->getWorldPosition();
97            Vector3 nextWaypoint = this->waypoints_[waypointIdx + 1]->getWorldPosition();
98
99            float waypointDistance = prevWaypoint.distance(nextWaypoint);
100            if (currentDistance + waypointDistance < totalDistance) {
101                currentDistance += waypointDistance;
102                waypointIdx++;
103            } else {
104                atEnd = false;
105                break;
106            }
107        }
108
109        //Nun sollten wir wissen, zwischen welchen Waypoints sich unser Raumschiff befindet, uns welche Restdistanz wir haben. Wir koennen die Position unseres Raumschiffes nun setzten.
110        //Der Offset ist glaube ich fuer alle Tiles und Objekte noetig, damits am richtigen Ort ist.
111
112        //Berechnung des Richtungsvektors
113        Vector3 newPosition;
114        Vector3 newDirection;
115        if (atEnd) {
116            newPosition = this->waypoints_.back()->getWorldPosition();
117        } else {
118            Vector3 prevWaypoint = this->waypoints_[waypointIdx]->getWorldPosition();
119            Vector3 nextWaypoint = this->waypoints_[waypointIdx + 1]->getWorldPosition();
120
121            Vector3 direction = (nextWaypoint -  prevWaypoint).normalisedCopy();
122            newPosition = prevWaypoint + (totalDistance - currentDistance) * direction;
123            newDirection = direction;
124        }
125
126        this->getControllableEntity()->setPosition(offset_ + newPosition);
127        this->getControllableEntity()->lookAt(this->getControllableEntity()->getPosition() + newDirection);
[12153]128        this->getControllableEntity()->setVelocity(speed * newDirection);
[12126]129    }
130
131}
Note: See TracBrowser for help on using the repository browser.