Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fixed yet another segfault (which we hadn't seen yet).
Cleared emptyLevel, created two testlevels (testing with boxes)

File size: 8.7 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#include "items/PartDestructionEvent.h"
43
44
45namespace orxonox
46{
47    RegisterClass(ShipPart);
48
49    ShipPart::ShipPart(Context* context)
50        : Item(context)
51    {
52        RegisterObject(ShipPart);
53        this->setAlive(true);
54    }
55
56    ShipPart::~ShipPart()
57    {
58
59    }
60
61    void ShipPart::XMLPort(Element& xmlelement, XMLPort::Mode mode)
62    {
63        SUPER(ShipPart, XMLPort, xmlelement, mode);
64
65        XMLPortParam(ShipPart, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
66        XMLPortParam(ShipPart, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
67        XMLPortParam(ShipPart, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
68
69        XMLPortParam(ShipPart, "damageabsorption", setDamageAbsorption, getDamageAbsorption, xmlelement, mode).defaultValues(0.5);
70
71        XMLPortObject(ShipPart, PartDestructionEvent, "destructionevents", addDestructionEvent, getDestructionEvent, xmlelement, mode);
72
73        /*
74        XMLPortParam(ShipPart, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0);
75        XMLPortParam(ShipPart, "initialshieldhealth", setInitialShieldHealth, getInitialShieldHealth, xmlelement, mode).defaultValues(0);
76        XMLPortParam(ShipPart, "maxshieldhealth", setMaxShieldHealth, getMaxShieldHealth, xmlelement, mode).defaultValues(100);
77        XMLPortParam(ShipPart, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0);
78
79        XMLPortParam(ShipPart, "sShipPartparticlesource", setSShipPartParticleSource, getSShipPartParticleSource, xmlelement, mode);
80        XMLPortParam(ShipPart, "sShipPartparticleduration", setSShipPartParticleDuration, getSShipPartParticleDuration, xmlelement, mode).defaultValues(3.0f);
81        XMLPortParam(ShipPart, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
82
83        XMLPortParam(ShipPart, "reloadrate", setReloadRate, getReloadRate, xmlelement, mode).defaultValues(0);
84        XMLPortParam(ShipPart, "reloadwaittime", setReloadWaitTime, getReloadWaitTime, xmlelement, mode).defaultValues(1.0f);
85
86        XMLPortParam(ShipPart, "explosionSound",  setExplosionSound,  getExplosionSound,  xmlelement, mode);
87
88        XMLPortParam ( RadarViewable, "radarname", setRadarName, getRadarName, xmlelement, mode );
89        */
90    }
91
92    void ShipPart::death()
93    {
94        if (!(this->isAlive()))
95            return;
96
97        this->setAlive(false);
98
99        // Execute all destruction events
100        for (unsigned int i = 0; i < this->eventList_.size(); i++)
101        {
102            orxout() << "executing" << endl;
103            this->getDestructionEvent(i)->execute();
104        }
105
106        // Remove this ShipPart from the parent.
107        this->parent_->removeShipPart(this);
108        orxout() << this->getName() << " has died." << endl;
109    }
110
111    /**
112    @brief
113        Add a StaticEntity to the ShipPart.
114    @param engine
115        A pointer to the StaticEntity to be added.
116    */
117    void ShipPart::addEntity(StaticEntity* entity)
118    {
119        OrxAssert(entity != NULL, "The Entity cannot be NULL.");
120        this->entityList_.push_back(entity);
121    }
122
123    /**
124    @brief
125        Get the i-th StaticEntity of the ShipPart.
126    @return
127        Returns a pointer to the i-the StaticEntity. NULL if there is no StaticEntity with that index.
128    */
129    StaticEntity* ShipPart::getEntity(unsigned int index)
130    {
131        if(this->entityList_.size() >= index)
132            return NULL;
133        else
134            return this->entityList_[index];
135    }
136
137    /**
138    @brief
139        Check whether the ShipPart has a particular Entity.
140    @param engine
141        A pointer to the Entity to be checked.
142    */
143    bool ShipPart::hasEntity(StaticEntity* entity) const
144    {
145        for(unsigned int i = 0; i < this->entityList_.size(); i++)
146        {
147            if(this->entityList_[i] == entity)
148                return true;
149        }
150        return false;
151    }
152
153    void ShipPart::printEntities()
154    {
155        orxout() << "ShipPart " << this->getName() << " has the following entities assigned:" << endl;
156        for(unsigned int j = 0; j < this->entityList_.size(); j++)
157        {
158            orxout() << "  " << this->entityList_[j]->getName() << endl;
159        }
160    }
161
162    /**
163    @brief
164        Add a PartDestructionEvent to the ShipPart.
165    @param engine
166        A pointer to the PartDestructionEvent to be added.
167    */
168    void ShipPart::addDestructionEvent(PartDestructionEvent* event)
169    {
170        OrxAssert(event != NULL, "The PartDestructionEvent cannot be NULL.");
171        event->setParent(this);
172        this->eventList_.push_back(event);
173    }
174
175    /**
176    @brief
177        Get the i-th PartDestructionEvent of the ShipPart.
178    @return
179        Returns a pointer to the i-the PartDestructionEvent. NULL if there is no PartDestructionEvent with that index.
180    */
181    PartDestructionEvent* ShipPart::getDestructionEvent(unsigned int index)
182    {
183        if(this->eventList_.size() <= index)
184            return NULL;
185        else
186            return this->eventList_[index];
187    }
188
189    void ShipPart::setDamageAbsorption(float value)
190    {
191        this->damageAbsorption_ = value;
192    }
193
194    void ShipPart::setParent(ModularSpaceShip* ship)
195    {
196        this->parent_ = ship;
197    }
198
199    /**
200    @brief
201        Sets the health of the ShipPart.
202    */
203    void ShipPart::setHealth(float health)
204    {
205        this->health_ = health;
206    }
207
208    /**
209    @brief
210        Handles a received hit.
211    */
212    void ShipPart::handleHit(float damage, float healthdamage, float shielddamage, Pawn* originator)
213    {
214        orxout() << "ShipPart " <<this->getName() << " is handling a hit!" << endl;
215
216        if (parent_->getGametype() && parent_->getGametype()->allowPawnDamage(parent_, originator))
217        {
218            if (shielddamage >= parent_->getShieldHealth())
219            {
220                parent_->setShieldHealth(0);
221                this->setHealth(this->health_ - (healthdamage + damage) * this->damageAbsorption_);
222                parent_->setHealth(parent_->getHealth() - (healthdamage + damage) * (1 - this->damageAbsorption_));
223            }
224            else
225            {
226                parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage);
227
228                // remove remaining shieldAbsorpton-Part of damage from shield
229                shielddamage = damage * parent_->getShieldAbsorption();
230                shielddamage = std::min(parent_->getShieldHealth(),shielddamage);
231                parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage);
232
233                // set remaining damage to health
234                this->setHealth(this->health_ - ((damage - shielddamage) - healthdamage) * this->damageAbsorption_);
235                parent_->setHealth(parent_->getHealth() - ((damage - shielddamage) - healthdamage) * (1- this->damageAbsorption_));
236            }
237        }
238        if (this->health_ < 0)
239            this->death();
240        orxout() << "Health of ShipPart " << this->getName() << " is " << this->getHealth() << endl;
241    }
242
243
244    /**
245    @brief
246        Adds the ShipPart to the input SpaceShip.
247    @param ship
248        A pointer to the SpaceShip to which the ShipPart is added.
249    */
250    /*void ShipPart::addToSpaceShip(ModularSpaceShip* ship)
251    {
252        this->parent_ = ship;
253
254        if (ship)
255        {
256            this->parentID_ = ship->getObjectID();
257            if (!ship->hasShipPart(this))
258                ship->addShipPart(this);
259        }
260    }*/
261
262}
Note: See TracBrowser for help on using the repository browser.