Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Cleaned up code. Added console command "ModularSpaceShip killshippart [string]" which allows manual destruction of a ShipPart by name. Added more functionality to PartDestructionEvents.

File size: 8.8 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    /**
93    @brief
94        Is called when the ShipPart dies.
95        Executes PartDestructionEvents.
96        Tells the ModularSpaceShip to remove this ShipPart.
97    */
98    void ShipPart::death()
99    {
100        if (!(this->isAlive()))
101            return;
102
103        this->setAlive(false);
104
105        // Execute all destruction events
106        for (unsigned int i = 0; i < this->eventList_.size(); i++)
107        {
108            orxout() << "executing" << endl;
109            this->getDestructionEvent(i)->execute();
110        }
111
112        // Remove this ShipPart from the parent.
113        this->parent_->removeShipPart(this);
114        orxout() << this->getName() << " has died." << endl;
115    }
116
117    /**
118    @brief
119        Add a StaticEntity to the ShipPart.
120    @param engine
121        A pointer to the StaticEntity to be added.
122    */
123    void ShipPart::addEntity(StaticEntity* entity)
124    {
125        OrxAssert(entity != NULL, "The Entity cannot be NULL.");
126        this->entityList_.push_back(entity);
127    }
128
129    /**
130    @brief
131        Get the i-th StaticEntity of the ShipPart.
132    @return
133        Returns a pointer to the i-the StaticEntity. NULL if there is no StaticEntity with that index.
134    */
135    StaticEntity* ShipPart::getEntity(unsigned int index)
136    {
137        if(this->entityList_.size() >= index)
138            return NULL;
139        else
140            return this->entityList_[index];
141    }
142
143    /**
144    @brief
145        Check whether the ShipPart has a particular Entity.
146    @param engine
147        A pointer to the Entity to be checked.
148    */
149    bool ShipPart::hasEntity(StaticEntity* entity) const
150    {
151        for(unsigned int i = 0; i < this->entityList_.size(); i++)
152        {
153            if(this->entityList_[i] == entity)
154                return true;
155        }
156        return false;
157    }
158
159    void ShipPart::printEntities()
160    {
161        orxout() << "ShipPart " << this->getName() << " has the following entities assigned:" << endl;
162        for(unsigned int j = 0; j < this->entityList_.size(); j++)
163        {
164            orxout() << "  " << this->entityList_[j]->getName() << endl;
165        }
166    }
167
168    /**
169    @brief
170        Add a PartDestructionEvent to the ShipPart.
171    @param engine
172        A pointer to the PartDestructionEvent to be added.
173    */
174    void ShipPart::addDestructionEvent(PartDestructionEvent* event)
175    {
176        OrxAssert(event != NULL, "The PartDestructionEvent cannot be NULL.");
177        event->setParent(this);
178        this->eventList_.push_back(event);
179    }
180
181    /**
182    @brief
183        Get the i-th PartDestructionEvent of the ShipPart.
184    @return
185        Returns a pointer to the i-the PartDestructionEvent. NULL if there is no PartDestructionEvent with that index.
186    */
187    PartDestructionEvent* ShipPart::getDestructionEvent(unsigned int index)
188    {
189        if(this->eventList_.size() <= index)
190            return NULL;
191        else
192            return this->eventList_[index];
193    }
194
195    void ShipPart::setDamageAbsorption(float value)
196    {
197        this->damageAbsorption_ = value;
198    }
199
200    void ShipPart::setParent(ModularSpaceShip* ship)
201    {
202        this->parent_ = ship;
203    }
204
205    /**
206    @brief
207        Sets the health of the ShipPart.
208    */
209    void ShipPart::setHealth(float health)
210    {
211        this->health_ = health;
212    }
213
214    /**
215    @brief
216        Handles a received hit.
217    */
218    void ShipPart::handleHit(float damage, float healthdamage, float shielddamage, Pawn* originator)
219    {
220        orxout() << "ShipPart " <<this->getName() << " is handling a hit!" << endl;
221
222        if (parent_->getGametype() && parent_->getGametype()->allowPawnDamage(parent_, originator))
223        {
224            if (shielddamage >= parent_->getShieldHealth())
225            {
226                parent_->setShieldHealth(0);
227                this->setHealth(this->health_ - (healthdamage + damage) * this->damageAbsorption_);
228                parent_->setHealth(parent_->getHealth() - (healthdamage + damage) * (1 - this->damageAbsorption_));
229            }
230            else
231            {
232                parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage);
233
234                // remove remaining shieldAbsorpton-Part of damage from shield
235                shielddamage = damage * parent_->getShieldAbsorption();
236                shielddamage = std::min(parent_->getShieldHealth(),shielddamage);
237                parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage);
238
239                // set remaining damage to health
240                this->setHealth(this->health_ - ((damage - shielddamage) - healthdamage) * this->damageAbsorption_);
241                parent_->setHealth(parent_->getHealth() - ((damage - shielddamage) - healthdamage) * (1- this->damageAbsorption_));
242            }
243        }
244        if (this->health_ < 0)
245            this->death();
246        orxout() << "Health of ShipPart " << this->getName() << " is " << this->getHealth() << endl;
247    }
248
249
250    /**
251    @brief
252        Adds the ShipPart to the input SpaceShip.
253    @param ship
254        A pointer to the SpaceShip to which the ShipPart is added.
255    */
256    /*void ShipPart::addToSpaceShip(ModularSpaceShip* ship)
257    {
258        this->parent_ = ship;
259
260        if (ship)
261        {
262            this->parentID_ = ship->getObjectID();
263            if (!ship->hasShipPart(this))
264                ship->addShipPart(this);
265        }
266    }*/
267
268}
Note: See TracBrowser for help on using the repository browser.