Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7546 was 7541, checked in by dafrick, 14 years ago

Some more documentation.

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