Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai/src/modules/pickup/items/DronePickup.cc @ 6847

Last change on this file since 6847 was 6847, checked in by gasserlu, 14 years ago

drone follows Owner

File size: 5.4 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->destroy();
134               
135                //Attach to pawn
136                Drone* drone = new Drone(this);
137                drone->addTemplate(this->getDroneTemplate());
138                pawn->attach(drone);
139                Controller* controller = drone->getController();
140                DroneController* droneController = dynamic_cast<DroneController*>(controller);
141                if(droneController != NULL)
142                {
143                    droneController->setOwner(pawn);
144                }
145
146                //! The pickup has been used up.
147                this->setUsed(false);
148        }
149        else
150        {
151            //! If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused.
152            if(this->isOnce() || (this->isContinuous() ))
153            {
154                this->destroy();
155            }
156        }
157    }
158   
159    /**
160    @brief
161        Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails.
162    @return
163        A pointer to the Pawn, or NULL if the conversion failed.
164    */
165    Pawn* DronePickup::carrierToPawnHelper(void)
166    {
167        PickupCarrier* carrier = this->getCarrier();
168        Pawn* pawn = dynamic_cast<Pawn*>(carrier);
169       
170        if(pawn == NULL)
171        {
172            COUT(1) << "Invalid PickupCarrier in DronePickup." << std::endl;
173        }
174       
175        return pawn;
176    }
177   
178    /**
179    @brief
180        Creates a duplicate of the input OrxonoxClass.
181    @param item
182        A pointer to the Orxonox class.
183    */
184    void DronePickup::clone(OrxonoxClass*& item)
185    {
186        if(item == NULL)
187            item = new DronePickup(this);
188       
189        SUPER(DronePickup, clone, item);
190       
191        DronePickup* pickup = dynamic_cast<DronePickup*>(item);
192        pickup->setDroneTemplate(this->getDroneTemplate());
193       
194        pickup->initializeIdentifier();
195    }
196}
Note: See TracBrowser for help on using the repository browser.