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 "ScriptableControllerDrone.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
33 | |
---|
34 | namespace orxonox |
---|
35 | { |
---|
36 | //TODO: Put your code in here: |
---|
37 | // Create the factory for the drone. |
---|
38 | |
---|
39 | /** |
---|
40 | @brief |
---|
41 | Constructor. Registers the object and initializes some default values. |
---|
42 | @param creator |
---|
43 | The creator of this object. |
---|
44 | */ |
---|
45 | |
---|
46 | RegisterClass(ScriptableControllerDrone); |
---|
47 | |
---|
48 | ScriptableControllerDrone::ScriptableControllerDrone(Context* context) : Pawn(context) |
---|
49 | { |
---|
50 | //TODO: Put your code in here: |
---|
51 | // Register the drone class to the core. |
---|
52 | |
---|
53 | RegisterObject(ScriptableControllerDrone); |
---|
54 | |
---|
55 | //this->myController_ = NULL; |
---|
56 | |
---|
57 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
58 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
59 | this->primaryThrust_ = 100; |
---|
60 | this->auxiliaryThrust_ = 100; |
---|
61 | this->rotationThrust_ = 10; |
---|
62 | this->mass_ = 100; |
---|
63 | this->linearDamping_ = 0.5; |
---|
64 | this->angularDamping_ = 0.3; |
---|
65 | |
---|
66 | this->setRadarVisibility(false); |
---|
67 | |
---|
68 | this->setCollisionType(WorldEntity::CollisionType::Dynamic); |
---|
69 | |
---|
70 | //this->setCollisionType(CollisionType::Dynamic); |
---|
71 | |
---|
72 | //this->myController_ = new ScriptableControllerDroneController(this); // Creates a new controller and passes our this pointer to it as creator. |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | @brief |
---|
77 | Destructor. Destroys controller, if present. |
---|
78 | */ |
---|
79 | ScriptableControllerDrone::~ScriptableControllerDrone() |
---|
80 | { |
---|
81 | // Deletes the controller if the object was initialized and the pointer to the controller is not NULL. |
---|
82 | if( this->isInitialized()); //)&& this->myController_ != NULL ) |
---|
83 | //delete this->myController_; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | @brief |
---|
88 | Method for creating a ScriptableControllerDrone through XML. |
---|
89 | */ |
---|
90 | void ScriptableControllerDrone::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
91 | { |
---|
92 | // This calls the XMLPort function of the parent class |
---|
93 | SUPER(ScriptableControllerDrone, XMLPort, xmlelement, mode); |
---|
94 | |
---|
95 | XMLPortParam(ScriptableControllerDrone, "primaryThrust", setPrimaryThrust, getPrimaryThrust, xmlelement, mode); |
---|
96 | //TODO: Put your code in here: |
---|
97 | // Make sure you add the variables auxiliaryThrust_ and rotationThrust_ to XMLPort. |
---|
98 | // Variables can be added by the following command |
---|
99 | // XMLPortParam(Classname, "xml-attribute-name (i.e. variablename)", setFunctionName, getFunctionName, xmlelement, mode); |
---|
100 | // Also make sure that you also create the get- and set-functions in ScriptableControllerDrone.h. As you can see, the get- and set-functions for the variable primaryThrust_ has already been specified there, so you can get your inspiration from there. |
---|
101 | XMLPortParam(ScriptableControllerDrone, "auxiliaryThrust", setAuxiliaryThrust, getAuxiliaryThrust, xmlelement, mode); |
---|
102 | XMLPortParam(ScriptableControllerDrone, "rotationThrust", setRotationThrust, getRotationThrust, xmlelement, mode); |
---|
103 | XMLPortParam(ScriptableControllerDrone, "mass", setMass, getMass, xmlelement, mode); |
---|
104 | XMLPortParam(ScriptableControllerDrone, "linearDamping", setLinearDamping, getLinearDamping, xmlelement, mode); |
---|
105 | XMLPortParam(ScriptableControllerDrone, "angularDamping", setAngularDamping, getAngularDamping, xmlelement, mode); |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | @brief |
---|
115 | Defines which actions the ScriptableControllerDrone has to take in each tick. |
---|
116 | @param dt |
---|
117 | The length of the tick. |
---|
118 | */ |
---|
119 | void ScriptableControllerDrone::tick(float dt) |
---|
120 | { |
---|
121 | SUPER(ScriptableControllerDrone, tick, dt); |
---|
122 | |
---|
123 | |
---|
124 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxiliaryThrust_); |
---|
125 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxiliaryThrust_); |
---|
126 | |
---|
127 | |
---|
128 | if (this->localLinearAcceleration_.z() > 0) |
---|
129 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxiliaryThrust_); |
---|
130 | else |
---|
131 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_); |
---|
132 | |
---|
133 | this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_); |
---|
134 | this->localLinearAcceleration_.setValue(0, 0, 0); |
---|
135 | |
---|
136 | this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; |
---|
137 | this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); |
---|
138 | this->localAngularAcceleration_.setValue(0, 0, 0); |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | @brief |
---|
143 | Moves the ScriptableControllerDrone in the negative z-direction (Front/Back) by an amount specified by the first component of the input 2-dim vector. |
---|
144 | @param value |
---|
145 | The vector determining the amount of the movement. |
---|
146 | */ |
---|
147 | void ScriptableControllerDrone::moveFrontBack(const Vector2& value) |
---|
148 | { |
---|
149 | this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | @brief |
---|
154 | Moves the ScriptableControllerDrone in the x-direction (Right/Left) by an amount specified by the first component of the input 2-dim vector. |
---|
155 | @param value |
---|
156 | The vector determining the amount of the movement. |
---|
157 | */ |
---|
158 | void ScriptableControllerDrone::moveRightLeft(const Vector2& value) |
---|
159 | { |
---|
160 | this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | @brief |
---|
165 | Moves the ScriptableControllerDrone in the y-direction (Up/Down) by an amount specified by the first component of the input 2-dim vector. |
---|
166 | @param value |
---|
167 | The vector determining the amount of the movement. |
---|
168 | */ |
---|
169 | void ScriptableControllerDrone::moveUpDown(const Vector2& value) |
---|
170 | { |
---|
171 | this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x); |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | @brief |
---|
176 | Rotates the ScriptableControllerDrone around the y-axis by the amount specified by the first component of the input 2-dim vector. |
---|
177 | @param value |
---|
178 | The vector determining the amount of the angular movement. |
---|
179 | */ |
---|
180 | void ScriptableControllerDrone::rotateYaw(const Vector2& value) |
---|
181 | { |
---|
182 | this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() - value.x); |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | @brief |
---|
187 | Rotates the ScriptableControllerDrone around the x-axis by the amount specified by the first component of the input 2-dim vector. |
---|
188 | @param value |
---|
189 | The vector determining the amount of the angular movement. |
---|
190 | */ |
---|
191 | void ScriptableControllerDrone::rotatePitch(const Vector2& value) |
---|
192 | { |
---|
193 | this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x); |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | @brief |
---|
198 | Rotates the ScriptableControllerDrone around the z-axis by the amount specified by the first component of the input 2-dim vector. |
---|
199 | @param value |
---|
200 | The vector determining the amount of the angular movement. |
---|
201 | */ |
---|
202 | void ScriptableControllerDrone::rotateRoll(const Vector2& value) |
---|
203 | { |
---|
204 | this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x); |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | } |
---|