Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/fabienHS15/src/modules/pickup/items/MunitionPickup.cc @ 10715

Last change on this file since 10715 was 10715, checked in by fvultier, 9 years ago
File size: 4.5 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 *      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
40#include "worldentities/pawns/Pawn.h"
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
83        XMLPortObject(MunitionPickup, MunitionContainer, "munitioncontainers", addMunitionContainer, getMunitionContainer, xmlelement, mode);
84    }
85
86    void MunitionPickup::addMunitionContainer(MunitionContainer* munitionContainer)
87    {
88        OrxAssert(munitionContainer != NULL, "The munitionContainer cannot be NULL.");
89        this->munitionContainers_.push_back(munitionContainer);
90    }
91
92    MunitionContainer* MunitionPickup::getMunitionContainer(unsigned int index)
93    {
94        if(this->munitionContainers_.size() >= index)
95            return NULL;
96        else
97            return this->munitionContainers_[index];
98    }
99
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
110        if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
111            this->Pickupable::destroy();
112
113
114        // If the pickup has transited to used.
115        if(this->isUsed())
116        {
117            for(std::vector<MunitionContainer*>::iterator it = this->munitionContainers_.begin(); it != this->munitionContainers_.end(); ++it)
118            {
119                //Get pointer to the appropriate munition
120                SubclassIdentifier<Munition> identifier = (*it)->getMunitionType();
121                Munition* munition = pawn->getMunition(&identifier);
122                if (munition)
123                {
124                    // Add munition and magzines
125                    munition->addMunition((*it)->getMunitionAmount());
126                    munition->addMagazines((*it)->getMagazinesAmount());
127                }
128                (*it)->destroy();
129            }
130            // This will destroy the pickp
131            this->setUsed(false);
132        }
133        else
134        {
135            this->Pickupable::destroy();
136        }       
137    }   
138
139    /**
140    @brief
141        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
142    @return
143        A pointer to the Pawn, or NULL if the conversion failed.
144    */
145    Pawn* MunitionPickup::carrierToPawnHelper(void)
146    {
147        PickupCarrier* carrier = this->getCarrier();
148        Pawn* pawn = orxonox_cast<Pawn*>(carrier);
149
150        if(pawn == NULL)
151        {
152            orxout(internal_error, context::pickups) << "Invalid PickupCarrier in MunitionPickup." << endl;
153        }
154        return pawn;
155    }     
156}
157
Note: See TracBrowser for help on using the repository browser.