Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mac_osx/src/modules/pickup/items/DronePickup.cc @ 8466

Last change on this file since 8466 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
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 *      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);
51
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);
59
60        this->initialize();
61    }
62
63    /**
64    @brief
65        Destructor.
66    */
67    DronePickup::~DronePickup()
68    {
69
70    }
71
72    /**
73    @brief
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    }
82
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    }
93
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);
102
103        this->initializeIdentifier();
104    }
105
106    void DronePickup::setDroneTemplate(std::string templatename){
107        droneTemplate_ = templatename;
108    }
109
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);
122
123        //! If the pickup is not picked up nothing must be done.
124        if(!this->isPickedUp())
125            return;
126
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.
133                    this->Pickupable::destroy();
134
135                //Attach to pawn
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)
137                drone->addTemplate(this->getDroneTemplate());
138
139                Controller* controller = drone->getController();
140                DroneController* droneController = dynamic_cast<DroneController*>(controller);
141                if(droneController != NULL)
142                {
143                    droneController->setOwner(pawn);
144                }
145
146                Vector3 spawnPosition = pawn->getWorldPosition() + Vector3(30,0,-30);
147                drone->setPosition(spawnPosition);
148
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            {
157                this->Pickupable::destroy();
158            }
159        }
160    }
161
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);
172
173        if(pawn == NULL)
174        {
175            COUT(1) << "Invalid PickupCarrier in DronePickup." << std::endl;
176        }
177
178        return pawn;
179    }
180
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);
191
192        SUPER(DronePickup, clone, item);
193
194        DronePickup* pickup = dynamic_cast<DronePickup*>(item);
195        pickup->setDroneTemplate(this->getDroneTemplate());
196
197        pickup->initializeIdentifier();
198    }
199}
Note: See TracBrowser for help on using the repository browser.