Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/environment2/src/modules/pickup/items/DronePickup.cc @ 11183

Last change on this file since 11183 was 7548, checked in by dafrick, 14 years ago

Resolving some TODO's.

  • 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        std::string val = this->getDroneTemplate();
88        std::string type = "droneTemplate";
89        this->pickupIdentifier_->addParameter(type, val);
90    }
91
92    /**
93    @brief
94        Method for creating a DronePickup object through XML.
95    */
96    void DronePickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode)
97    {
98        SUPER(DronePickup, XMLPort, xmlelement, mode);
99        XMLPortParam(DronePickup, "droneTemplate", setDroneTemplate, getDroneTemplate, xmlelement, mode);
100
101        this->initializeIdentifier();
102    }
103
104    /**
105    @brief
106        Set the droneTemplate.
107    @param templatename
108        The name of the Template to e set.
109    */
110    void DronePickup::setDroneTemplate(std::string templatename){
111        droneTemplate_ = templatename;
112    }
113
114    /**
115    @brief
116        Get the name of the droneTemplate.
117    @return
118        Returns the name of the droneTemplate.
119    */
120    const std::string& DronePickup::getDroneTemplate() const
121    {
122        return droneTemplate_;
123    }
124
125    /**
126    @brief
127        Is called when the pickup has transited from used to unused or the other way around.
128    */
129    void DronePickup::changedUsed(void)
130    {
131        SUPER(DronePickup, changedUsed);
132
133        // If the pickup has transited to used.
134        if(this->isUsed())
135        {
136
137                Pawn* pawn = this->carrierToPawnHelper();
138                if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
139                    this->Pickupable::destroy();
140
141                //Attach to pawn
142                Drone* drone = new Drone(pawn->getCreator()); // this is neccessary because the projectiles fired need a valid creator for the particlespawner (when colliding against something)
143                drone->addTemplate(this->getDroneTemplate());
144
145                Controller* controller = drone->getController();
146                DroneController* droneController = dynamic_cast<DroneController*>(controller);
147                if(droneController != NULL)
148                {
149                    droneController->setOwner(pawn);
150                }
151
152                Vector3 spawnPosition = pawn->getWorldPosition() + Vector3(30,0,-30);
153                drone->setPosition(spawnPosition);
154
155                // The pickup has been used up.
156                this->setUsed(false);
157        }
158        else
159        {
160            // If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused.
161            if(this->isOnce() || (this->isContinuous() ))
162            {
163                this->Pickupable::destroy();
164            }
165        }
166    }
167
168    /**
169    @brief
170        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
171    @return
172        A pointer to the Pawn, or NULL if the conversion failed.
173    */
174    Pawn* DronePickup::carrierToPawnHelper(void)
175    {
176        PickupCarrier* carrier = this->getCarrier();
177        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
178
179        if(pawn == NULL)
180        {
181            COUT(1) << "Invalid PickupCarrier in DronePickup." << std::endl;
182        }
183
184        return pawn;
185    }
186
187    /**
188    @brief
189        Creates a duplicate of the input OrxonoxClass.
190    @param item
191        A pointer to the Orxonox class.
192    */
193    void DronePickup::clone(OrxonoxClass*& item)
194    {
195        if(item == NULL)
196            item = new DronePickup(this);
197
198        SUPER(DronePickup, clone, item);
199
200        DronePickup* pickup = dynamic_cast<DronePickup*>(item);
201        pickup->setDroneTemplate(this->getDroneTemplate());
202
203        pickup->initializeIdentifier();
204    }
205}
Note: See TracBrowser for help on using the repository browser.