Skip to content

Commit

Permalink
Update enemy states and use shooting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalkah committed Nov 27, 2024
1 parent 0e79a44 commit 18e9491
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 94 deletions.
3 changes: 3 additions & 0 deletions src/State/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ target_link_libraries(
enemy_state
PUBLIC
character
animation
navigation_manager
texture_manager
)

Expand All @@ -30,4 +32,5 @@ target_link_libraries(
PUBLIC
weapon
texture_manager
shooting_manager
)
42 changes: 17 additions & 25 deletions src/State/include/State/enemy_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
#ifndef STATE_INCLUDE_STATE_ENEMY_STATE_H_
#define STATE_INCLUDE_STATE_ENEMY_STATE_H_

#include "Animation/time_based_single_animation.h"
#include "State/state.h"
#include <memory>
#include <vector>

namespace wolfenstein {

namespace {
constexpr double kIdlePatrolDistance = 0.5;
} // namespace

class Enemy;

template <>
Expand Down Expand Up @@ -52,12 +50,9 @@ class IdleState : public EnemyState
EnemyStateType GetType() const override;

private:
int current_frame;
double counter;
double interval;
double animation_speed_;
double patrol_distance{0.25};
std::vector<uint16_t> textures;
double range_;
std::unique_ptr<TBSAnimation> animation_;
};

// ########################################### WalkState ###########################################
Expand All @@ -74,11 +69,14 @@ class WalkState : public EnemyState
EnemyStateType GetType() const override;

private:
int current_frame;
double counter;
double interval;
double animation_speed_;
std::vector<uint16_t> textures;
double range_max_;
double range_min_;
double attack_range_;
double attack_rate_;
double attack_counter_;
bool is_attacked_;
std::unique_ptr<TBSAnimation> animation_;
};

// ########################################### AttackState ###########################################
Expand All @@ -95,11 +93,9 @@ class AttackState : public EnemyState
EnemyStateType GetType() const override;

private:
int current_frame;
double counter;
double interval;
double animation_speed_;
std::vector<uint16_t> textures;
double attack_counter_;
std::unique_ptr<TBSAnimation> animation_;
};

// ########################################### PainState ###########################################
Expand All @@ -116,11 +112,9 @@ class PainState : public EnemyState
EnemyStateType GetType() const override;

private:
int current_frame;
double counter;
double interval;
double animation_speed_;
std::vector<uint16_t> textures;
double counter;
std::unique_ptr<TBSAnimation> animation_;
};

// ########################################### DeathState ###########################################
Expand All @@ -137,11 +131,9 @@ class DeathState : public EnemyState
EnemyStateType GetType() const override;

private:
int current_frame;
double counter;
double interval;
double animation_speed_;
std::vector<uint16_t> textures;
double counter;
std::unique_ptr<TBSAnimation> animation_;
};

} // namespace wolfenstein
Expand Down
172 changes: 103 additions & 69 deletions src/State/src/enemy_state.cpp
Original file line number Diff line number Diff line change
@@ -1,183 +1,217 @@
#include "State/enemy_state.h"
#include "Characters/enemy.h"
#include "NavigationManager/navigation_manager.h"
#include "ShootingManager/shooting_manager.h"
#include "TextureManager/texture_manager.h"

