Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Continued working on ShipPart and ModularSpaceShip implementation

File size: 5.0 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 "gametypes/Gametype.h"
40#include "worldentities/StaticEntity.h"
41
42
43namespace orxonox
44{
45    RegisterClass(ShipPart);
46
47    ShipPart::ShipPart(Context* context)
48        : Item(context)
49    {
50        RegisterObject(ShipPart);
51    }
52
53    ShipPart::~ShipPart()
54    {
55
56    }
57
58
59    /**
60    @brief
61        Add a StaticEntity to the ShipPart.
62    @param engine
63        A pointer to the StaticEntity to be added.
64    */
65    void ShipPart::addEntity(StaticEntity* entity)
66    {
67        OrxAssert(entity != NULL, "The Entity cannot be NULL.");
68        this->entityList_.push_back(entity);
69        //part->addToSpaceShip(this); //FIXME: (noep) add
70    }
71
72    /**
73    @brief
74        Get the i-th StaticEntity of the ShipPart.
75    @return
76        Returns a pointer to the i-the StaticEntity. NULL if there is no StaticEntity with that index.
77    */
78    StaticEntity* ShipPart::getEntity(unsigned int index)
79    {
80        if(this->entityList_.size() >= index)
81            return NULL;
82        else
83            return this->entityList_[index];
84    }
85
86    /**
87    @brief
88        Check whether the ShipPart has a particular Entity.
89    @param engine
90        A pointer to the Entity to be checked.
91    */
92    bool ShipPart::hasEntity(StaticEntity* entity) const
93    {
94        for(unsigned int i = 0; i < this->entityList_.size(); i++)
95        {
96            if(this->entityList_[i] == entity)
97                return true;
98        }
99        return false;
100    }
101
102    void ShipPart::printEntities()
103    {
104        orxout() << "ShipPart " << this->getName() << " has the following entities assigned:" << endl;
105        for(unsigned int j = 0; j < this->entityList_.size(); j++)
106        {
107            orxout() << "  " << this->entityList_[j]->getName() << endl;
108        }
109    }
110
111    void ShipPart::setDamageAbsorption(float value)
112    {
113        this->damageAbsorption_ = value;
114    }
115
116    /**
117    @brief
118        Sets the health of the ShipPart.
119    */
120    void ShipPart::setHealth(float health)
121    {
122        this->health_ = health;
123    }
124
125    /**
126    @brief
127        Handles a received hit.
128    */
129    void ShipPart::handleHit(float damage, float healthdamage, float shielddamage, Pawn* originator)
130    {
131        orxout() << "ShipPart " <<this->getName() << " is handling a hit!" << endl;
132        if (parent_->getGametype() && parent_->getGametype()->allowPawnDamage(parent_, originator))
133        {
134            if (shielddamage >= parent_->getShieldHealth())
135            {
136                parent_->setShieldHealth(0);
137                this->setHealth(this->health_ - (healthdamage + damage) * this->damageAbsorption_);
138                parent_->setHealth(parent_->getHealth() - (healthdamage + damage) * (1 - this->damageAbsorption_));
139            }
140            else
141            {
142                parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage);
143
144                // remove remaining shieldAbsorpton-Part of damage from shield
145                shielddamage = damage * parent_->getShieldAbsorption();
146                shielddamage = std::min(parent_->getShieldHealth(),shielddamage);
147                parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage);
148
149                // set remaining damage to health
150                this->setHealth(this->health_ - ((damage - shielddamage) - healthdamage) * this->damageAbsorption_);
151                parent_->setHealth(parent_->getHealth() - ((damage - shielddamage) - healthdamage) * (1- this->damageAbsorption_));
152            }
153        }
154    }
155
156
157    /**
158    @brief
159        Adds the ShipPart to the input SpaceShip.
160    @param ship
161        A pointer to the SpaceShip to which the ShipPart is added.
162    */
163    /*void ShipPart::addToSpaceShip(ModularSpaceShip* ship)
164    {
165        this->parent_ = ship;
166
167        if (ship)
168        {
169            this->parentID_ = ship->getObjectID();
170            if (!ship->hasShipPart(this))
171                ship->addShipPart(this);
172        }
173    }*/
174
175}
Note: See TracBrowser for help on using the repository browser.