Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/modularships/src/orxonox/items/ShipPart.cc @ 10029

Last change on this file since 10029 was 10023, checked in by noep, 11 years ago

ShipParts can "die" and detach the corresponding Entity while doing so. Issue: The Entity being detached while a hit on a collisionshape is being handled causes a runtime-error.

File size: 7.3 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 *      Noe Pedrazzini
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "ShipPart.h"
30
31#include <algorithm>
32
33#include "core/CoreIncludes.h"
34#include "core/GameMode.h"
35#include "core/XMLPort.h"
36#include "network/NetworkFunction.h"
37#include "Item.h"
38#include "worldentities/pawns/Pawn.h"
39#include "worldentities/pawns/ModularSpaceShip.h"
40#include "gametypes/Gametype.h"
41#include "worldentities/StaticEntity.h"
42
43
44namespace orxonox
45{
46    RegisterClass(ShipPart);
47
48    ShipPart::ShipPart(Context* context)
49        : Item(context)
50    {
51        RegisterObject(ShipPart);
52    }
53
54    ShipPart::~ShipPart()
55    {
56
57    }
58
59    void ShipPart::XMLPort(Element& xmlelement, XMLPort::Mode mode)
60    {
61        SUPER(ShipPart, XMLPort, xmlelement, mode);
62
63        XMLPortParam(ShipPart, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
64        XMLPortParam(ShipPart, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
65        XMLPortParam(ShipPart, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
66
67        XMLPortParam(ShipPart, "damageabsorption", setDamageAbsorption, getDamageAbsorption, xmlelement, mode).defaultValues(0.5);
68
69        /*
70        XMLPortParam(ShipPart, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0);
71        XMLPortParam(ShipPart, "initialshieldhealth", setInitialShieldHealth, getInitialShieldHealth, xmlelement, mode).defaultValues(0);
72        XMLPortParam(ShipPart, "maxshieldhealth", setMaxShieldHealth, getMaxShieldHealth, xmlelement, mode).defaultValues(100);
73        XMLPortParam(ShipPart, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0);
74
75        XMLPortParam(ShipPart, "sShipPartparticlesource", setSShipPartParticleSource, getSShipPartParticleSource, xmlelement, mode);
76        XMLPortParam(ShipPart, "sShipPartparticleduration", setSShipPartParticleDuration, getSShipPartParticleDuration, xmlelement, mode).defaultValues(3.0f);
77        XMLPortParam(ShipPart, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
78
79        XMLPortParam(ShipPart, "reloadrate", setReloadRate, getReloadRate, xmlelement, mode).defaultValues(0);
80        XMLPortParam(ShipPart, "reloadwaittime", setReloadWaitTime, getReloadWaitTime, xmlelement, mode).defaultValues(1.0f);
81
82        XMLPortParam(ShipPart, "explosionSound",  setExplosionSound,  getExplosionSound,  xmlelement, mode);
83
84        XMLPortParam ( RadarViewable, "radarname", setRadarName, getRadarName, xmlelement, mode );
85        */
86    }
87
88    void ShipPart::death()
89    {
90        this->parent_->removeShipPart(this);
91    }
92
93    /**
94    @brief
95        Add a StaticEntity to the ShipPart.
96    @param engine
97        A pointer to the StaticEntity to be added.
98    */
99    void ShipPart::addEntity(StaticEntity* entity)
100    {
101        OrxAssert(entity != NULL, "The Entity cannot be NULL.");
102        this->entityList_.push_back(entity);
103        //part->addToSpaceShip(this); //FIXME: (noep) add
104    }
105
106    /**
107    @brief
108        Get the i-th StaticEntity of the ShipPart.
109    @return
110        Returns a pointer to the i-the StaticEntity. NULL if there is no StaticEntity with that index.
111    */
112    StaticEntity* ShipPart::getEntity(unsigned int index)
113    {
114        if(this->entityList_.size() >= index)
115            return NULL;
116        else
117            return this->entityList_[index];
118    }
119
120    /**
121    @brief
122        Check whether the ShipPart has a particular Entity.
123    @param engine
124        A pointer to the Entity to be checked.
125    */
126    bool ShipPart::hasEntity(StaticEntity* entity) const
127    {
128        for(unsigned int i = 0; i < this->entityList_.size(); i++)
129        {
130            if(this->entityList_[i] == entity)
131                return true;
132        }
133        return false;
134    }
135
136    void ShipPart::printEntities()
137    {
138        orxout() << "ShipPart " << this->getName() << " has the following entities assigned:" << endl;
139        for(unsigned int j = 0; j < this->entityList_.size(); j++)
140        {
141            orxout() << "  " << this->entityList_[j]->getName() << endl;
142        }
143    }
144
145    void ShipPart::setDamageAbsorption(float value)
146    {
147        this->damageAbsorption_ = value;
148    }
149
150    void ShipPart::setParent(ModularSpaceShip* ship)
151    {
152        this->parent_ = ship;
153    }
154
155    /**
156    @brief
157        Sets the health of the ShipPart.
158    */
159    void ShipPart::setHealth(float health)
160    {
161        this->health_ = health;
162    }
163
164    /**
165    @brief
166        Handles a received hit.
167    */
168    void ShipPart::handleHit(float damage, float healthdamage, float shielddamage, Pawn* originator)
169    {
170        orxout() << "ShipPart " <<this->getName() << " is handling a hit!" << endl;
171        if (parent_->getGametype() && parent_->getGametype()->allowPawnDamage(parent_, originator))
172        {
173            if (shielddamage >= parent_->getShieldHealth())
174            {
175                parent_->setShieldHealth(0);
176                this->setHealth(this->health_ - (healthdamage + damage) * this->damageAbsorption_);
177                parent_->setHealth(parent_->getHealth() - (healthdamage + damage) * (1 - this->damageAbsorption_));
178            }
179            else
180            {
181                parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage);
182
183                // remove remaining shieldAbsorpton-Part of damage from shield
184                shielddamage = damage * parent_->getShieldAbsorption();
185                shielddamage = std::min(parent_->getShieldHealth(),shielddamage);
186                parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage);
187
188                // set remaining damage to health
189                this->setHealth(this->health_ - ((damage - shielddamage) - healthdamage) * this->damageAbsorption_);
190                parent_->setHealth(parent_->getHealth() - ((damage - shielddamage) - healthdamage) * (1- this->damageAbsorption_));
191            }
192        }
193        if (this->health_ < 0)
194            this->death();
195        orxout() << "Health of ShipPart " << this->getName() << " is " << this->getHealth() << endl;
196    }
197
198
199    /**
200    @brief
201        Adds the ShipPart to the input SpaceShip.
202    @param ship
203        A pointer to the SpaceShip to which the ShipPart is added.
204    */
205    /*void ShipPart::addToSpaceShip(ModularSpaceShip* ship)
206    {
207        this->parent_ = ship;
208
209        if (ship)
210        {
211            this->parentID_ = ship->getObjectID();
212            if (!ship->hasShipPart(this))
213                ship->addShipPart(this);
214        }
215    }*/
216
217}
Note: See TracBrowser for help on using the repository browser.