Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentationFS14/src/orxonox/worldentities/pawns/ModularSpaceShip.cc @ 10212

Last change on this file since 10212 was 10212, checked in by landauf, 9 years ago

removed detach() from ModularSpaceShip which was just copy-pasted from its base class WorldEntity.
removed another unused function from WorldEntity.

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