Skip to content

Commit

Permalink
Adjust states
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalkah committed Nov 30, 2024
1 parent e16a099 commit 1074e87
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 89 deletions.
12 changes: 12 additions & 0 deletions src/Characters/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ Player::Player(CharacterConfig& config, std::shared_ptr<Camera2D>& 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);
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 6 additions & 15 deletions src/State/include/State/enemy_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class EnemyState : public State<Enemy>
{
public:
virtual ~EnemyState() = default;

void Reset() override;
int GetCurrentFrame() const override;

protected:
std::unique_ptr<TBSAnimation> animation_;
};

typedef std::shared_ptr<EnemyState> EnemyStatePtr;
Expand All @@ -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<TBSAnimation> animation_;
};

// ########################################### WalkState ###########################################
Expand All @@ -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:
Expand All @@ -76,7 +77,6 @@ class WalkState : public EnemyState
double attack_rate_;
double attack_counter_;
bool is_attacked_;
std::unique_ptr<TBSAnimation> animation_;
};

// ########################################### AttackState ###########################################
Expand All @@ -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<TBSAnimation> animation_;
};

// ########################################### PainState ###########################################
Expand All @@ -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<TBSAnimation> animation_;
};

// ########################################### DeathState ###########################################
Expand All @@ -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<TBSAnimation> animation_;
};

} // namespace wolfenstein
Expand Down
14 changes: 5 additions & 9 deletions src/State/include/State/weapon_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class WeaponState : public State<Weapon>
public:
virtual ~WeaponState() = default;
virtual void PullTrigger() {};
void Reset() override;
int GetCurrentFrame() const override;

protected:
std::unique_ptr<TBSAnimation> animation_;
};

typedef std::shared_ptr<WeaponState> WeaponStatePtr;
Expand All @@ -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;
Expand All @@ -55,7 +58,6 @@ class LoadedState : public WeaponState
bool trigger_pulled_;
double trigger_pull_time_;
double fire_rate_;
std::unique_ptr<TBSAnimation> animation_;
};

// ########################################### OutOfAmmoState ###########################################
Expand All @@ -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;
Expand All @@ -77,7 +77,6 @@ class OutOfAmmoState : public WeaponState
bool trigger_pulled_;
double trigger_pull_time_;
double fire_rate_;
std::unique_ptr<TBSAnimation> animation_;
};

// ########################################### ReloadingState ###########################################
Expand All @@ -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<TBSAnimation> animation_;
};

} // namespace wolfenstein
Expand Down
49 changes: 9 additions & 40 deletions src/State/src/enemy_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

namespace wolfenstein {

void EnemyState::Reset() {
animation_->Reset();
}

int EnemyState::GetCurrentFrame() const {
return animation_->GetCurrentFrame();
}

// ########################################### IdleState ###########################################
IdleState::IdleState() {}

Expand All @@ -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;
Expand All @@ -36,10 +40,6 @@ void IdleState::OnContextSet() {
animation_speed_);
}

int IdleState::GetCurrentFrame() const {
return animation_->GetCurrentFrame();
}

EnemyStateType IdleState::GetType() const {
return EnemyStateType::Idle;
}
Expand Down Expand Up @@ -95,22 +95,13 @@ 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();
animation_ = std::make_unique<TBSAnimation>(
context_->GetBotName() + "_walk", animation_speed_);
}

int WalkState::GetCurrentFrame() const {
return animation_->GetCurrentFrame();
}

EnemyStateType WalkState::GetType() const {
return EnemyStateType::Walk;
}
Expand All @@ -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<TBSAnimation>(
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() {}

Expand All @@ -172,19 +155,11 @@ void PainState::Update(const double& delta_time) {
}
}

void PainState::Reset() {
animation_->Reset();
}

void PainState::OnContextSet() {
animation_ = std::make_unique<TBSAnimation>(
context_->GetBotName() + "_pain", animation_speed_);
}

int PainState::GetCurrentFrame() const {
return animation_->GetCurrentFrame();
}

EnemyStateType PainState::GetType() const {
return EnemyStateType::Pain;
}
Expand All @@ -205,18 +180,12 @@ void DeathState::Update(const double& delta_time) {
}
}

void DeathState::Reset() {
animation_->Reset();
}

void DeathState::OnContextSet() {
animation_ = std::make_unique<TBSAnimation>(
context_->GetBotName() + "_death", animation_speed_);
}

int DeathState::GetCurrentFrame() const {
return animation_->GetCurrentFrame();
}

EnemyStateType DeathState::GetType() const {
return EnemyStateType::Death;
Expand Down
34 changes: 9 additions & 25 deletions src/State/src/weapon_state.cpp
Original file line number Diff line number Diff line change
@@ -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 <memory>
#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) {}
Expand All @@ -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<TBSAnimation>(
context_->GetWeaponName() + "_loaded", fire_rate_);
}

int LoadedState::GetCurrentFrame() const {
return animation_->GetCurrentFrame();
}

void LoadedState::PullTrigger() {
if (trigger_pulled_) {
return;
Expand Down Expand Up @@ -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<TBSAnimation>(
context_->GetWeaponName() + "_outofammo", fire_rate_);
}

int OutOfAmmoState::GetCurrentFrame() const {
return animation_->GetCurrentFrame();
}

void OutOfAmmoState::PullTrigger() {
if (trigger_pulled_) {
return;
Expand Down Expand Up @@ -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<TBSAnimation>(
context_->GetWeaponName() + "_reload", reload_speed_);
}

int ReloadingState::GetCurrentFrame() const {
return animation_->GetCurrentFrame();
}

} // namespace wolfenstein

0 comments on commit 1074e87

Please sign in to comment.