Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7721 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
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
[7541]36#include <sstream>
[6782]37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39
[7541]40#include "controllers/DroneController.h"
41#include "pickup/PickupIdentifier.h"
[7547]42#include "worldentities/Drone.h"
[6782]43#include "worldentities/pawns/Pawn.h"
44
45namespace orxonox
46{
47
48    CreateFactory(DronePickup);
[7127]49
[6782]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);
[7127]57
[6782]58        this->initialize();
59    }
[7127]60
[6782]61    /**
62    @brief
63        Destructor.
64    */
65    DronePickup::~DronePickup()
66    {
[7127]67
[6782]68    }
[7127]69
[6782]70    /**
[7127]71    @brief
[6782]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    }
[7127]80
[6782]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    }
[7127]91
[6782]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);
[7127]100
[6782]101        this->initializeIdentifier();
102    }
[7127]103
[7541]104    /**
105    @brief
106        Set the droneTemplate.
107    @param templatename
108        The name of the Template to e set.
109    */
[6782]110    void DronePickup::setDroneTemplate(std::string templatename){
111        droneTemplate_ = templatename;
[7127]112    }
113
[7541]114    /**
115    @brief
116        Get the name of the droneTemplate.
117    @return
118        Returns the name of the droneTemplate.
119    */
[6782]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);
[7127]132
[7541]133        // If the pickup has transited to used.
[6782]134        if(this->isUsed())
135        {
136
137                Pawn* pawn = this->carrierToPawnHelper();
[7541]138                if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
[7162]139                    this->Pickupable::destroy();
[7127]140
[6782]141                //Attach to pawn
[6891]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)
[6782]143                drone->addTemplate(this->getDroneTemplate());
[6891]144
[6782]145                Controller* controller = drone->getController();
146                DroneController* droneController = dynamic_cast<DroneController*>(controller);
147                if(droneController != NULL)
148                {
[6847]149                    droneController->setOwner(pawn);
[6782]150                }
[7127]151
[6891]152                Vector3 spawnPosition = pawn->getWorldPosition() + Vector3(30,0,-30);
153                drone->setPosition(spawnPosition);
[7127]154
[7541]155                // The pickup has been used up.
[6782]156                this->setUsed(false);
157        }
158        else
159        {
[7541]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.
[6782]161            if(this->isOnce() || (this->isContinuous() ))
162            {
[7162]163                this->Pickupable::destroy();
[6782]164            }
165        }
166    }
[7127]167
[6782]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);
[7127]178
[6782]179        if(pawn == NULL)
180        {
181            COUT(1) << "Invalid PickupCarrier in DronePickup." << std::endl;
182        }
[7127]183
[6782]184        return pawn;
185    }
[7127]186
[6782]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);
[7127]197
[6782]198        SUPER(DronePickup, clone, item);
[7127]199
[6782]200        DronePickup* pickup = dynamic_cast<DronePickup*>(item);
201        pickup->setDroneTemplate(this->getDroneTemplate());
[7127]202
[6782]203        pickup->initializeIdentifier();
204    }
205}
Note: See TracBrowser for help on using the repository browser.