From 9d133392bc9af56e62240c2b724c8dacbb2fa66a Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 6 Jul 2013 13:38:15 +0200 Subject: [PATCH] Display weapon name on GUI. --- source/Game.cpp | 15 ++++++++++----- source/Game.h | 3 ++- source/abstract/Character.cpp | 5 +++++ source/abstract/Character.h | 1 + source/items/Weapon.cpp | 6 ++++++ source/items/Weapon.h | 2 ++ source/sprites/Player.h | 1 + 7 files changed, 27 insertions(+), 6 deletions(-) diff --git a/source/Game.cpp b/source/Game.cpp index fc60297..d610cf1 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -34,10 +34,9 @@ Game::Game(tgui::Window& window) : mWorld.insertCharacter(mPlayer); mAmmo = window.add(); - setAmmoText(); mAmmo->setTextSize(20); - mAmmo->setPosition(mWindow.getSize().x - mAmmo->getSize().x, - mWindow.getSize().y - mAmmo->getSize().y); + mCurrentWeapon = window.add(); + mCurrentWeapon->setTextSize(14); } /** @@ -63,7 +62,7 @@ Game::loop() { mWorld.step(elapsed); - setAmmoText(); + updateGui(); render(); @@ -75,7 +74,7 @@ Game::loop() { * Displays current player ammo in the ammo widget. */ void -Game::setAmmoText() { +Game::updateGui() { int mag = mPlayer->getMagazineAmmo(); int total = mPlayer->getTotalAmmo(); @@ -87,6 +86,12 @@ Game::setAmmoText() { if (total < 10) totalString = "0" + totalString; mAmmo->setText(magString + "/" + totalString); + mCurrentWeapon->setText(mPlayer->getWeaponName()); + + mAmmo->setPosition(mWindow.getSize().x - mAmmo->getSize().x, + mWindow.getSize().y - mAmmo->getSize().y); + mCurrentWeapon->setPosition(mWindow.getSize().x - mCurrentWeapon->getSize().x, + mAmmo->getPosition().y - mCurrentWeapon->getSize().y); } /** diff --git a/source/Game.h b/source/Game.h index f709ed6..794d259 100644 --- a/source/Game.h +++ b/source/Game.h @@ -37,7 +37,7 @@ private: void mouseUp(const sf::Event& event); sf::Vector2 convertCoordinates(int x, int y); - void setAmmoText(); + void updateGui(); private: static const int FPS_GOAL; @@ -46,6 +46,7 @@ private: sf::Clock mClock; sf::View mWorldView; tgui::Label* mAmmo; + tgui::Label* mCurrentWeapon; World mWorld; Pathfinder mPathfinder; diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index 32d42ef..c159e31 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -182,6 +182,11 @@ Character::getTotalAmmo() const { return mActiveWeapon->getTotalAmmo(); } +std::string +Character::getWeaponName() const { + return mActiveWeapon->getName(); +} + void Character::reload() { mActiveWeapon->reload(); diff --git a/source/abstract/Character.h b/source/abstract/Character.h index 9eb9c24..2f1c354 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -49,6 +49,7 @@ protected: std::vector > getCharacters() const; int getMagazineAmmo() const; int getTotalAmmo() const; + std::string getWeaponName() const; void reload(); void toggleWeapon(); void selectFirstWeapon(); diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index 9fd5a6f..3263890 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -16,6 +16,7 @@ Weapon::Weapon(World& world, Character& holder, const Yaml& config) : mWorld(world), mHolder(holder), + mName(config.get("name", std::string())), mProjectile(config.get("bullet", std::string("bullet.yaml"))), mDamage(config.get("damage", 0)), mProjectileSpeed(config.get("projectile_speed", 0.0f)), @@ -120,6 +121,11 @@ Weapon::getTotalAmmo() const { return mTotalAmmo; } +std::string +Weapon::getName() const { + return mName; +} + void Weapon::reload() { if (mMagazineAmmo == mMagazineSize) diff --git a/source/items/Weapon.h b/source/items/Weapon.h index bbf21b1..efc2739 100755 --- a/source/items/Weapon.h +++ b/source/items/Weapon.h @@ -31,6 +31,7 @@ public: void onThink(int elapsed); int getMagazineAmmo() const; int getTotalAmmo() const; + std::string getName() const; void reload(); void cancelReload(); @@ -43,6 +44,7 @@ private: Character& mHolder; thor::Timer mTimer; + const std::string mName; const Yaml mProjectile; const int mDamage; const float mProjectileSpeed; diff --git a/source/sprites/Player.h b/source/sprites/Player.h index 60722a7..efbb2c1 100644 --- a/source/sprites/Player.h +++ b/source/sprites/Player.h @@ -38,6 +38,7 @@ public: void setDestination(const sf::Vector2f& destination); using Character::getMagazineAmmo; using Character::getTotalAmmo; + using Character::getWeaponName; using Character::reload; using Character::toggleWeapon; using Character::selectFirstWeapon;