Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/fabienHS15/src/modules/overlays/hud/HUDWeaponMode.cc @ 10708

Last change on this file since 10708 was 10688, checked in by fvultier, 9 years ago

There is now a HUD that shows the status of the weapon system: all weapons, weapon modes and their munition. Progress bars show the progress of replenishing munition.

File size: 9.3 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 *      Yuning Chai
24 *      Felix Schulthess
25 *   Co-authors:
26 *      Reto Grieder
27 *      Wolfgang Roenninger
28 *
29 */
30
31#include "HUDWeaponMode.h"
32
33#include <OgreOverlayManager.h>
34#include <OgrePanelOverlayElement.h>
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "tools/TextureGenerator.h"
39#include "weaponsystem/WeaponMode.h"
40#include "weaponsystem/Munition.h"
41#include "util/Convert.h"
42
43namespace orxonox
44{
45    RegisterClass(HUDWeaponMode);
46
47    HUDWeaponMode::HUDWeaponMode(Context* context) : OrxonoxOverlay(context)
48    {
49        RegisterObject(HUDWeaponMode);
50
51        overlayElementIcon_ = static_cast<Ogre::PanelOverlayElement* >(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDWeaponMode" + getUniqueNumberString()));
52        overlayElementIcon_->setPosition(0.0f,0.0f);
53        overlayElementIcon_->setDimensions(1.0f,1.0f);
54        this->background_->addChild(overlayElementIcon_);
55
56        overlayElementReplenish_ = static_cast<Ogre::PanelOverlayElement* >(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDWeaponMode" + getUniqueNumberString()));
57        overlayElementReplenish_->setPosition(0.0f,0.0f);
58        overlayElementReplenish_->setDimensions(1.0f,1.0f);
59        this->background_->addChild(overlayElementReplenish_);
60
61        overlayElementMunition_ = static_cast<Ogre::PanelOverlayElement* >(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDWeaponMode" + getUniqueNumberString()));
62        overlayElementMunition_->setPosition(0.0f,0.0f);
63        overlayElementMunition_->setDimensions(1.0f,1.0f);
64        this->background_->addChild(overlayElementMunition_);
65
66        overlayElementState_ = static_cast<Ogre::PanelOverlayElement* >(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDWeaponMode" + getUniqueNumberString()));
67        overlayElementState_->setPosition(0.0f,0.0f);
68        overlayElementState_->setDimensions(1.0f,1.0f);
69        this->background_->addChild(overlayElementState_);
70
71        overlayElementIcon_->show();
72        overlayElementReplenish_->show();
73        overlayElementMunition_->show();
74        overlayElementState_->show();
75
76        // Create two text overlays
77        this->textOverlayLeft_ = new OverlayText(this->getContext());
78        assert(this->textOverlayLeft_.get());
79        this->textOverlayLeft_->setCaption("???");
80        textOverlayLeft_->setPickPoint(Vector2(0.0f,0.0f));
81        textOverlayLeft_->setVisible(true);
82        textOverlayLeft_->setAlignment(OverlayText::Left);
83        textOverlayLeft_->setTextSize(0.02f);
84        textOverlayLeft_->setColour(ColourValue(0.21,0.70,0.21,1.0));
85        textOverlayLeft_->setPosition(getPosition());
86        textOverlayLeft_->setAspectCorrection(this->getAspectCorrection());
87       
88        this->textOverlayRight_ = new OverlayText(this->getContext());
89        assert(this->textOverlayLeft_.get());
90        this->textOverlayRight_->setCaption("???");
91        textOverlayRight_->setPickPoint(Vector2(0.0f,0.0f));
92        textOverlayRight_->setVisible(true);
93        textOverlayRight_->setAlignment(OverlayText::Left);
94        textOverlayRight_->setTextSize(0.02f);
95        textOverlayRight_->setColour(ColourValue(0.21,0.70,0.21,1.0));
96        textOverlayRight_->setPosition(getPosition());
97        textOverlayRight_->setAspectCorrection(this->getAspectCorrection());       
98
99        materialNameState_ = "Orxonox/WSHUD_Reloading";
100
101        overlayElementReplenish_->setMaterialName("Orxonox/WSHUD_Replenish");
102    }
103
104    HUDWeaponMode::~HUDWeaponMode()
105    {
106        if (this->isInitialized())
107        {
108            //this->textOverlay_->destroy();           
109        }
110    }
111
112    void HUDWeaponMode::tick(float dt)
113    {
114        SUPER(HUDWeaponMode, tick, dt);
115
116        if (this->owner_ && this->weaponMode_)
117        {
118            std::string lastMaterialNameState = materialNameState_;
119
120            if (weaponMode_->getReloading())
121            {
122                materialNameState_ = "Orxonox/WSHUD_Reloading";
123            }
124            else
125            {
126                materialNameState_ = "Orxonox/WSHUD_Ready";
127            }           
128
129            Munition* munition = this->weaponMode_->getMunition();
130
131            if (munition != NULL)
132            {
133                bool useSeparateMagazines = munition->getUseSeparateMagazines();
134                bool stackMunition = munition->getStackMunition();
135
136                if (!useSeparateMagazines && !stackMunition)
137                {
138                    this->textOverlayLeft_->setCaption(multi_cast<std::string>(munition->getNumMunitionInCurrentMagazine(weaponMode_)));
139                    this->textOverlayRight_->setCaption(multi_cast<std::string>(munition->getNumMagazines()));
140                }
141                else if (!useSeparateMagazines && stackMunition)
142                {
143                    this->textOverlayLeft_->setCaption(multi_cast<std::string>(munition->getNumMunition(weaponMode_)));
144                    this->textOverlayRight_->setCaption("");
145                }
146                else if (useSeparateMagazines && !stackMunition)
147                {
148                    this->textOverlayLeft_->setCaption(multi_cast<std::string>(munition->getNumMunitionInCurrentMagazine(weaponMode_)));
149                    this->textOverlayRight_->setCaption(multi_cast<std::string>(munition->getNumMagazines()));
150                }
151
152                if (munition->getNumMunition(weaponMode_) == 0)
153                {
154                    materialNameState_ = "Orxonox/WSHUD_Empty";
155                }
156            }
157
158            if (munition->isA(Class(ReplenishingMunition)))
159            {
160                ReplenishingMunition* replenishingMunition = dynamic_cast<ReplenishingMunition*>(munition);
161
162                if (replenishingMunition->canAddMunition(1))
163                {
164                    float progress = 1.0f - replenishingMunition->getProgress();
165                    overlayElementReplenish_->setDimensions(1.0f,progress);                   
166                }
167                else
168                {
169                    overlayElementReplenish_->setDimensions(1.0f,1.0f);
170                }
171            }
172
173            if (materialNameState_ != lastMaterialNameState)
174            {
175                overlayElementState_->setMaterialName(materialNameState_);
176            }
177        }
178    }   
179
180    void HUDWeaponMode::positionChanged()
181    {
182        OrxonoxOverlay::positionChanged();
183
184        positionHUDChilds();
185    }   
186
187    void HUDWeaponMode::changedOwner()
188    {
189        SUPER(HUDWeaponMode, changedOwner);
190
191        this->owner_ = orxonox_cast<Pawn*>(this->getOwner());
192
193        //updateWeaponsList();
194    }
195
196    void HUDWeaponMode::changedOverlayGroup()
197    {
198        SUPER(HUDWeaponMode, changedOverlayGroup);
199
200        this->getOverlayGroup()->addElement(this->textOverlayLeft_.get());
201        this->getOverlayGroup()->addElement(this->textOverlayRight_.get());
202    }   
203
204    void HUDWeaponMode::changedVisibility()
205    {
206        SUPER(HUDWeaponMode, changedVisibility);
207
208        //this->textOverlay_->setVisible(this->isVisible());
209    }
210
211    void HUDWeaponMode::changedName()
212    {
213        SUPER(HUDWeaponMode, changedName);
214
215        //this->textOverlay_->setName(this->getName() + "text");
216    } 
217
218    void HUDWeaponMode::setWeaponMode(WeaponMode* weaponMode)
219    {
220        weaponMode_ = weaponMode;
221
222        if (!weaponMode_)
223        {
224            return;
225        }
226
227        std::string materialName = weaponMode_->getHUDImageString();
228
229        overlayElementIcon_->setMaterialName(materialName);
230
231        Munition* munition = this->weaponMode_->getMunition();
232
233        if (munition != NULL)
234        {
235            bool useSeparateMagazines = munition->getUseSeparateMagazines();
236            bool stackMunition = munition->getStackMunition();
237
238            if (!useSeparateMagazines && !stackMunition)
239            {
240                overlayElementMunition_->setMaterialName("Orxonox/WSHUD_MunitionShare");
241            }
242            else if (!useSeparateMagazines && stackMunition)
243            {
244                overlayElementMunition_->setMaterialName("Orxonox/WSHUD_MunitionStack");
245            }
246            else if (useSeparateMagazines && !stackMunition)
247            {
248                overlayElementMunition_->setMaterialName("Orxonox/WSHUD_MunitionSeparate");
249            }
250        }     
251    }
252
253    void HUDWeaponMode::positionHUDChilds()
254    {
255        textOverlayLeft_->setPosition(getPosition() + Vector2(0.06f,0.0f));
256        textOverlayRight_->setPosition(getPosition() + Vector2(0.105f,0.0f));
257    } 
258}
Note: See TracBrowser for help on using the repository browser.