Fixed weapon pickup not working and crashing.

This commit is contained in:
Felix Ableitner 2013-08-29 22:42:45 +02:00
parent 6b1e8d906f
commit 624ce8daaa

View file

@ -111,9 +111,9 @@ Character::onDeath() {
dropItem(mSecondWeapon); dropItem(mSecondWeapon);
break; break;
case 2: case 2:
if (mLeftGadget.get() != nullptr) if (mLeftGadget)
dropItem(mLeftGadget); dropItem(mLeftGadget);
else if (mRightGadget.get() != nullptr) else if (mRightGadget)
dropItem(mRightGadget); dropItem(mRightGadget);
} }
// To avoid weapons continuing to fire after drop and pickup. // To avoid weapons continuing to fire after drop and pickup.
@ -228,10 +228,9 @@ Character::reload() {
void void
Character::toggleWeapon() { Character::toggleWeapon() {
mActiveWeapon->cancelReload(); (mActiveWeapon == mFirstWeapon)
mActiveWeapon = (mActiveWeapon == mFirstWeapon) ? selectSecondWeapon()
? mSecondWeapon : selectFirstWeapon();
: mFirstWeapon;
} }
void void
@ -263,6 +262,7 @@ Character::setFirstWeapon(std::shared_ptr<Weapon> weapon) {
if (mFirstWeapon == mActiveWeapon) if (mFirstWeapon == mActiveWeapon)
mActiveWeapon = weapon; mActiveWeapon = weapon;
mFirstWeapon = weapon; mFirstWeapon = weapon;
mFirstWeapon->setHolder(*this);
} }
/** /**
@ -274,6 +274,7 @@ Character::setSecondWeapon(std::shared_ptr<Weapon> weapon) {
if (mSecondWeapon == mActiveWeapon) if (mSecondWeapon == mActiveWeapon)
mActiveWeapon = weapon; mActiveWeapon = weapon;
mSecondWeapon = weapon; mSecondWeapon = weapon;
mSecondWeapon->setHolder(*this);
} }
void void
@ -315,20 +316,21 @@ void
Character::pickUpItem() { Character::pickUpItem() {
std::shared_ptr<Item> closest = mWorld.getClosestItem(getPosition()); std::shared_ptr<Item> closest = mWorld.getClosestItem(getPosition());
if (closest == nullptr) { if (!closest) {
std::swap(mLeftGadget, mRightGadget); std::swap(mLeftGadget, mRightGadget);
return; return;
} }
std::shared_ptr<Weapon> weapon = std::dynamic_pointer_cast<Weapon>(closest); std::shared_ptr<Weapon> weapon = std::dynamic_pointer_cast<Weapon>(closest);
if (weapon != nullptr) { if (weapon) {
mWorld.insert(mActiveWeapon); mWorld.insert(mActiveWeapon);
mActiveWeapon->drop(getPosition()); mActiveWeapon->drop(getPosition());
mActiveWeapon = weapon; (mActiveWeapon == mFirstWeapon)
mActiveWeapon->setHolder(*this); ? setFirstWeapon(weapon)
: setSecondWeapon(weapon);
} }
std::shared_ptr<Gadget> gadget = std::dynamic_pointer_cast<Gadget>(closest); std::shared_ptr<Gadget> gadget = std::dynamic_pointer_cast<Gadget>(closest);
if (gadget != nullptr) { if (gadget) {
mWorld.insert(mRightGadget); mWorld.insert(mRightGadget);
mRightGadget->drop(getPosition()); mRightGadget->drop(getPosition());
mRightGadget = mLeftGadget; mRightGadget = mLeftGadget;