Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/TowerDefense_HS18/src/modules/towerdefense/TowerDefenseController.cc @ 12147

Last change on this file since 12147 was 12137, checked in by matanner, 6 years ago

Quality of life changes

File size: 5.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 *      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) {
83            speed = engine->getMaxSpeedFront(); //Geschwindigkeit wird ausgelesen
84            orxout() << "shiftshan" << endl;
85        } else {
86            speed = 100;
87            orxout() << "shiftshannonen" << endl;
88        }
89
90        //Zurueckgelegte Gesamtdistanz aktualisieren.
91        totalDistance += speed * dt;
92
93        float currentDistance = 0;
94        int waypointIdx = 0;
95        bool atEnd = true;
96        while (waypointIdx + 1 < this->waypoints_.size()) {
97            Vector3 prevWaypoint = this->waypoints_[waypointIdx]->getWorldPosition();
98            Vector3 nextWaypoint = this->waypoints_[waypointIdx + 1]->getWorldPosition();
99
100            float waypointDistance = prevWaypoint.distance(nextWaypoint);
101            if (currentDistance + waypointDistance < totalDistance) {
102                currentDistance += waypointDistance;
103                waypointIdx++;
104            } else {
105                atEnd = false;
106                break;
107            }
108        }
109
110        //Nun sollten wir wissen, zwischen welchen Waypoints sich unser Raumschiff befindet, uns welche Restdistanz wir haben. Wir koennen die Position unseres Raumschiffes nun setzten.
111        //Der Offset ist glaube ich fuer alle Tiles und Objekte noetig, damits am richtigen Ort ist.
112
113        //Berechnung des Richtungsvektors
114        Vector3 newPosition;
115        Vector3 newDirection;
116        if (atEnd) {
117            newPosition = this->waypoints_.back()->getWorldPosition();
118        } else {
119            Vector3 prevWaypoint = this->waypoints_[waypointIdx]->getWorldPosition();
120            Vector3 nextWaypoint = this->waypoints_[waypointIdx + 1]->getWorldPosition();
121
122            Vector3 direction = (nextWaypoint -  prevWaypoint).normalisedCopy();
123            newPosition = prevWaypoint + (totalDistance - currentDistance) * direction;
124            newDirection = direction;
125        }
126
127        this->getControllableEntity()->setPosition(offset_ + newPosition);
128        this->getControllableEntity()->lookAt(this->getControllableEntity()->getPosition() + newDirection);
129
130    }
131
132}
Note: See TracBrowser for help on using the repository browser.