From 6b1e8d906fb824ae6a260b38340d5da236d5cd8b Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 29 Aug 2013 21:53:14 +0200 Subject: [PATCH] Random item is dropped on death. --- src/Game.cpp | 1 + src/sprites/abstract/Character.cpp | 27 ++++++++++++++++++++++----- src/sprites/abstract/Character.h | 1 + 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index 1f166b2..c6f7bdc 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -31,6 +31,7 @@ Game::Game(tgui::Window& window) : mPaused(false) { mWindow.setFramerateLimit(FPS_GOAL); mWindow.setKeyRepeatEnabled(false); + srand(time(nullptr)); mGenerator.generateCurrentAreaIfNeeded(Vector2f()); initPlayer(); diff --git a/src/sprites/abstract/Character.cpp b/src/sprites/abstract/Character.cpp index 4e743e9..a6718f1 100644 --- a/src/sprites/abstract/Character.cpp +++ b/src/sprites/abstract/Character.cpp @@ -91,17 +91,34 @@ Character::getFaction() const { return mFaction; } +void Character::dropItem(std::shared_ptr item) { + mWorld.insert(item); + item->drop(getPosition()); +} + /** - * Called when health reaches zero. Drops corpse and item. + * Called when health reaches zero, drops corpse and a random item. */ void Character::onDeath() { mWorld.insert(std::shared_ptr(new Corpse(getPosition()))); - mWorld.insert(mActiveWeapon); - mActiveWeapon->drop(getPosition()); - // To avoid the weapon continuing to fire after pickup. - mActiveWeapon->releaseTrigger(); + switch (rand() % 3) { + case 0: + dropItem(mFirstWeapon); + break; + case 1: + dropItem(mSecondWeapon); + break; + case 2: + if (mLeftGadget.get() != nullptr) + dropItem(mLeftGadget); + else if (mRightGadget.get() != nullptr) + dropItem(mRightGadget); + } + // To avoid weapons continuing to fire after drop and pickup. + mFirstWeapon->releaseTrigger(); + mSecondWeapon->releaseTrigger(); } diff --git a/src/sprites/abstract/Character.h b/src/sprites/abstract/Character.h index 1d35600..42f6889 100644 --- a/src/sprites/abstract/Character.h +++ b/src/sprites/abstract/Character.h @@ -73,6 +73,7 @@ protected: private: void move(); + void dropItem(std::shared_ptr item); private: friend class Shield;