Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/modularships/src/orxonox/worldentities/pawns/ModularSpaceShip.cc @ 10067

Last change on this file since 10067 was 10067, checked in by noep, 10 years ago

Added explosions. (Because who doesn't love explosions.) Added printing health-status of shipparts to chat. Removed more debug-output.

File size: 11.5 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 *      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#include "core/command/ConsoleCommand.h"
40
41#include "items/ShipPart.h"
42#include "items/Engine.h"
43#include "worldentities/StaticEntity.h"
44#include "collisionshapes/WorldEntityCollisionShape.h"
45#include <BulletCollision/CollisionShapes/btCollisionShape.h>
46#include <BulletCollision/CollisionShapes/btCompoundShape.h>
47
48
49
50namespace orxonox
51{
52    SetConsoleCommand("ModularSpaceShip", "killshippart", &ModularSpaceShip::killShipPart);
53
54    RegisterClass(ModularSpaceShip);
55
56    std::map<StaticEntity*, ShipPart*>* ModularSpaceShip::partMap_s = 0;
57
58    ModularSpaceShip::ModularSpaceShip(Context* context) : SpaceShip(context)
59    {
60        RegisterObject(ModularSpaceShip);
61
62        this->registerVariables();
63
64        ModularSpaceShip::partMap_s = &(this->partMap_);
65
66    }
67
68    ModularSpaceShip::~ModularSpaceShip()
69    {
70        if (this->isInitialized())
71        {
72
73        }
74    }
75
76    void ModularSpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
77    {
78        SUPER(ModularSpaceShip, XMLPort, xmlelement, mode);
79        XMLPortObject(ModularSpaceShip, ShipPart, "parts", addShipPart, getShipPart, xmlelement, mode);
80    }
81
82    void ModularSpaceShip::registerVariables()
83    {
84        return;
85    }
86
87    /**
88    @brief
89        Searches for ShipParts matching to StaticEntities.
90    */
91    void ModularSpaceShip::updatePartAssignment()
92    {
93        // iterate through all attached objects
94        for (unsigned int i=0; i < (unsigned int)(this->getNumAttachedObj()); i++)
95        {
96            if (this->getAttachedObject(i) == NULL)
97            {
98                break;
99            }
100            // iterate through all attached parts
101            for(unsigned int j = 0; j < this->partList_.size(); j++)
102            {
103                // if the name of the part matches the name of the object, add the object to that parts entitylist (unless it was already done).
104                if((this->partList_[j]->getName() == this->getAttachedObject(i)->getName()) && !this->partList_[j]->hasEntity(orxonox_cast<StaticEntity*>(this->getAttachedObject(i))))
105                {
106                    // The Entity is added to the part's entityList_
107                    this->partList_[j]->addEntity(orxonox_cast<StaticEntity*>(this->getAttachedObject(i)));
108                    // An entry in the partMap_ is created, assigning the part to the entity.
109                    this->addPartEntityAssignment((StaticEntity*)(this->getAttachedObject(i)), this->partList_[j]);
110                }
111            }
112        }
113    }
114
115    void ModularSpaceShip::attach(WorldEntity* object)
116    {
117        SpaceShip::attach(object);
118        this->updatePartAssignment();
119    }
120
121    /**
122    @brief
123        Creates a new assignment for the given StaticEntity and ShipPart in the partMap_
124    @param entity
125        A pointer to the StaticEntity
126    @param part
127        A pointer to the ShipPart.
128    */
129    void ModularSpaceShip::addPartEntityAssignment(StaticEntity* entity, ShipPart* part)
130    {
131        if (!entity || !part)
132            return;
133
134        if (this->partMap_.find(entity) != this->partMap_.end())
135                {
136                    orxout(internal_warning) << "Assigning an Entity to multiple parts is not yet supported." << endl;
137                    return;
138                }
139
140        this->partMap_[entity] = part;
141    }
142
143
144    /**
145    @brief
146        Get the ShipPart an attached entity belongs to.
147    @param entity
148        The entity to be searched.
149    @return
150        Returns a pointer to the ShipPart the entity belongs to.
151    */
152    ShipPart* ModularSpaceShip::getPartOfEntity(StaticEntity* entity) const
153    {
154        for (std::map<StaticEntity*, ShipPart*>::const_iterator it = this->partMap_.begin(); it != this->partMap_.end(); ++it)
155        {
156            if (it->first == entity)
157                return it->second;
158        }
159        return NULL;
160    }
161
162    /**
163    @brief
164        If the damage occurred on an attached StaticEntity, the damage is given to the corresponding ShipPart to handle.
165    */
166    void ModularSpaceShip::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs)
167    {
168        if (this->getPartOfEntity((StaticEntity*)(cs->getUserPointer())) != NULL)
169            this->getPartOfEntity((StaticEntity*)(cs->getUserPointer()))->handleHit(damage, healthdamage, shielddamage, originator);
170        else
171            SpaceShip::damage(damage, healthdamage, shielddamage, originator, cs);
172    }
173
174    /**
175    @brief
176        Kills the ShipPart with the given name. Used from the console-command "ModularSpaceShip killshippart [string]".
177    @param name
178        The name of the part to be killed.
179    */
180    void ModularSpaceShip::killShipPart(std::string name)
181    {
182        for (std::map<StaticEntity*, ShipPart*>::const_iterator it = ModularSpaceShip::partMap_s->begin(); it != ModularSpaceShip::partMap_s->end(); ++it)
183        {
184            if (it->second->getName() == name)
185            {
186                it->second->death();
187                return;
188            }
189        }
190        orxout(internal_warning) << "Could not apply damage to ShipPart \"" << name << "\". Part not found." << endl;
191    }
192
193    /**
194    @brief
195        Add a ShipPart to the SpaceShip.
196    @param engine
197        A pointer to the ShipPart to be added.
198    */
199    void ModularSpaceShip::addShipPart(ShipPart* part)
200    {
201        OrxAssert(part != NULL, "The ShipPart cannot be NULL.");
202        this->partList_.push_back(part);
203        part->setParent(this);
204        this->updatePartAssignment();
205    }
206
207    /**
208    @brief
209        Get the i-th ShipPart of the SpaceShip.
210    @return
211        Returns a pointer to the i-the ShipPart. NULL if there is no ShipPart with that index.
212    */
213    ShipPart* ModularSpaceShip::getShipPart(unsigned int index)
214    {
215        if(this->partList_.size() <= index)
216            return NULL;
217        else
218            return this->partList_[index];
219    }
220
221    /**
222    @brief
223        Looks for an attached ShipPart with a certain name.
224    @param name
225        The name of the ShipPart to be returned.
226    @return
227        Pointer to the ShipPart with the given name, or NULL if not found.
228    */
229    ShipPart* ModularSpaceShip::getShipPartByName(std::string name)
230    {
231        for(std::vector<ShipPart*>::iterator it = this->partList_.begin(); it != this->partList_.end(); ++it)
232        {
233            if(orxonox_cast<ShipPart*>(*it)->getName() == name)
234            {
235                return orxonox_cast<ShipPart*>(*it);
236            }
237        }
238        orxout(internal_warning) << "Couldn't find ShipPart with name \"" << name << "\"." << endl;
239        return NULL;
240    }
241
242    /**
243    @brief
244        Check whether the SpaceShip has a particular Engine.
245    @param engine
246        A pointer to the Engine to be checked.
247    */
248    bool ModularSpaceShip::hasShipPart(ShipPart* part) const
249    {
250        for(unsigned int i = 0; i < this->partList_.size(); i++)
251        {
252            if(this->partList_[i] == part)
253                return true;
254        }
255        return false;
256    }
257
258
259    /**
260    @brief
261        Removes a ShipPart from the SpaceShip, destroying the corresponding StaticEntity
262    @param part
263        The ShipPart to be removed.
264    */
265    void ModularSpaceShip::removeShipPart(ShipPart* part)
266    {
267        // Remove the part from the partList_
268        std::vector<ShipPart*>::iterator it = this->partList_.begin();
269        for(unsigned int i = 0; i < this->partList_.size(); i++)
270        {
271            if(this->partList_[i] == part)
272            {
273                this->partList_.erase(it);
274                break;
275            }
276            it++;
277        }
278        // Remove the part-entity assignment and detach the Entity of this ShipPart
279        for (std::map<StaticEntity*, ShipPart*>::iterator itt = this->partMap_.begin(); itt != this->partMap_.end(); )
280        {
281            if (itt->second == part)
282            {
283                this->detach(itt->first);
284                itt->first->destroy();
285                //itt->first->setActive(false);
286                //itt->first->setVisible(false);
287                //itt->first->setCollisionResponse(false);
288                //itt->first->setCollisionType(None);
289                //itt->first->deactivatePhysics();
290                this->partMap_.erase(itt++);
291            } else {
292                ++itt;
293            }
294        }
295    }
296
297    /**
298    @brief
299        Looks for an attached Engine with a certain name.
300    @param name
301        The name of the engine to be returned.
302    @return
303        Pointer to the engine with the given name, or NULL if not found.
304    */
305    Engine* ModularSpaceShip::getEngineByName(std::string name)
306    {
307        for(std::vector<Engine*>::iterator it = this->engineList_.begin(); it != this->engineList_.end(); ++it)
308        {
309            if(orxonox_cast<Engine*>(*it)->getName() == name)
310            {
311                return orxonox_cast<Engine*>(*it);
312            }
313        }
314        orxout(internal_warning) << "Couldn't find Engine with name \"" << name << "\"." << endl;
315        return NULL;
316    }
317
318    /**
319    @brief
320        Detaches a child WorldEntity from this instance.
321    */
322    void ModularSpaceShip::detach(WorldEntity* object)
323    {
324        std::set<WorldEntity*>::iterator it = this->children_.find(object);
325        if (it == this->children_.end())
326        {
327            orxout(internal_warning) << "Cannot detach an object that is not a child." << endl;
328            return;
329        }
330
331        // collision shapes
332
333        //this->printBtChildShapes((btCompoundShape*)(this->getWorldEntityCollisionShape()->getCollisionShape()), 2, 0);
334        this->detachCollisionShape(object->collisionShape_);  // after succeeding, causes a crash in the collision handling
335        //this->printBtChildShapes((btCompoundShape*)(this->getWorldEntityCollisionShape()->getCollisionShape()), 2, 0);
336
337        // mass
338        if (object->getMass() > 0.0f)
339        {
340            this->childrenMass_ -= object->getMass();
341            recalculateMassProps();
342        }
343
344        this->detachNode(object->node_);
345        this->children_.erase(it);        // this causes a crash when unloading the level. Or not?
346
347        object->notifyDetached();
348    }
349}
Note: See TracBrowser for help on using the repository browser.