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 | * Oli Scheuss |
---|
24 | * Co-authors: |
---|
25 | * Damian 'Mozork' Frick |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "ArrowController.h" |
---|
30 | #include "HumanController.h" |
---|
31 | #include "worldentities/WorldEntity.h" |
---|
32 | |
---|
33 | #include "worldentities/Arrow.h" |
---|
34 | #include "util/Math.h" |
---|
35 | |
---|
36 | namespace orxonox |
---|
37 | { |
---|
38 | |
---|
39 | RegisterClass(ArrowController); |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | /** |
---|
44 | @brief |
---|
45 | Constructor. |
---|
46 | */ |
---|
47 | ArrowController::ArrowController(Context* context) : Controller(context) |
---|
48 | { |
---|
49 | |
---|
50 | RegisterObject(ArrowController); |
---|
51 | |
---|
52 | this->currentGPSPoint_ = 0; |
---|
53 | this->accuracy_ = 1000.0f; |
---|
54 | |
---|
55 | arrow = nullptr; |
---|
56 | |
---|
57 | for(Arrow* a: ObjectList<Arrow>()) |
---|
58 | arrow = a; |
---|
59 | |
---|
60 | assert(arrow != nullptr); |
---|
61 | this->setControllableEntity(arrow); |
---|
62 | |
---|
63 | orxout() << "constructor aufgerufen" << endl; |
---|
64 | } |
---|
65 | |
---|
66 | //Set the distance you need to reach before the next waypoint will be selected |
---|
67 | void ArrowController::setAccuracy(float accuracy){ |
---|
68 | this->accuracy_ = accuracy; |
---|
69 | } |
---|
70 | |
---|
71 | float ArrowController::getAccuracy(){ |
---|
72 | return this->accuracy_; |
---|
73 | }; |
---|
74 | |
---|
75 | void ArrowController::addGPSPoint(WorldEntity* gpspoint) |
---|
76 | { |
---|
77 | this->gpspoints_.push_back(gpspoint); |
---|
78 | } |
---|
79 | |
---|
80 | WorldEntity* ArrowController::getGPSPoint(unsigned int index) const |
---|
81 | { |
---|
82 | if (index < this->gpspoints_.size()) |
---|
83 | return this->gpspoints_[index]; |
---|
84 | else |
---|
85 | return nullptr; |
---|
86 | } |
---|
87 | /** |
---|
88 | @brief |
---|
89 | Destructor. |
---|
90 | */ |
---|
91 | |
---|
92 | ArrowController::~ArrowController() |
---|
93 | { |
---|
94 | for (WorldEntity* gpspoint : this->gpspoints_) |
---|
95 | { |
---|
96 | if(gpspoint) |
---|
97 | gpspoint->destroy(); |
---|
98 | } |
---|
99 | |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | void ArrowController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
104 | { |
---|
105 | SUPER(ArrowController, XMLPort, xmlelement, mode); |
---|
106 | |
---|
107 | XMLPortObject(ArrowController, WorldEntity, "gpspoints", addGPSPoint, getGPSPoint, xmlelement, mode); |
---|
108 | XMLPortParam(ArrowController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode); |
---|
109 | } |
---|
110 | |
---|
111 | void ArrowController::tick(float dt) |
---|
112 | { |
---|
113 | if (!this->isActive()) |
---|
114 | return; |
---|
115 | |
---|
116 | if (this->gpspoints_.size() == 0 || !this->getControllableEntity()) |
---|
117 | return; |
---|
118 | //Set all waypoint to invisible at the beginning |
---|
119 | if (this->currentGPSPoint_ == 0){ |
---|
120 | for(unsigned int i = 0; i < this->gpspoints_.size(); i++ ) |
---|
121 | this->gpspoints_[i]->setVisible(false); |
---|
122 | } |
---|
123 | //Make the arrow inivisible as soon as you reached the last Waypoint, otherwise make the next waypoint visible |
---|
124 | if(currentGPSPoint_ >= gpspoints_.size()){ |
---|
125 | this->getControllableEntity()->setVisible(false); |
---|
126 | return; |
---|
127 | } |
---|
128 | else this->gpspoints_[this->currentGPSPoint_]->setVisible(true); |
---|
129 | |
---|
130 | //Set the next waypoint as target as soon as you reached the previous one |
---|
131 | if (this->gpspoints_[this->currentGPSPoint_]->getWorldPosition().squaredDistance(this->getControllableEntity()->getPosition()) <= this->accuracy_){ |
---|
132 | this->gpspoints_[this->currentGPSPoint_]->setVisible(false); |
---|
133 | this->currentGPSPoint_ = (this->currentGPSPoint_ + 1); |
---|
134 | return; |
---|
135 | } |
---|
136 | |
---|
137 | Vector3 target = gpspoints_[currentGPSPoint_]->getWorldPosition(); |
---|
138 | WorldEntity::TransformSpace trans = WorldEntity::TransformSpace::World; |
---|
139 | |
---|
140 | //Get the position and orientation of the Spaceship |
---|
141 | Vector3 spaceShipPosition = HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition(); |
---|
142 | Quaternion spaceShipOrientation = HumanController::getLocalControllerSingleton()->getControllableEntity()->getOrientation(); |
---|
143 | |
---|
144 | //Calculate the new arrow position |
---|
145 | Vector3 ss_y = spaceShipOrientation.yAxis(); |
---|
146 | spaceShipPosition.x += 20 * ss_y.x; |
---|
147 | spaceShipPosition.y += 20 * ss_y.y; |
---|
148 | spaceShipPosition.z += 20 * ss_y.z; |
---|
149 | |
---|
150 | //Update Arrow position and orientation |
---|
151 | this->getControllableEntity()->setPosition(spaceShipPosition); |
---|
152 | this->getControllableEntity()->lookAt(target, trans, Vector3(0,0,1)); |
---|
153 | |
---|
154 | |
---|
155 | } |
---|
156 | } |
---|