Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/items/DronePickup.cc @ 7297

Last change on this file since 7297 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

  • Property svn:eol-style set to native
File size: 5.5 KB
RevLine 
[6782]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 *      Lukas Gasser
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file DronePickup.cc
31    @brief Implementation of the DronePickup class.
32*/
33
34#include "DronePickup.h"
35#include "worldentities/Drone.h"
36#include "controllers/DroneController.h"
37
38#include "core/CoreIncludes.h"
39#include "core/XMLPort.h"
40#include "util/StringUtils.h"
41
42#include "worldentities/pawns/Pawn.h"
43#include "pickup/PickupIdentifier.h"
44
45#include <sstream>
46
47namespace orxonox
48{
49
50    CreateFactory(DronePickup);
[7127]51
[6782]52    /**
53    @brief
54        Constructor. Registers the object and initializes the member variables.
55    */
56    DronePickup::DronePickup(BaseObject* creator) : Pickup(creator)
57    {
58        RegisterObject(DronePickup);
[7127]59
[6782]60        this->initialize();
61    }
[7127]62
[6782]63    /**
64    @brief
65        Destructor.
66    */
67    DronePickup::~DronePickup()
68    {
[7127]69
[6782]70    }
[7127]71
[6782]72    /**
[7127]73    @brief
[6782]74        Initializes the member variables.
75    */
76    void DronePickup::initialize(void)
77    {
78        this->addTarget(ClassIdentifier<Pawn>::getIdentifier());
79        this->setDurationTypeDirect(pickupDurationType::once);
80        this->droneTemplate_ = "";
81    }
[7127]82
[6782]83    /**
84    @brief
85        Initializes the PickupIdentifier of this pickup.
86    */
87    void DronePickup::initializeIdentifier(void)
88    {
89        std::string val = this->getDroneTemplate();
90        std::string type = "droneTemplate";
91        this->pickupIdentifier_->addParameter(type, val);
92    }
[7127]93
[6782]94    /**
95    @brief
96        Method for creating a DronePickup object through XML.
97    */
98    void DronePickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
99    {
100        SUPER(DronePickup, XMLPort, xmlelement, mode);
101        XMLPortParam(DronePickup, "droneTemplate", setDroneTemplate, getDroneTemplate, xmlelement, mode);
[7127]102
[6782]103        this->initializeIdentifier();
104    }
[7127]105
[6782]106    void DronePickup::setDroneTemplate(std::string templatename){
107        droneTemplate_ = templatename;
[7127]108    }
109
[6782]110    const std::string& DronePickup::getDroneTemplate() const
111    {
112        return droneTemplate_;
113    }
114
115    /**
116    @brief
117        Is called when the pickup has transited from used to unused or the other way around.
118    */
119    void DronePickup::changedUsed(void)
120    {
121        SUPER(DronePickup, changedUsed);
[7127]122
[6782]123        //! If the pickup is not picked up nothing must be done.
124        if(!this->isPickedUp())
125            return;
[7127]126
[6782]127        //! If the pickup has transited to used.
128        if(this->isUsed())
129        {
130
131                Pawn* pawn = this->carrierToPawnHelper();
132                if(pawn == NULL) //!< If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
[7162]133                    this->Pickupable::destroy();
[7127]134
[6782]135                //Attach to pawn
[6891]136                Drone* drone = new Drone(pawn->getCreator()); // this is neccessary because the projectiles fired need a valid creator for the particlespawner (when colliding against something)
[6782]137                drone->addTemplate(this->getDroneTemplate());
[6891]138
[6782]139                Controller* controller = drone->getController();
140                DroneController* droneController = dynamic_cast<DroneController*>(controller);
141                if(droneController != NULL)
142                {
[6847]143                    droneController->setOwner(pawn);
[6782]144                }
[7127]145
[6891]146                Vector3 spawnPosition = pawn->getWorldPosition() + Vector3(30,0,-30);
147                drone->setPosition(spawnPosition);
[7127]148
[6782]149                //! The pickup has been used up.
150                this->setUsed(false);
151        }
152        else
153        {
154            //! If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused.
155            if(this->isOnce() || (this->isContinuous() ))
156            {
[7162]157                this->Pickupable::destroy();
[6782]158            }
159        }
160    }
[7127]161
[6782]162    /**
163    @brief
164        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
165    @return
166        A pointer to the Pawn, or NULL if the conversion failed.
167    */
168    Pawn* DronePickup::carrierToPawnHelper(void)
169    {
170        PickupCarrier* carrier = this->getCarrier();
171        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
[7127]172
[6782]173        if(pawn == NULL)
174        {
175            COUT(1) << "Invalid PickupCarrier in DronePickup." << std::endl;
176        }
[7127]177
[6782]178        return pawn;
179    }
[7127]180
[6782]181    /**
182    @brief
183        Creates a duplicate of the input OrxonoxClass.
184    @param item
185        A pointer to the Orxonox class.
186    */
187    void DronePickup::clone(OrxonoxClass*& item)
188    {
189        if(item == NULL)
190            item = new DronePickup(this);
[7127]191
[6782]192        SUPER(DronePickup, clone, item);
[7127]193
[6782]194        DronePickup* pickup = dynamic_cast<DronePickup*>(item);
195        pickup->setDroneTemplate(this->getDroneTemplate());
[7127]196
[6782]197        pickup->initializeIdentifier();
198    }
199}
Note: See TracBrowser for help on using the repository browser.