Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7527 was 7504, checked in by dafrick, 14 years ago

Pickups module is now (from what I can tell after some basic testing) fully functional over the network.
However it's still a little messy, needs some cleanup and documentation.
I introduced a new class, the PickupListener, which allows reacting to pickups becoming used, unused, picked up or dropped.

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