Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/PickupRepresentation.cc @ 7547

Last change on this file since 7547 was 7540, checked in by dafrick, 14 years ago

Some more documenting.

  • Property svn:eol-style set to native
File size: 7.3 KB
RevLine 
[6474]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 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27*/
28
[6540]29/**
30    @file PickupRepresentation.cc
31    @brief Implementation of the PickupRepresentation class.
32*/
33
[6474]34#include "PickupRepresentation.h"
35
36#include "core/CoreIncludes.h"
[7504]37#include "core/GameMode.h"
[7540]38#include "util/StringUtils.h"
39
[6475]40#include "graphics/Billboard.h"
[7540]41
[6474]42#include "PickupManager.h"
43
44namespace orxonox
45{
[7163]46
[6474]47    CreateFactory(PickupRepresentation);
[7163]48
[6475]49    /**
50    @brief
51        Constructor. Registers the object and initializes its member variables.
52        This is primarily for use of the PickupManager in creating a default PickupRepresentation.
53    */
[7504]54    PickupRepresentation::PickupRepresentation() : BaseObject(NULL), Synchronisable(NULL), spawnerRepresentation_(NULL), pickup_(NULL)
[6474]55    {
56        RegisterObject(PickupRepresentation);
[7163]57
[6474]58        this->initialize();
[7540]59        this->setSyncMode(0x0); // The default PickupRperesentation created by each PickupManager is not synchronised, since it only exists locally.
[6474]60    }
[7163]61
[6475]62    /**
63    @brief
64        Default Constructor. Registers the object and initializes its member variables.
65    */
[7504]66    PickupRepresentation::PickupRepresentation(BaseObject* creator) : BaseObject(creator), Synchronisable(creator), spawnerRepresentation_(NULL), pickup_(NULL)
[6474]67    {
68        RegisterObject(PickupRepresentation);
[7163]69
[6474]70        this->initialize();
[7504]71        this->registerVariables();
72
[7540]73        PickupManager::getInstance().registerRepresentation(this); // Registers the PickupRepresentation with the PickupManager.
[6474]74    }
[7163]75
[6475]76    /**
77    @brief
78        Destructor.
79    */
[6474]80    PickupRepresentation::~PickupRepresentation()
81    {
[6475]82        if(this->spawnerRepresentation_ != NULL)
83            this->spawnerRepresentation_->destroy();
[7163]84
[7504]85        if(this->isInitialized())
86        {
[7533]87            //TODO: Also (network) unregister for master.
[7504]88            if(GameMode::isMaster() && this->pickup_ != NULL)
89            {
90                PickupManager::getInstance().unregisterRepresentation(this->pickup_->getPickupIdentifier(), this);
91            }
92            if(!GameMode::isMaster())
93            {
94                PickupManager::getInstance().unregisterRepresentation(this);
95            }
96        }
[6474]97    }
[7163]98
[6475]99    /**
100    @brief
101        Initializes the member variables of this PickupRepresentation.
102    */
[6474]103    void PickupRepresentation::initialize(void)
104    {
105        this->description_ = "This is a pickup.";
106        this->name_ = "Pickup";
107        this->spawnerTemplate_ = "";
[6711]108        this->inventoryRepresentation_ = "Default";
[6474]109    }
[7163]110
[7540]111    /**
112    @brief
113        Registers the variables that need to be synchornised.
114    */
[7504]115    void PickupRepresentation::registerVariables(void)
116    {
117        registerVariable(this->description_, VariableDirection::ToClient);
118        registerVariable(this->name_, VariableDirection::ToClient);
119        registerVariable(this->inventoryRepresentation_, VariableDirection::ToClient);
120    }
121
[6475]122    /**
123    @brief
124        Method for creating a PickupRepresentation object through XML.
125    */
[6474]126    void PickupRepresentation::XMLPort(Element& xmlelement, XMLPort::Mode mode)
127    {
128        SUPER(PickupRepresentation, XMLPort, xmlelement, mode);
[7163]129
[6711]130        XMLPortParam(PickupRepresentation, "pickupName", setPickupName, getPickupName, xmlelement, mode);
131        XMLPortParam(PickupRepresentation, "pickupDescription", setPickupDescription, getPickupDescription, xmlelement, mode);
[6474]132        XMLPortParam(PickupRepresentation, "spawnerTemplate", setSpawnerTemplate, getSpawnerTemplate, xmlelement, mode);
[6711]133        XMLPortParam(PickupRepresentation, "inventoryRepresentation", setInventoryRepresentation, getInventoryRepresentation, xmlelement, mode);
[6474]134        XMLPortObject(PickupRepresentation, Pickupable, "pickup", setPickup, getPickup, xmlelement, mode);
135        XMLPortObject(PickupRepresentation, StaticEntity, "spawner-representation", setSpawnerRepresentation, getSpawnerRepresentationIndex, xmlelement, mode);
[7163]136
[7504]137        if(GameMode::isMaster())
138        {
[7540]139            // Registers the PickupRepresentation with the PickupManager through the PickupIdentifier of the Pickupable it represents.
140            PickupManager::getInstance().registerRepresentation(this->pickup_->getPickupIdentifier(), this);
[7504]141        }
[7163]142
[6484]143        if(this->spawnerRepresentation_ != NULL)
144            this->spawnerRepresentation_->setVisible(false);
[7163]145
[6484]146        COUT(4) << "PickupRepresentation created: name: '" << this->name_ << "', description: '" << this->description_ << "', spawnerTemplate: '" << this->spawnerTemplate_ << "'." << std::endl;
[6474]147    }
[7163]148
[6475]149    /**
150    @brief
151        Get a spawnerRepresentation for a specific PickupSpawner.
152    @param spawner
153        A pointer to the PickupSpawner.
154    @return
155        Returns a pointer to the StaticEntity.
156    */
[6474]157    StaticEntity* PickupRepresentation::getSpawnerRepresentation(PickupSpawner* spawner)
158    {
159        if(this->spawnerRepresentation_ == NULL)
160        {
161            COUT(4) << "PickupRepresentation: No spawner representation found." << std::endl;
[6475]162            if(this->spawnerTemplate_ == BLANKSTRING)
[6474]163            {
164                COUT(4) << "PickupRepresentation: Spawner template is empty." << std::endl;
[7540]165                // If neither spawnerRepresentation nor spawnerTemplate was specified
[6474]166                return this->getDefaultSpawnerRepresentation(spawner);
167            }
168            this->addTemplate(this->spawnerTemplate_);
169        }
[7163]170
[6474]171        StaticEntity* representation = this->spawnerRepresentation_;
[6484]172        representation->setVisible(true);
[7163]173
[6474]174        this->addTemplate(this->spawnerTemplate_);
[6484]175        this->spawnerRepresentation_->setVisible(false);
[7163]176
[6474]177        return representation;
178    }
[7163]179
[6475]180    /**
181    @brief
182        Get the default spawnerRepresentation for a specific PickupSpawner.
183        Helper method of internal use.
184    @param spawner
185        A pointer to the PickupSpawner.
186    @return
187        Returns a pointer to the StaticEntity.
188    */
[7540]189    //TODO: Possibility to define default representation through XML?
[6474]190    StaticEntity* PickupRepresentation::getDefaultSpawnerRepresentation(PickupSpawner* spawner)
191    {
192        StaticEntity* representation = new StaticEntity(spawner);
[6512]193        Billboard* sphere = new Billboard(spawner);
[6676]194        sphere->setColour(ColourValue(0.95f, 0.85f, 0.27f));
[6512]195        sphere->setMaterial("Sphere2");
[6676]196        sphere->setScale(0.1f);
[6512]197        Billboard* icon = new Billboard(spawner);
[6676]198        icon->setColour(ColourValue(0.89f, 0.79f, 0.08f));
[6512]199        icon->setMaterial("Asterix");
200        icon->setScale(0.5);
201        sphere->attach(icon);
202        representation->attach(sphere);
[6474]203        return representation;
204    }
[7163]205
[6474]206}
Note: See TracBrowser for help on using the repository browser.