Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mac_osx/src/modules/pickup/PickupRepresentation.cc @ 8318

Last change on this file since 8318 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
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 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27*/
28
29/**
30    @file PickupRepresentation.cc
31    @brief Implementation of the PickupRepresentation class.
32*/
33
34#include "PickupRepresentation.h"
35
36#include "core/CoreIncludes.h"
37#include "core/GameMode.h"
38#include "graphics/Billboard.h"
39#include "util/StringUtils.h"
40#include "PickupManager.h"
41
42namespace orxonox
43{
44
45    CreateFactory(PickupRepresentation);
46
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    */
52    PickupRepresentation::PickupRepresentation() : BaseObject(NULL), Synchronisable(NULL), spawnerRepresentation_(NULL), pickup_(NULL)
53    {
54        RegisterObject(PickupRepresentation);
55
56        this->initialize();
57        this->setSyncMode(0x0);
58    }
59
60    /**
61    @brief
62        Default Constructor. Registers the object and initializes its member variables.
63    */
64    PickupRepresentation::PickupRepresentation(BaseObject* creator) : BaseObject(creator), Synchronisable(creator), spawnerRepresentation_(NULL), pickup_(NULL)
65    {
66        RegisterObject(PickupRepresentation);
67
68        this->initialize();
69        this->registerVariables();
70
71        PickupManager::getInstance().registerRepresentation(this); //!< Registers the PickupRepresentation with the PickupManager.
72    }
73
74    /**
75    @brief
76        Destructor.
77    */
78    PickupRepresentation::~PickupRepresentation()
79    {
80        if(this->spawnerRepresentation_ != NULL)
81            this->spawnerRepresentation_->destroy();
82
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        }
94    }
95
96    /**
97    @brief
98        Initializes the member variables of this PickupRepresentation.
99    */
100    void PickupRepresentation::initialize(void)
101    {
102        this->description_ = "This is a pickup.";
103        this->name_ = "Pickup";
104        this->spawnerTemplate_ = "";
105        this->inventoryRepresentation_ = "Default";
106    }
107
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
115    /**
116    @brief
117        Method for creating a PickupRepresentation object through XML.
118    */
119    void PickupRepresentation::XMLPort(Element& xmlelement, XMLPort::Mode mode)
120    {
121        SUPER(PickupRepresentation, XMLPort, xmlelement, mode);
122
123        XMLPortParam(PickupRepresentation, "pickupName", setPickupName, getPickupName, xmlelement, mode);
124        XMLPortParam(PickupRepresentation, "pickupDescription", setPickupDescription, getPickupDescription, xmlelement, mode);
125        XMLPortParam(PickupRepresentation, "spawnerTemplate", setSpawnerTemplate, getSpawnerTemplate, xmlelement, mode);
126        XMLPortParam(PickupRepresentation, "inventoryRepresentation", setInventoryRepresentation, getInventoryRepresentation, xmlelement, mode);
127        XMLPortObject(PickupRepresentation, Pickupable, "pickup", setPickup, getPickup, xmlelement, mode);
128        XMLPortObject(PickupRepresentation, StaticEntity, "spawner-representation", setSpawnerRepresentation, getSpawnerRepresentationIndex, xmlelement, mode);
129
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        }
134
135        if(this->spawnerRepresentation_ != NULL)
136            this->spawnerRepresentation_->setVisible(false);
137
138        COUT(4) << "PickupRepresentation created: name: '" << this->name_ << "', description: '" << this->description_ << "', spawnerTemplate: '" << this->spawnerTemplate_ << "'." << std::endl;
139    }
140
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    */
149    StaticEntity* PickupRepresentation::getSpawnerRepresentation(PickupSpawner* spawner)
150    {
151        if(this->spawnerRepresentation_ == NULL)
152        {
153            COUT(4) << "PickupRepresentation: No spawner representation found." << std::endl;
154            if(this->spawnerTemplate_ == BLANKSTRING)
155            {
156                COUT(4) << "PickupRepresentation: Spawner template is empty." << std::endl;
157                //!< If neither spawnerRepresentation nor spawnerTemplate was specified
158                return this->getDefaultSpawnerRepresentation(spawner);
159            }
160            this->addTemplate(this->spawnerTemplate_);
161        }
162
163        StaticEntity* representation = this->spawnerRepresentation_;
164        representation->setVisible(true);
165
166        this->addTemplate(this->spawnerTemplate_);
167        this->spawnerRepresentation_->setVisible(false);
168
169        return representation;
170    }
171
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    */
181    //TODO: Possibility to define default representation through XML.
182    StaticEntity* PickupRepresentation::getDefaultSpawnerRepresentation(PickupSpawner* spawner)
183    {
184        StaticEntity* representation = new StaticEntity(spawner);
185        Billboard* sphere = new Billboard(spawner);
186        sphere->setColour(ColourValue(0.95f, 0.85f, 0.27f));
187        sphere->setMaterial("Sphere2");
188        sphere->setScale(0.1f);
189        Billboard* icon = new Billboard(spawner);
190        icon->setColour(ColourValue(0.89f, 0.79f, 0.08f));
191        icon->setMaterial("Asterix");
192        icon->setScale(0.5);
193        sphere->attach(icon);
194        representation->attach(sphere);
195        return representation;
196    }
197
198}
Note: See TracBrowser for help on using the repository browser.