Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/items/MunitionPickup.cc @ 11212

Last change on this file since 11212 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 4.5 KB
RevLine 
[10692]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 *      Fabien Vultier
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file MunitionPickup.cc
31    @brief Implementation of the MunitionPickup class.
32*/
33
34#include "MunitionPickup.h"
35
36#include <sstream>
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39
[10715]40#include "worldentities/pawns/Pawn.h"
[10692]41
42namespace orxonox
43{
44    RegisterClass(MunitionPickup);
45
46    /**
47    @brief
48        Constructor. Registers the object and initializes the member variables.
49    */
50    MunitionPickup::MunitionPickup(Context* context) : Pickup(context)
51    {
52        RegisterObject(MunitionPickup);
53
54        this->initialize();
55    }
56
57    /**
58    @brief
59        Destructor.
60    */
61    MunitionPickup::~MunitionPickup()
62    {
63    }
64
65    /**
66    @brief
67        Initializes the member variables.
68    */
69    void MunitionPickup::initialize(void)
70    {
71        //Defines who is allowed to pick up the pickup.
72        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
73    }
74
75    /**
76    @brief
77        Method for creating a MunitionPickup object through XML.
78    */
79    void MunitionPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
80    {
81        SUPER(MunitionPickup, XMLPort, xmlelement, mode);
82
[10715]83        XMLPortObject(MunitionPickup, MunitionContainer, "munitioncontainers", addMunitionContainer, getMunitionContainer, xmlelement, mode);
[10692]84    }
85
[10715]86    void MunitionPickup::addMunitionContainer(MunitionContainer* munitionContainer)
87    {
[11071]88        OrxAssert(munitionContainer != nullptr, "The munitionContainer cannot be nullptr.");
[10715]89        this->munitionContainers_.push_back(munitionContainer);
90    }
91
92    MunitionContainer* MunitionPickup::getMunitionContainer(unsigned int index)
93    {
94        if(this->munitionContainers_.size() >= index)
[11071]95            return nullptr;
[10715]96        else
97            return this->munitionContainers_[index];
98    }
99
[10692]100    /**
101    @brief
102        Is called when the pickup has transisted from used to unused or the other way around.
103    */
104    void MunitionPickup::changedUsed(void)
105    {
106        SUPER(MunitionPickup, changedUsed);
107
108        Pawn* pawn = this->carrierToPawnHelper();
109
[11071]110        if(pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
[10692]111            this->Pickupable::destroy();
112
113        // If the pickup has transited to used.
114        if(this->isUsed())
115        {
[11071]116            for(MunitionContainer* container : this->munitionContainers_)
[10692]117            {
[10715]118                //Get pointer to the appropriate munition
[11071]119                SubclassIdentifier<Munition> identifier = container->getMunitionType();
[10715]120                Munition* munition = pawn->getMunition(&identifier);
121                if (munition)
122                {
123                    // Add munition and magzines
[11071]124                    munition->addMunition(container->getMunitionAmount());
125                    munition->addMagazines(container->getMagazinesAmount());
[10715]126                }
[11071]127                container->destroy();
[10715]128            }
129            // This will destroy the pickp
130            this->setUsed(false);
[10692]131        }
132        else
133        {
134            this->Pickupable::destroy();
[10721]135        }
[10715]136    }   
[10692]137
138    /**
139    @brief
140        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
141    @return
[11071]142        A pointer to the Pawn, or nullptr if the conversion failed.
[10692]143    */
144    Pawn* MunitionPickup::carrierToPawnHelper(void)
145    {
146        PickupCarrier* carrier = this->getCarrier();
147        Pawn* pawn = orxonox_cast<Pawn*>(carrier);
148
[11071]149        if(pawn == nullptr)
[10692]150        {
151            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in MunitionPickup." << endl;
152        }
153        return pawn;
[10715]154    }     
[10692]155}
156
Note: See TracBrowser for help on using the repository browser.