Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10066 was 10058, checked in by noep, 11 years ago

Expanded functionality of PartDestructionEvents, fixed cursor not showing when flying a ModularSpaceShip

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