namespace wolfenstein {

// ########################################### IdleState ###########################################
IdleState::IdleState() : current_frame(0), counter(0), interval(0) {}
IdleState::IdleState() {}

IdleState::~IdleState() {}

void IdleState::Update(const double& delta_time) {
counter += delta_time;
if (counter > animation_speed_) {
current_frame = (current_frame + 1) % textures.size();
counter = 0;
animation_->Update(delta_time);

if (context_->IsPlayerInShootingRange()) {
context_->TransitionTo(std::make_shared<WalkState>());
}
if (context_->IsAttacked()) {
context_->TransitionTo(std::make_shared<PainState>());
}
interval += delta_time;
// if (interval > 5) {
// context_->TransitionTo(std::make_shared<WalkState>());
// }
}

void IdleState::Reset() {
current_frame = 0;
counter = 0;
animation_->Reset();
}

void IdleState::OnContextSet() {
animation_speed_ = 0.3;
textures = TextureManager::GetInstance().GetTextureCollection(
context_->GetBotName() + "_idle");
const auto config = context_->GetStateConfig();
animation_speed_ = config.animation_time.idle_animation_speed;
range_ = config.follow_range_max;
animation_ = std::make_unique<TBSAnimation>(
TextureManager::GetInstance().GetTextureCollection(
context_->GetBotName() + "_idle"),
animation_speed_);
}

int IdleState::GetCurrentFrame() const {
return textures[current_frame];
return animation_->GetCurrentFrame();
}

EnemyStateType IdleState::GetType() const {
return EnemyStateType::Idle;
}

// ########################################### WalkState ###########################################
WalkState::WalkState() : current_frame(0), counter(0), interval(0) {}
WalkState::WalkState()
: animation_speed_(1.2),
range_max_(5.0),
range_min_(1.5),
attack_range_(5.0),
attack_rate_(1.0),
attack_counter_(0.0),
is_attacked_(false) {}

WalkState::~WalkState() {}

void WalkState::Update(const double& delta_time) {
counter += delta_time;
if (counter > animation_speed_) {
current_frame = (current_frame + 1) % textures.size();
counter = 0;
const auto bot_position = context_->GetPosition();
const auto distance =
NavigationManager::GetInstance().EuclideanDistanceToPlayer(
bot_position);
if (context_->IsAttacked()) {
NavigationManager::GetInstance().ResetPath(context_->GetId());
context_->SetNextPose(bot_position.pose);
context_->TransitionTo(std::make_shared<PainState>());
return;
}
if (!context_->IsPlayerInShootingRange()) {
if (distance > range_max_) {
NavigationManager::GetInstance().ResetPath(context_->GetId());
context_->TransitionTo(std::make_shared<IdleState>());
return;
}
}
interval += delta_time;
if (interval > 5) {
context_->TransitionTo(std::make_shared<AttackState>());
if (distance <= attack_range_ && context_->IsPlayerInShootingRange()) {
attack_counter_ += delta_time;
if (attack_counter_ > attack_rate_) {
context_->SetNextPose(bot_position.pose);
context_->TransitionTo(std::make_shared<AttackState>());
return;
}
}
if ((!context_->IsPlayerInShootingRange()) ||
((distance > range_min_) && context_->IsPlayerInShootingRange())) {
auto next_position = NavigationManager::GetInstance().FindPathToPlayer(
bot_position, context_->GetId());
context_->SetNextPose(next_position);
}
else {
NavigationManager::GetInstance().ResetPath(context_->GetId());
context_->SetNextPose(bot_position.pose);
}

animation_->Update(delta_time);
}

void WalkState::Reset() {
current_frame = 0;
counter = 0;
animation_->Reset();
}

void WalkState::OnContextSet() {
animation_speed_ = 0.3;
textures = TextureManager::GetInstance().GetTextureCollection(
context_->GetBotName() + "_walk");
animation_ = std::make_unique<TBSAnimation>(
context_->GetBotName() + "_walk", animation_speed_);
}

int WalkState::GetCurrentFrame() const {
return textures[current_frame];
return animation_->GetCurrentFrame();
}

EnemyStateType WalkState::GetType() const {
return EnemyStateType::Walk;
}

// ########################################### AttackState ###########################################
AttackState::AttackState() : current_frame(0), counter(0), interval(0) {}
AttackState::AttackState() : animation_speed_(0.5), attack_counter_(0.0) {}

AttackState::~AttackState() {}

void AttackState::Update(const double& delta_time) {
counter += delta_time;
if (counter > animation_speed_) {
current_frame = (current_frame + 1) % textures.size();
counter = 0;
animation_->Update(delta_time);
if (attack_counter_ == 0.0) {
ShootingManager::GetInstance().EnemyShoot();
}
interval += delta_time;
if (interval > 5) {
if (context_->IsAttacked()) {
context_->TransitionTo(std::make_shared<PainState>());
return;
}
if (attack_counter_ > animation_speed_) {
context_->TransitionTo(std::make_shared<WalkState>());
return;
}
attack_counter_ += delta_time;
}

void AttackState::Reset() {
current_frame = 0;
counter = 0;
animation_->Reset();
}

void AttackState::OnContextSet() {
animation_speed_ = 0.3;
textures = TextureManager::GetInstance().GetTextureCollection(
context_->GetBotName() + "_attack");
animation_ = std::make_unique<TBSAnimation>(
context_->GetBotName() + "_attack", animation_speed_);
}

int AttackState::GetCurrentFrame() const {
return textures[current_frame];
return animation_->GetCurrentFrame();
}

EnemyStateType AttackState::GetType() const {
return EnemyStateType::Attack;
}

// ########################################### PainState ###########################################
PainState::PainState() : current_frame(0), counter(0), interval(0) {}
PainState::PainState() : animation_speed_(0.25), counter(0.0) {}

PainState::~PainState() {}

void PainState::Update(const double& delta_time) {
animation_->Update(delta_time);
counter += delta_time;
if (counter > animation_speed_) {
current_frame = (current_frame + 1) % textures.size();
counter = 0;
}
interval += delta_time;
if (interval > 5) {
context_->TransitionTo(std::make_shared<DeathState>());
if (context_->GetHealth() <= 0) {
context_->TransitionTo(std::make_shared<DeathState>());
return;
}
context_->SetAttacked(false);
context_->TransitionTo(std::make_shared<WalkState>());
}
}

void PainState::Reset() {
current_frame = 0;
counter = 0;
animation_->Reset();
}

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

int PainState::GetCurrentFrame() const {
return textures[current_frame];
return animation_->GetCurrentFrame();
}

EnemyStateType PainState::GetType() const {
return EnemyStateType::Pain;
}

// ########################################### DeathState ###########################################
DeathState::DeathState() : current_frame(0), counter(0), interval(0) {}
DeathState::DeathState() : animation_speed_(1.0) {}

DeathState::~DeathState() {}

void DeathState::Update(const double& delta_time) {
counter += delta_time;
if (counter > animation_speed_) {
current_frame = (current_frame + 1) % textures.size();
counter = 0;
if (!animation_->IsAnimationFinishedOnce()) {
animation_->Update(delta_time);
counter += delta_time;
}
interval += delta_time;
if (interval > 5) {
context_->TransitionTo(std::make_shared<IdleState>());
else {
context_->SetDeath();
}
}

void DeathState::Reset() {
current_frame = 0;
counter = 0;
animation_->Reset();
}

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

int DeathState::GetCurrentFrame() const {
return textures[current_frame];
return animation_->GetCurrentFrame();
}

EnemyStateType DeathState::GetType() const {
Expand Down
Loading

0 comments on commit 18e9491

Please sign in to comment.