diff --git a/src/Characters/src/player.cpp b/src/Characters/src/player.cpp index deb3916..610a23b 100644 --- a/src/Characters/src/player.cpp +++ b/src/Characters/src/player.cpp @@ -21,6 +21,16 @@ Player::Player(CharacterConfig& config, std::shared_ptr& camera) } void Player::Update(double delta_time) { + + [this](double delta_time) { + static double time = 0; + time += delta_time; + if (time >= 1.0) { + time = 0.0; + IncreaseHealth(1); + } + }(delta_time); + ShootOrReload(); weapon_->Update(delta_time); Move(delta_time); @@ -50,10 +60,12 @@ void Player::SetPosition(Position2D position) { void Player::IncreaseHealth(double amount) { health_ += amount; + health_ = std::min(health_, 100.0); } void Player::DecreaseHealth(double amount) { health_ -= amount; + health_ = std::max(health_, 0.0); } double Player::GetHealth() const { diff --git a/src/State/include/State/enemy_state.h b/src/State/include/State/enemy_state.h index 7e5f1f1..0f783b1 100644 --- a/src/State/include/State/enemy_state.h +++ b/src/State/include/State/enemy_state.h @@ -32,6 +32,12 @@ class EnemyState : public State { public: virtual ~EnemyState() = default; + + void Reset() override; + int GetCurrentFrame() const override; + + protected: + std::unique_ptr animation_; }; typedef std::shared_ptr EnemyStatePtr; @@ -44,15 +50,12 @@ class IdleState : public EnemyState ~IdleState(); void Update(const double& delta_time) override; - void Reset() override; void OnContextSet() override; - int GetCurrentFrame() const override; EnemyStateType GetType() const override; private: double animation_speed_; double range_; - std::unique_ptr animation_; }; // ########################################### WalkState ########################################### @@ -63,9 +66,7 @@ class WalkState : public EnemyState ~WalkState(); void Update(const double& delta_time) override; - void Reset() override; void OnContextSet() override; - int GetCurrentFrame() const override; EnemyStateType GetType() const override; private: @@ -76,7 +77,6 @@ class WalkState : public EnemyState double attack_rate_; double attack_counter_; bool is_attacked_; - std::unique_ptr animation_; }; // ########################################### AttackState ########################################### @@ -87,15 +87,12 @@ class AttackState : public EnemyState ~AttackState(); void Update(const double& delta_time) override; - void Reset() override; void OnContextSet() override; - int GetCurrentFrame() const override; EnemyStateType GetType() const override; private: double animation_speed_; double attack_counter_; - std::unique_ptr animation_; }; // ########################################### PainState ########################################### @@ -106,15 +103,12 @@ class PainState : public EnemyState ~PainState(); void Update(const double& delta_time) override; - void Reset() override; void OnContextSet() override; - int GetCurrentFrame() const override; EnemyStateType GetType() const override; private: double animation_speed_; double counter; - std::unique_ptr animation_; }; // ########################################### DeathState ########################################### @@ -125,15 +119,12 @@ class DeathState : public EnemyState ~DeathState(); void Update(const double& delta_time) override; - void Reset() override; void OnContextSet() override; - int GetCurrentFrame() const override; EnemyStateType GetType() const override; private: double animation_speed_; double counter; - std::unique_ptr animation_; }; } // namespace wolfenstein diff --git a/src/State/include/State/weapon_state.h b/src/State/include/State/weapon_state.h index 97c4624..10b6cc3 100644 --- a/src/State/include/State/weapon_state.h +++ b/src/State/include/State/weapon_state.h @@ -32,6 +32,11 @@ class WeaponState : public State public: virtual ~WeaponState() = default; virtual void PullTrigger() {}; + void Reset() override; + int GetCurrentFrame() const override; + + protected: + std::unique_ptr animation_; }; typedef std::shared_ptr WeaponStatePtr; @@ -44,9 +49,7 @@ class LoadedState : public WeaponState ~LoadedState(); void Update(const double&) override; - void Reset() override; void OnContextSet() override; - int GetCurrentFrame() const override; WeaponStateType GetType() const override; void PullTrigger() override; @@ -55,7 +58,6 @@ class LoadedState : public WeaponState bool trigger_pulled_; double trigger_pull_time_; double fire_rate_; - std::unique_ptr animation_; }; // ########################################### OutOfAmmoState ########################################### @@ -66,9 +68,7 @@ class OutOfAmmoState : public WeaponState ~OutOfAmmoState(); void Update(const double&) override; - void Reset() override; void OnContextSet() override; - int GetCurrentFrame() const override; WeaponStateType GetType() const override; void PullTrigger() override; @@ -77,7 +77,6 @@ class OutOfAmmoState : public WeaponState bool trigger_pulled_; double trigger_pull_time_; double fire_rate_; - std::unique_ptr animation_; }; // ########################################### ReloadingState ########################################### @@ -88,15 +87,12 @@ class ReloadingState : public WeaponState ~ReloadingState(); void Update(const double&) override; - void Reset() override; void OnContextSet() override; - int GetCurrentFrame() const override; WeaponStateType GetType() const override; private: double reload_time_; double reload_speed_; - std::unique_ptr animation_; }; } // namespace wolfenstein diff --git a/src/State/src/enemy_state.cpp b/src/State/src/enemy_state.cpp index 0971185..7b1727b 100644 --- a/src/State/src/enemy_state.cpp +++ b/src/State/src/enemy_state.cpp @@ -6,6 +6,14 @@ namespace wolfenstein { +void EnemyState::Reset() { + animation_->Reset(); +} + +int EnemyState::GetCurrentFrame() const { + return animation_->GetCurrentFrame(); +} + // ########################################### IdleState ########################################### IdleState::IdleState() {} @@ -22,10 +30,6 @@ void IdleState::Update(const double& delta_time) { } } -void IdleState::Reset() { - animation_->Reset(); -} - void IdleState::OnContextSet() { const auto config = context_->GetStateConfig(); animation_speed_ = config.animation_time.idle_animation_speed; @@ -36,10 +40,6 @@ void IdleState::OnContextSet() { animation_speed_); } -int IdleState::GetCurrentFrame() const { - return animation_->GetCurrentFrame(); -} - EnemyStateType IdleState::GetType() const { return EnemyStateType::Idle; } @@ -95,11 +95,6 @@ void WalkState::Update(const double& delta_time) { animation_->Update(delta_time); } - -void WalkState::Reset() { - animation_->Reset(); -} - void WalkState::OnContextSet() { attack_rate_ = context_->GetWeapon()->GetAttackRate(); attack_range_ = context_->GetWeapon()->GetAttackRange(); @@ -107,10 +102,6 @@ void WalkState::OnContextSet() { context_->GetBotName() + "_walk", animation_speed_); } -int WalkState::GetCurrentFrame() const { - return animation_->GetCurrentFrame(); -} - EnemyStateType WalkState::GetType() const { return EnemyStateType::Walk; } @@ -136,26 +127,18 @@ void AttackState::Update(const double& delta_time) { attack_counter_ += delta_time; } -void AttackState::Reset() { - animation_->Reset(); -} - void AttackState::OnContextSet() { animation_speed_ = context_->GetWeapon()->GetAttackSpeed(); animation_ = std::make_unique( context_->GetBotName() + "_attack", animation_speed_); } -int AttackState::GetCurrentFrame() const { - return animation_->GetCurrentFrame(); -} - EnemyStateType AttackState::GetType() const { return EnemyStateType::Attack; } // ########################################### PainState ########################################### -PainState::PainState() : animation_speed_(0.25), counter(0.0) {} +PainState::PainState() : animation_speed_(0.2), counter(0.0) {} PainState::~PainState() {} @@ -172,19 +155,11 @@ void PainState::Update(const double& delta_time) { } } -void PainState::Reset() { - animation_->Reset(); -} - void PainState::OnContextSet() { animation_ = std::make_unique( context_->GetBotName() + "_pain", animation_speed_); } -int PainState::GetCurrentFrame() const { - return animation_->GetCurrentFrame(); -} - EnemyStateType PainState::GetType() const { return EnemyStateType::Pain; } @@ -205,18 +180,12 @@ void DeathState::Update(const double& delta_time) { } } -void DeathState::Reset() { - animation_->Reset(); -} void DeathState::OnContextSet() { animation_ = std::make_unique( context_->GetBotName() + "_death", animation_speed_); } -int DeathState::GetCurrentFrame() const { - return animation_->GetCurrentFrame(); -} EnemyStateType DeathState::GetType() const { return EnemyStateType::Death; diff --git a/src/State/src/weapon_state.cpp b/src/State/src/weapon_state.cpp index 276abee..dc8f65e 100644 --- a/src/State/src/weapon_state.cpp +++ b/src/State/src/weapon_state.cpp @@ -1,12 +1,20 @@ #include "State/weapon_state.h" #include "Animation/time_based_single_animation.h" +#include "ShootingManager/shooting_manager.h" #include "State/state.h" #include "Strike/weapon.h" #include -#include "ShootingManager/shooting_manager.h" namespace wolfenstein { +void WeaponState::Reset() { + animation_->Reset(); +} + +int WeaponState::GetCurrentFrame() const { + return animation_->GetCurrentFrame(); +} + // ########################################### LoadedState ########################################### LoadedState::LoadedState() : trigger_pulled_(false), trigger_pull_time_(0.0), fire_rate_(0.0) {} @@ -33,20 +41,12 @@ WeaponStateType LoadedState::GetType() const { return WeaponStateType::Loaded; } -void LoadedState::Reset() { - animation_->Reset(); -} - void LoadedState::OnContextSet() { fire_rate_ = context_->GetAttackSpeed(); animation_ = std::make_unique( context_->GetWeaponName() + "_loaded", fire_rate_); } -int LoadedState::GetCurrentFrame() const { - return animation_->GetCurrentFrame(); -} - void LoadedState::PullTrigger() { if (trigger_pulled_) { return; @@ -78,20 +78,12 @@ WeaponStateType OutOfAmmoState::GetType() const { return WeaponStateType::OutOfAmmo; } -void OutOfAmmoState::Reset() { - animation_->Reset(); -} - void OutOfAmmoState::OnContextSet() { fire_rate_ = context_->GetAttackSpeed(); animation_ = std::make_unique( context_->GetWeaponName() + "_outofammo", fire_rate_); } -int OutOfAmmoState::GetCurrentFrame() const { - return animation_->GetCurrentFrame(); -} - void OutOfAmmoState::PullTrigger() { if (trigger_pulled_) { return; @@ -120,18 +112,10 @@ WeaponStateType ReloadingState::GetType() const { return WeaponStateType::Reloading; } -void ReloadingState::Reset() { - animation_->Reset(); -} - void ReloadingState::OnContextSet() { reload_speed_ = context_->GetReloadSpeed(); animation_ = std::make_unique( context_->GetWeaponName() + "_reload", reload_speed_); } -int ReloadingState::GetCurrentFrame() const { - return animation_->GetCurrentFrame(); -} - } // namespace wolfenstein