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 | * Noe Pedrazzini |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "ModularSpaceShip.h" |
---|
30 | |
---|
31 | #include <BulletDynamics/Dynamics/btRigidBody.h> |
---|
32 | |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/config/ConfigValueIncludes.h" |
---|
35 | #include "core/Template.h" |
---|
36 | #include "core/XMLPort.h" |
---|
37 | #include "util/Math.h" |
---|
38 | #include "gametypes/Gametype.h" |
---|
39 | |
---|
40 | #include "items/ShipPart.h" |
---|
41 | #include "worldentities/StaticEntity.h" |
---|
42 | #include <BulletCollision/CollisionShapes/btCollisionShape.h> |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
48 | RegisterClass(ModularSpaceShip); |
---|
49 | |
---|
50 | ModularSpaceShip::ModularSpaceShip(Context* context) : SpaceShip(context) |
---|
51 | { |
---|
52 | RegisterObject(ModularSpaceShip); |
---|
53 | |
---|
54 | this->registerVariables(); |
---|
55 | |
---|
56 | } |
---|
57 | |
---|
58 | ModularSpaceShip::~ModularSpaceShip() |
---|
59 | { |
---|
60 | if (this->isInitialized()) |
---|
61 | { |
---|
62 | |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | void ModularSpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
67 | { |
---|
68 | SUPER(ModularSpaceShip, XMLPort, xmlelement, mode); |
---|
69 | XMLPortObject(ModularSpaceShip, ShipPart, "parts", addShipPart, getShipPart, xmlelement, mode); |
---|
70 | } |
---|
71 | |
---|
72 | void ModularSpaceShip::registerVariables() |
---|
73 | { |
---|
74 | return; |
---|
75 | } |
---|
76 | |
---|
77 | void ModularSpaceShip::updatePartAssignment() |
---|
78 | { |
---|
79 | // iterate through all attached objects |
---|
80 | for (unsigned int i=0; i < 100; i++) |
---|
81 | { |
---|
82 | if (this->getAttachedObject(i) == NULL) |
---|
83 | break; |
---|
84 | // iterate through all attached parts |
---|
85 | for(unsigned int j = 0; j < this->partList_.size(); j++) |
---|
86 | { |
---|
87 | // if the name of the part matches the name of the object, add the object to that parts entitylist (unless it was already done). |
---|
88 | if((this->partList_[j]->getName() == this->getAttachedObject(i)->getName()) && !this->partList_[j]->hasEntity(orxonox_cast<StaticEntity*>(this->getAttachedObject(i)))) |
---|
89 | { |
---|
90 | this->partList_[j]->addEntity(orxonox_cast<StaticEntity*>(this->getAttachedObject(i))); |
---|
91 | orxout() << "A matching part-entity-pair with name " << this->partList_[j]->getName() << " was found!" << endl; |
---|
92 | this->partList_[j]->printEntities(); // FIXME: (noep) remove debug |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | void ModularSpaceShip::attach(WorldEntity* object) |
---|
99 | { |
---|
100 | SpaceShip::attach(object); |
---|
101 | this->updatePartAssignment(); |
---|
102 | } |
---|
103 | |
---|
104 | void ModularSpaceShip::addPartEntityAssignment(StaticEntity* entity, ShipPart* part) |
---|
105 | { |
---|
106 | if (!entity || !part) |
---|
107 | return; |
---|
108 | |
---|
109 | if (this->partMap_.find(entity) != this->partMap_.end()) |
---|
110 | { |
---|
111 | orxout(internal_warning) << "Assigning an Entity to multiple parts is not yet supported." << endl; |
---|
112 | return; |
---|
113 | } |
---|
114 | |
---|
115 | this->partMap_[entity] = part; |
---|
116 | orxout() << "New entity-part assignment created!" << endl; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | @brief |
---|
121 | Get the ShipPart an attached entity belongs to. |
---|
122 | @param entity |
---|
123 | The entity to be searched. |
---|
124 | @return |
---|
125 | Returns a pointer to the ShipPart the entity belongs to. |
---|
126 | */ |
---|
127 | ShipPart* ModularSpaceShip::getPartOfEntity(StaticEntity* entity) const |
---|
128 | { |
---|
129 | for (std::map<StaticEntity*, ShipPart*>::const_iterator it = this->partMap_.begin(); it != this->partMap_.end(); ++it) |
---|
130 | { |
---|
131 | if (it->first == entity) |
---|
132 | return it->second; |
---|
133 | } |
---|
134 | return NULL; |
---|
135 | } |
---|
136 | |
---|
137 | //FIXME: (noep) finish |
---|
138 | // void ModularSpaceShip::attach |
---|
139 | |
---|
140 | void ModularSpaceShip::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs) |
---|
141 | { |
---|
142 | orxout() << "Mdamage(): Collision detected on " << this->getRadarName() << ", btCS*: " << cs << endl; |
---|
143 | orxout() << "UserPtr of said collisionShape: " << cs->getUserPointer() << endl; |
---|
144 | |
---|
145 | // List all attached Objects |
---|
146 | orxout() << " " << this->getName() << " has the following Objects attached:" << endl; |
---|
147 | for (int i=0; i<10; i++) |
---|
148 | { |
---|
149 | if (this->getAttachedObject(i)==NULL) |
---|
150 | break; |
---|
151 | orxout() << " " << i << ": " << this->getAttachedObject(i) << " (" << this->getAttachedObject(i)->getName() << ")"; |
---|
152 | orxout() << endl; |
---|
153 | } |
---|
154 | |
---|
155 | orxout() << " Attached ShipParts:" << endl; |
---|
156 | for(unsigned int i=0; i < this->partList_.size(); i++) |
---|
157 | { |
---|
158 | orxout() << " " << i << ": " << this->partList_[i] << " (" << this->partList_[i]->getName() << ")" << endl; |
---|
159 | } |
---|
160 | |
---|
161 | //int collisionShapeIndex = this->isMyCollisionShape(cs); |
---|
162 | //orxout() << collisionShapeIndex << endl; |
---|
163 | |
---|
164 | orxout() << "ShipPart of Entity " << cs->getUserPointer() << ": " << this->getPartOfEntity((StaticEntity*)(cs->getUserPointer())) << endl; |
---|
165 | |
---|
166 | if (this->getPartOfEntity((StaticEntity*)(cs->getUserPointer())) != NULL) |
---|
167 | this->getPartOfEntity((StaticEntity*)(cs->getUserPointer()))->handleHit(damage, healthdamage, shielddamage, originator); |
---|
168 | //else |
---|
169 | // SpaceShip::damage(damage, healthdamage, shielddamage, originator, cs); |
---|
170 | |
---|
171 | /* |
---|
172 | // Applies multiplier given by the DamageBoost Pickup. |
---|
173 | if (originator) |
---|
174 | damage *= originator->getDamageMultiplier(); |
---|
175 | |
---|
176 | if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator)) |
---|
177 | { |
---|
178 | if (shielddamage >= this->getShieldHealth()) |
---|
179 | { |
---|
180 | this->setShieldHealth(0); |
---|
181 | this->setHealth(this->health_ - (healthdamage + damage)); |
---|
182 | } |
---|
183 | else |
---|
184 | { |
---|
185 | this->setShieldHealth(this->shieldHealth_ - shielddamage); |
---|
186 | |
---|
187 | // remove remaining shieldAbsorpton-Part of damage from shield |
---|
188 | shielddamage = damage * this->shieldAbsorption_; |
---|
189 | shielddamage = std::min(this->getShieldHealth(),shielddamage); |
---|
190 | this->setShieldHealth(this->shieldHealth_ - shielddamage); |
---|
191 | |
---|
192 | // set remaining damage to health |
---|
193 | this->setHealth(this->health_ - (damage - shielddamage) - healthdamage); |
---|
194 | } |
---|
195 | |
---|
196 | this->lastHitOriginator_ = originator; |
---|
197 | }*/ |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | @brief |
---|
202 | Add a ShipPart to the SpaceShip. |
---|
203 | @param engine |
---|
204 | A pointer to the ShipPart to be added. |
---|
205 | */ |
---|
206 | void ModularSpaceShip::addShipPart(ShipPart* part) |
---|
207 | { |
---|
208 | OrxAssert(part != NULL, "The ShipPart cannot be NULL."); |
---|
209 | this->partList_.push_back(part); |
---|
210 | //part->addToSpaceShip(this); //FIXME: (noep) add |
---|
211 | this->updatePartAssignment(); |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | @brief |
---|
216 | Get the i-th ShipPart of the SpaceShip. |
---|
217 | @return |
---|
218 | Returns a pointer to the i-the ShipPart. NULL if there is no ShipPart with that index. |
---|
219 | */ |
---|
220 | ShipPart* ModularSpaceShip::getShipPart(unsigned int index) |
---|
221 | { |
---|
222 | if(this->partList_.size() >= index) |
---|
223 | return NULL; |
---|
224 | else |
---|
225 | return this->partList_[index]; |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | @brief |
---|
230 | Check whether the SpaceShip has a particular Engine. |
---|
231 | @param engine |
---|
232 | A pointer to the Engine to be checked. |
---|
233 | */ |
---|
234 | bool ModularSpaceShip::hasShipPart(ShipPart* part) const |
---|
235 | { |
---|
236 | for(unsigned int i = 0; i < this->partList_.size(); i++) |
---|
237 | { |
---|
238 | if(this->partList_[i] == part) |
---|
239 | return true; |
---|
240 | } |
---|
241 | return false; |
---|
242 | } |
---|
243 | |
---|
244 | } |
---|