Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2012merge/src/modules/pickup/items/DronePickup.cc @ 9305

Last change on this file since 9305 was 9305, checked in by landauf, 12 years ago

simplified code a little by using MultiType instead of explicit conversion

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