From 07f7b19ca2e591c25063393c92b0daf2e8e6cdf8 Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Wed, 13 Apr 2016 21:16:27 +0100 Subject: [PATCH] Make it possible to fire weapons again --- rwengine/include/ai/CharacterController.hpp | 6 ---- rwengine/include/objects/CharacterObject.hpp | 8 ++++++ rwengine/src/ai/CharacterController.cpp | 19 ++++--------- rwengine/src/items/WeaponItem.cpp | 19 +++++-------- rwengine/src/objects/CharacterObject.cpp | 29 ++++++++++++-------- rwgame/ingamestate.cpp | 4 +-- 6 files changed, 40 insertions(+), 45 deletions(-) diff --git a/rwengine/include/ai/CharacterController.hpp b/rwengine/include/ai/CharacterController.hpp index 8f1addf1..c85c837d 100644 --- a/rwengine/include/ai/CharacterController.hpp +++ b/rwengine/include/ai/CharacterController.hpp @@ -102,12 +102,6 @@ public: virtual glm::vec3 getTargetPosition() = 0; - /** - * Uses the character's active item. - * @param primary use the primary action. - */ - void useItem(bool active, bool primary = true); - /** * @brief * @return Returns the Character Object diff --git a/rwengine/include/objects/CharacterObject.hpp b/rwengine/include/objects/CharacterObject.hpp index a64849c6..c771868d 100644 --- a/rwengine/include/objects/CharacterObject.hpp +++ b/rwengine/include/objects/CharacterObject.hpp @@ -28,6 +28,8 @@ struct CharacterState uint32_t lastFireTimeMS; bool primaryActive; bool secondaryActive; + uint32_t primaryStartTime; + uint32_t primaryEndTime; }; class VehicleObject; @@ -177,6 +179,12 @@ public: InventoryItem* getActiveItem(); void removeFromInventory( int slot ); + /** + * Uses the character's active item. + * @param primary use the primary action. + */ + void useItem(bool active, bool primary = true); + void cycleInventory( bool up ); }; diff --git a/rwengine/src/ai/CharacterController.cpp b/rwengine/src/ai/CharacterController.cpp index e2c91d9a..3ad47846 100644 --- a/rwengine/src/ai/CharacterController.cpp +++ b/rwengine/src/ai/CharacterController.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include bool CharacterController::updateActivity() { @@ -170,18 +170,6 @@ void CharacterController::update(float dt) } } -void CharacterController::useItem(bool active, bool primary) -{ - if( character->getActiveItem() ) { - if( primary ) { - character->getCurrentState().primaryActive = active; - } - else { - character->getCurrentState().secondaryActive = active; - } - } -} - CharacterObject *CharacterController::getCharacter() const { return character; @@ -455,6 +443,11 @@ bool Activities::ShootWeapon::update(CharacterObject *character, CharacterContro character->animator->setAnimation(throwanim, false); } } + else if( wepdata->fireType == WeaponData::MELEE ) { + RW_CHECK(wepdata->fireType != WeaponData::MELEE, "Melee attacks not implemented"); + return true; + } + return false; } diff --git a/rwengine/src/items/WeaponItem.cpp b/rwengine/src/items/WeaponItem.cpp index 30de80c8..5be2b8d5 100644 --- a/rwengine/src/items/WeaponItem.cpp +++ b/rwengine/src/items/WeaponItem.cpp @@ -105,13 +105,13 @@ void WeaponItem::fireProjectile(CharacterObject* owner) // Work out the velocity multiplier as a function of how long the player // Was holding down the fire button. If _fireStop < 0.f then the player // is still holding the button down. - float throwTime = owner->engine->getGameTime() - owner->getCurrentState().lastFireTimeMS/1000.f; + float throwTime = owner->engine->getGameTime() - owner->getCurrentState().primaryStartTime/1000.f; float forceFactor = throwTime; -#if 0 - if( _fireStop > 0.f ) { - forceFactor = _fireStop - _fireStart; + if (owner->getCurrentState().primaryEndTime >= owner->getCurrentState().primaryStartTime) { + uint32_t heldTime = owner->getCurrentState().primaryEndTime - owner->getCurrentState().primaryStartTime; + forceFactor = (heldTime)/1000.f; } - forceFactor /= throwTime; + forceFactor = std::max(0.1f, forceFactor / throwTime); auto projectile = new ProjectileObject(owner->engine, fireOrigin, { @@ -124,20 +124,15 @@ void WeaponItem::fireProjectile(CharacterObject* owner) auto& pool = owner->engine->getTypeObjectPool( projectile ); pool.insert(projectile); -#endif + owner->engine->allObjects.push_back(projectile); } void WeaponItem::primary(CharacterObject* owner) { -#if 0 - if( active ) { - _fireStart = owner->engine->getGameTime(); - _fireStop = -1.f; - + if( owner->getCurrentState().primaryActive ) { // ShootWeapon will call ::fire() on us at the appropriate time. owner->controller->setNextActivity(new Activities::ShootWeapon(this)); } -#endif } void WeaponItem::secondary(CharacterObject* owner) diff --git a/rwengine/src/objects/CharacterObject.cpp b/rwengine/src/objects/CharacterObject.cpp index 37383238..88c1ca91 100644 --- a/rwengine/src/objects/CharacterObject.cpp +++ b/rwengine/src/objects/CharacterObject.cpp @@ -126,18 +126,6 @@ void CharacterObject::tick(float dt) animator->tick(dt); updateCharacter(dt); - // Update the item if we're using it - if (currentState.primaryActive) { - if (getActiveItem()) { - getActiveItem()->primary(this); - } - } - if (currentState.secondaryActive) { - if (getActiveItem()) { - getActiveItem()->secondary(this); - } - } - // Ensure the character doesn't need to be reset if(getPosition().z < -100.f) { resetToAINode(); @@ -523,3 +511,20 @@ void CharacterObject::cycleInventory(bool up) } } +void CharacterObject::useItem(bool active, bool primary) +{ + if( getActiveItem() ) { + if( primary ) { + if (active) + currentState.primaryStartTime = engine->getGameTime() * 1000.f; + else + currentState.primaryEndTime = engine->getGameTime() * 1000.f; + currentState.primaryActive = active; + getActiveItem()->primary(this); + } + else { + currentState.secondaryActive = active; + getActiveItem()->secondary(this); + } + } +} diff --git a/rwgame/ingamestate.cpp b/rwgame/ingamestate.cpp index 3fe74aa9..57293cc3 100644 --- a/rwgame/ingamestate.cpp +++ b/rwgame/ingamestate.cpp @@ -352,7 +352,7 @@ void IngameState::handlePlayerInput(const sf::Event& event) case sf::Event::MouseButtonPressed: switch(event.mouseButton.button) { case sf::Mouse::Left: - player->useItem(true, true); + player->getCharacter()->useItem(true, true); break; default: break; } @@ -360,7 +360,7 @@ void IngameState::handlePlayerInput(const sf::Event& event) case sf::Event::MouseButtonReleased: switch(event.mouseButton.button) { case sf::Mouse::Left: - player->useItem(false, true); + player->getCharacter()->useItem(false, true); break; default: break; }