Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7535 was 7533, checked in by dafrick, 14 years ago

Documenting in pickups module.
Cleaning up in PickupManager.
Removed some obsolete functions in HumanController and ControllableEntity, which were remenants of the old pickups module.
Fixed a bug.

  • 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            //TODO: Also (network) unregister for master.
86            if(GameMode::isMaster() && this->pickup_ != NULL)
87            {
88                PickupManager::getInstance().unregisterRepresentation(this->pickup_->getPickupIdentifier(), this);
89            }
90            if(!GameMode::isMaster())
91            {
92                PickupManager::getInstance().unregisterRepresentation(this);
93            }
94        }
95    }
96
97    /**
98    @brief
99        Initializes the member variables of this PickupRepresentation.
100    */
101    void PickupRepresentation::initialize(void)
102    {
103        this->description_ = "This is a pickup.";
104        this->name_ = "Pickup";
105        this->spawnerTemplate_ = "";
106        this->inventoryRepresentation_ = "Default";
107    }
108
109    void PickupRepresentation::registerVariables(void)
110    {
111        registerVariable(this->description_, VariableDirection::ToClient);
112        registerVariable(this->name_, VariableDirection::ToClient);
113        registerVariable(this->inventoryRepresentation_, VariableDirection::ToClient);
114    }
115
116    /**
117    @brief
118        Method for creating a PickupRepresentation object through XML.
119    */
120    void PickupRepresentation::XMLPort(Element& xmlelement, XMLPort::Mode mode)
121    {
122        SUPER(PickupRepresentation, XMLPort, xmlelement, mode);
123
124        XMLPortParam(PickupRepresentation, "pickupName", setPickupName, getPickupName, xmlelement, mode);
125        XMLPortParam(PickupRepresentation, "pickupDescription", setPickupDescription, getPickupDescription, xmlelement, mode);
126        XMLPortParam(PickupRepresentation, "spawnerTemplate", setSpawnerTemplate, getSpawnerTemplate, xmlelement, mode);
127        XMLPortParam(PickupRepresentation, "inventoryRepresentation", setInventoryRepresentation, getInventoryRepresentation, xmlelement, mode);
128        XMLPortObject(PickupRepresentation, Pickupable, "pickup", setPickup, getPickup, xmlelement, mode);
129        XMLPortObject(PickupRepresentation, StaticEntity, "spawner-representation", setSpawnerRepresentation, getSpawnerRepresentationIndex, xmlelement, mode);
130
131        if(GameMode::isMaster())
132        {
133            PickupManager::getInstance().registerRepresentation(this->pickup_->getPickupIdentifier(), this); //!< Registers the PickupRepresentation with the PickupManager through the PickupIdentifier of the Pickupable it represents.
134        }
135
136        if(this->spawnerRepresentation_ != NULL)
137            this->spawnerRepresentation_->setVisible(false);
138
139        COUT(4) << "PickupRepresentation created: name: '" << this->name_ << "', description: '" << this->description_ << "', spawnerTemplate: '" << this->spawnerTemplate_ << "'." << std::endl;
140    }
141
142    /**
143    @brief
144        Get a spawnerRepresentation for a specific PickupSpawner.
145    @param spawner
146        A pointer to the PickupSpawner.
147    @return
148        Returns a pointer to the StaticEntity.
149    */
150    StaticEntity* PickupRepresentation::getSpawnerRepresentation(PickupSpawner* spawner)
151    {
152        if(this->spawnerRepresentation_ == NULL)
153        {
154            COUT(4) << "PickupRepresentation: No spawner representation found." << std::endl;
155            if(this->spawnerTemplate_ == BLANKSTRING)
156            {
157                COUT(4) << "PickupRepresentation: Spawner template is empty." << std::endl;
158                //!< If neither spawnerRepresentation nor spawnerTemplate was specified
159                return this->getDefaultSpawnerRepresentation(spawner);
160            }
161            this->addTemplate(this->spawnerTemplate_);
162        }
163
164        StaticEntity* representation = this->spawnerRepresentation_;
165        representation->setVisible(true);
166
167        this->addTemplate(this->spawnerTemplate_);
168        this->spawnerRepresentation_->setVisible(false);
169
170        return representation;
171    }
172
173    /**
174    @brief
175        Get the default spawnerRepresentation for a specific PickupSpawner.
176        Helper method of internal use.
177    @param spawner
178        A pointer to the PickupSpawner.
179    @return
180        Returns a pointer to the StaticEntity.
181    */
182    //TODO: Possibility to define default representation through XML.
183    StaticEntity* PickupRepresentation::getDefaultSpawnerRepresentation(PickupSpawner* spawner)
184    {
185        StaticEntity* representation = new StaticEntity(spawner);
186        Billboard* sphere = new Billboard(spawner);
187        sphere->setColour(ColourValue(0.95f, 0.85f, 0.27f));
188        sphere->setMaterial("Sphere2");
189        sphere->setScale(0.1f);
190        Billboard* icon = new Billboard(spawner);
191        icon->setColour(ColourValue(0.89f, 0.79f, 0.08f));
192        icon->setMaterial("Asterix");
193        icon->setScale(0.5);
194        sphere->attach(icon);
195        representation->attach(sphere);
196        return representation;
197    }
198
199}
Note: See TracBrowser for help on using the repository browser.