From 8036e2591425b91d4ba6786f91f77e5beccfcbe3 Mon Sep 17 00:00:00 2001 From: Bilal Kahraman Date: Fri, 27 Sep 2024 19:30:44 +0300 Subject: [PATCH] Add basic member functions to character Remove bot and weapon name from states Remove texture maps Remove unnecessary dependencies --- src/Animation/CMakeLists.txt | 5 + .../Animation/time_based_single_animation.h | 3 + .../src/time_based_single_animation.cpp | 10 +- src/Camera/CMakeLists.txt | 5 +- src/Camera/include/Camera/camera.h | 3 - src/Camera/src/camera.cpp | 1 - src/Characters/CMakeLists.txt | 8 +- src/Characters/include/Characters/character.h | 5 + src/Characters/include/Characters/enemy.h | 24 +++- src/Characters/include/Characters/player.h | 4 + src/Characters/src/enemy.cpp | 41 +++++-- src/Characters/src/player.cpp | 15 ++- src/Core/CMakeLists.txt | 1 + src/Core/src/game.cpp | 25 +++-- src/Core/src/scene.cpp | 9 +- src/State/include/State/enemy_state.h | 30 +++-- src/State/include/State/weapon_state.h | 18 +-- src/State/src/enemy_state.cpp | 105 ++++++++---------- src/State/src/weapon_state.cpp | 52 +++------ src/Strike/include/Strike/weapon.h | 1 - src/Strike/src/weapon.cpp | 5 +- 21 files changed, 206 insertions(+), 164 deletions(-) diff --git a/src/Animation/CMakeLists.txt b/src/Animation/CMakeLists.txt index e2f3ec9..158583b 100644 --- a/src/Animation/CMakeLists.txt +++ b/src/Animation/CMakeLists.txt @@ -6,4 +6,9 @@ add_library( target_include_directories(animation PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ) +target_link_libraries( + animation + PUBLIC + texture_manager +) diff --git a/src/Animation/include/Animation/time_based_single_animation.h b/src/Animation/include/Animation/time_based_single_animation.h index 667b06e..fe4fc94 100644 --- a/src/Animation/include/Animation/time_based_single_animation.h +++ b/src/Animation/include/Animation/time_based_single_animation.h @@ -14,6 +14,7 @@ #include "Animation/animation.h" #include +#include #include namespace wolfenstein { @@ -23,6 +24,8 @@ class TBSAnimation : public IAnimation public: TBSAnimation(const std::vector& textures, const double animation_speed); + TBSAnimation(const std::string collection_name, + const double animation_speed); ~TBSAnimation() = default; void Update(const double& delta_time) override; diff --git a/src/Animation/src/time_based_single_animation.cpp b/src/Animation/src/time_based_single_animation.cpp index 1994685..1dc589c 100644 --- a/src/Animation/src/time_based_single_animation.cpp +++ b/src/Animation/src/time_based_single_animation.cpp @@ -1,5 +1,5 @@ #include "Animation/time_based_single_animation.h" - +#include "TextureManager/texture_manager.h" namespace wolfenstein { TBSAnimation::TBSAnimation(const std::vector& textures, @@ -9,6 +9,14 @@ TBSAnimation::TBSAnimation(const std::vector& textures, animation_speed(animation_speed), counter(0) {} +TBSAnimation::TBSAnimation(const std::string collection_name, + const double animation_speed) + : textures( + TextureManager::GetInstance().GetTextureCollection(collection_name)), + current_frame(0), + animation_speed(animation_speed / textures.size()), + counter(0) {} + void TBSAnimation::Update(const double& delta_time) { counter += delta_time; if (counter > animation_speed) { diff --git a/src/Camera/CMakeLists.txt b/src/Camera/CMakeLists.txt index 81b8d8a..b8a4180 100644 --- a/src/Camera/CMakeLists.txt +++ b/src/Camera/CMakeLists.txt @@ -27,9 +27,6 @@ target_link_libraries( PUBLIC game_object ray - character - vector - map scene - texture_manager + vector ) diff --git a/src/Camera/include/Camera/camera.h b/src/Camera/include/Camera/camera.h index dee5016..e9b60e0 100644 --- a/src/Camera/include/Camera/camera.h +++ b/src/Camera/include/Camera/camera.h @@ -14,11 +14,8 @@ #include "Camera/ray.h" #include "Camera/raycaster.h" -#include "Characters/character.h" #include "Core/scene.h" #include "GameObjects/game_object.h" -#include "GameObjects/static_object.h" -#include "Map/map.h" #include #include diff --git a/src/Camera/src/camera.cpp b/src/Camera/src/camera.cpp index 0771f84..d8eeb25 100644 --- a/src/Camera/src/camera.cpp +++ b/src/Camera/src/camera.cpp @@ -12,7 +12,6 @@ #include "Camera/camera.h" #include "Camera/ray.h" #include "Math/vector.h" -#include "TextureManager/texture_manager.h" #include #include diff --git a/src/Characters/CMakeLists.txt b/src/Characters/CMakeLists.txt index 0e13dea..b1970dc 100644 --- a/src/Characters/CMakeLists.txt +++ b/src/Characters/CMakeLists.txt @@ -10,12 +10,12 @@ target_include_directories(character PUBLIC target_link_libraries( character PUBLIC - game_object - vector - collision_manager animation + collision_manager + enemy_state + game_object SDL2 + vector weapon - enemy_state ) diff --git a/src/Characters/include/Characters/character.h b/src/Characters/include/Characters/character.h index 33d9361..01f7547 100644 --- a/src/Characters/include/Characters/character.h +++ b/src/Characters/include/Characters/character.h @@ -32,6 +32,8 @@ struct CharacterConfig Position2D initial_position; double translation_speed; double rotation_speed; + double width; + double height; }; class ICharacter @@ -41,6 +43,9 @@ class ICharacter virtual void SetPosition(Position2D position) = 0; virtual Position2D GetPosition() const = 0; + virtual void IncreaseHealth(double amount) = 0; + virtual void DecreaseHealth(double amount) = 0; + virtual double GetHealth() const = 0; }; } // namespace wolfenstein diff --git a/src/Characters/include/Characters/enemy.h b/src/Characters/include/Characters/enemy.h index 0d264d0..bde4bc5 100644 --- a/src/Characters/include/Characters/enemy.h +++ b/src/Characters/include/Characters/enemy.h @@ -17,46 +17,62 @@ #include "Math/vector.h" #include "State/enemy_state.h" #include +#include namespace wolfenstein { +class EnemyFactory; class Enemy : public ICharacter, public IGameObject, public std::enable_shared_from_this { public: - explicit Enemy(CharacterConfig config, EnemyStatePtr state, double width, - double height); + explicit Enemy(std::string bot_name, CharacterConfig config); ~Enemy() = default; - void Init(); void Update(double delta_time) override; void TransitionTo(EnemyStatePtr state); void SetPose(const vector2d& pose) override; void SetPosition(Position2D position) override; + void IncreaseHealth(double amount) override; + void DecreaseHealth(double amount) override; + double GetHealth() const override; ObjectType GetObjectType() const override; vector2d GetPose() const override; Position2D GetPosition() const override; std::string GetId() const override; + std::string GetBotName() const; int GetTextureId() const override; double GetWidth() const override; double GetHeight() const override; void SetNextPose(vector2d pose); + friend class EnemyFactory; private: Enemy() = default; + void Move(double delta_time); + std::string bot_name_; Position2D position_; double rotation_speed_; double translation_speed_; - EnemyStatePtr state_; double width; double height; + double health_{100}; std::string id_; vector2d next_pose; + EnemyStatePtr state_; + EnemyStatePtr previous_state_; +}; + +class EnemyFactory +{ + public: + static std::shared_ptr CreateEnemy(std::string bot_name, + CharacterConfig config); }; } // namespace wolfenstein diff --git a/src/Characters/include/Characters/player.h b/src/Characters/include/Characters/player.h index e4c10b9..149e6fe 100644 --- a/src/Characters/include/Characters/player.h +++ b/src/Characters/include/Characters/player.h @@ -33,6 +33,9 @@ class Player : public ICharacter, public IGameObject ObjectType GetObjectType() const override; vector2d GetPose() const override; void SetPosition(Position2D position) override; + void IncreaseHealth(double amount) override; + void DecreaseHealth(double amount) override; + double GetHealth() const override; Position2D GetPosition() const override; std::string GetId() const override; int GetTextureId() const override; @@ -50,6 +53,7 @@ class Player : public ICharacter, public IGameObject double translation_speed_; double width_{0.4}; double height_{1.0}; + double health_{100}; std::string id_; std::shared_ptr weapon_; std::vector> player_position_subscribers_; diff --git a/src/Characters/src/enemy.cpp b/src/Characters/src/enemy.cpp index 66d5e0b..da9b744 100644 --- a/src/Characters/src/enemy.cpp +++ b/src/Characters/src/enemy.cpp @@ -6,19 +6,15 @@ namespace wolfenstein { -Enemy::Enemy(CharacterConfig config, EnemyStatePtr state, - double width, double height) - : position_(config.initial_position), +Enemy::Enemy(std::string bot_name, CharacterConfig config) + : bot_name_(bot_name), + position_(config.initial_position), rotation_speed_(config.rotation_speed), translation_speed_(config.translation_speed), - state_(state), - width(width), - height(height), - id_(UuidGenerator::GetInstance().GenerateUuid().bytes()) {} - -void Enemy::Init() { - state_->SetContext(shared_from_this()); -} + width(config.width), + height(config.height), + id_(UuidGenerator::GetInstance().GenerateUuid().bytes()), + next_pose(position_.pose) {} void Enemy::TransitionTo(EnemyStatePtr state) { state_ = state; @@ -48,6 +44,18 @@ void Enemy::SetPosition(Position2D position) { position_ = position; } +void Enemy::IncreaseHealth(double amount) { + health_ += amount; +} + +void Enemy::DecreaseHealth(double amount) { + health_ -= amount; +} + +double Enemy::GetHealth() const { + return health_; +} + Position2D Enemy::GetPosition() const { return position_; } @@ -56,6 +64,10 @@ std::string Enemy::GetId() const { return id_; } +std::string Enemy::GetBotName() const { + return bot_name_; +} + void Enemy::Move(double delta_time) { vector2d direction = next_pose - position_.pose; direction.Norm(); @@ -85,4 +97,11 @@ double Enemy::GetHeight() const { return height; } +std::shared_ptr EnemyFactory::CreateEnemy(std::string bot_name, + CharacterConfig config) { + auto enemy_ptr = std::make_shared(bot_name, config); + enemy_ptr->TransitionTo(std::make_shared()); + return enemy_ptr; +} + } // namespace wolfenstein diff --git a/src/Characters/src/player.cpp b/src/Characters/src/player.cpp index 2e6c0af..80ea4f8 100644 --- a/src/Characters/src/player.cpp +++ b/src/Characters/src/player.cpp @@ -12,8 +12,9 @@ Player::Player(CharacterConfig& config) rotation_speed_(config.rotation_speed), translation_speed_(config.translation_speed) { id_ = UuidGenerator::GetInstance().GenerateUuid().bytes(); + weapon_ = std::make_shared("mp5"); - WeaponStatePtr loaded_state = std::make_shared("mp5"); + WeaponStatePtr loaded_state = std::make_shared(); weapon_->TransitionTo(loaded_state); } @@ -42,6 +43,18 @@ void Player::SetPosition(Position2D position) { position_ = position; } +void Player::IncreaseHealth(double amount) { + health_ += amount; +} + +void Player::DecreaseHealth(double amount) { + health_ -= amount; +} + +double Player::GetHealth() const { + return health_; +} + Position2D Player::GetPosition() const { return position_; } diff --git a/src/Core/CMakeLists.txt b/src/Core/CMakeLists.txt index 98642f6..afb13a4 100644 --- a/src/Core/CMakeLists.txt +++ b/src/Core/CMakeLists.txt @@ -13,6 +13,7 @@ target_link_libraries( map navigation_manager collision_manager + character ) add_library( diff --git a/src/Core/src/game.cpp b/src/Core/src/game.cpp index bf98007..660a0c9 100644 --- a/src/Core/src/game.cpp +++ b/src/Core/src/game.cpp @@ -46,7 +46,8 @@ void Game::Init() { config_.view_distance}; camera_ = std::make_shared(camera_config); - CharacterConfig player_config = {Position2D({3, 1.5}, 1.50), 2.0, 0.4}; + CharacterConfig player_config = {Position2D({3, 1.5}, 1.50), 2.0, 0.4, 0.4, + 1.0}; player_ = std::make_shared(player_config); player_->SubscribeToPlayerPosition(std::bind( @@ -116,16 +117,18 @@ void Game::Run() { } void Game::PrepareEnemies() { - auto caco_demon = std::make_shared( - CharacterConfig(Position2D({13.5, 2.5}, 1.50), 0.8, 0.4), - std::make_shared("caco_demon"), 0.4, 0.8); - - auto soldier = std::make_shared( - CharacterConfig(Position2D({9, 7}, 1.50), 0.8, 0.4), - std::make_shared("soldier"), 0.3, 0.6); - auto cyber_demon = std::make_shared( - CharacterConfig(Position2D({23.0, 4}, 1.50), 0.8, 0.4), - std::make_shared("cyber_demon"), 0.5, 1.0); + auto caco_demon = EnemyFactory::CreateEnemy( + "caco_demon", + CharacterConfig(Position2D({13.5, 2.5}, 1.50), 0.8, 0.4, 0.4, 0.8)); + + auto soldier = EnemyFactory::CreateEnemy( + "soldier", + CharacterConfig(Position2D({9, 7}, 1.50), 0.8, 0.4, 0.3, 0.6)); + + auto cyber_demon = EnemyFactory::CreateEnemy( + "cyber_demon", + CharacterConfig(Position2D({23.0, 4}, 1.50), 0.8, 0.4, 0.5, 1.0)); + scene_->AddObject(caco_demon); scene_->AddObject(soldier); scene_->AddObject(cyber_demon); diff --git a/src/Core/src/scene.cpp b/src/Core/src/scene.cpp index 4de3d3c..45cd394 100644 --- a/src/Core/src/scene.cpp +++ b/src/Core/src/scene.cpp @@ -8,7 +8,6 @@ void Scene::AddObject(std::shared_ptr object) { objects.push_back(object); if (object->GetObjectType() == ObjectType::CHARACTER_ENEMY) { auto enemy = std::dynamic_pointer_cast(object); - enemy->Init(); enemies.push_back(enemy); } } @@ -23,10 +22,10 @@ void Scene::SetPlayer(std::shared_ptr player) { void Scene::Update(double delta_time) { player->Update(delta_time); - for (auto& enemy : enemies) { - enemy->SetNextPose(NavigationManager::GetInstance().FindPath( - enemy->GetPosition(), player->GetPosition(), enemy->GetId())); - } + // for (auto& enemy : enemies) { + // enemy->SetNextPose(NavigationManager::GetInstance().FindPath( + // enemy->GetPosition(), player->GetPosition(), enemy->GetId())); + // } for (auto& object : objects) { object->Update(delta_time); } diff --git a/src/State/include/State/enemy_state.h b/src/State/include/State/enemy_state.h index 81d8637..b4fb8ca 100644 --- a/src/State/include/State/enemy_state.h +++ b/src/State/include/State/enemy_state.h @@ -17,6 +17,10 @@ namespace wolfenstein { +namespace { +constexpr double kIdlePatrolDistance = 0.5; +} // namespace + class Enemy; typedef std::shared_ptr> EnemyStatePtr; @@ -27,39 +31,42 @@ struct StateType }; typedef StateType::Type EnemyStateType; +// ########################################### IdleState ########################################### class IdleState : public State { public: - IdleState(const std::string bot_name); + IdleState(); ~IdleState(); void Update(const double& delta_time) override; void Reset() override; + void OnContextSet() override; int GetCurrentFrame() const override; EnemyStateType GetType() const override; private: - std::string bot_name_; int current_frame; double counter; double interval; double animation_speed_; + double patrol_distance{0.25}; std::vector textures; }; +// ########################################### WalkState ########################################### class WalkState : public State { public: - WalkState(const std::string bot_name); + WalkState(); ~WalkState(); void Update(const double& delta_time) override; void Reset() override; + void OnContextSet() override; int GetCurrentFrame() const override; EnemyStateType GetType() const override; private: - std::string bot_name_; int current_frame; double counter; double interval; @@ -67,19 +74,20 @@ class WalkState : public State std::vector textures; }; +// ########################################### AttackState ########################################### class AttackState : public State { public: - AttackState(const std::string bot_name); + AttackState(); ~AttackState(); void Update(const double& delta_time) override; void Reset() override; + void OnContextSet() override; int GetCurrentFrame() const override; EnemyStateType GetType() const override; private: - std::string bot_name_; int current_frame; double counter; double interval; @@ -87,19 +95,20 @@ class AttackState : public State std::vector textures; }; +// ########################################### PainState ########################################### class PainState : public State { public: - PainState(const std::string bot_name); + PainState(); ~PainState(); void Update(const double& delta_time) override; void Reset() override; + void OnContextSet() override; int GetCurrentFrame() const override; EnemyStateType GetType() const override; private: - std::string bot_name_; int current_frame; double counter; double interval; @@ -107,19 +116,20 @@ class PainState : public State std::vector textures; }; +// ########################################### DeathState ########################################### class DeathState : public State { public: - DeathState(const std::string bot_name); + DeathState(); ~DeathState(); void Update(const double& delta_time) override; void Reset() override; + void OnContextSet() override; int GetCurrentFrame() const override; EnemyStateType GetType() const override; private: - std::string bot_name_; int current_frame; double counter; double interval; diff --git a/src/State/include/State/weapon_state.h b/src/State/include/State/weapon_state.h index f68b1e1..3632f43 100644 --- a/src/State/include/State/weapon_state.h +++ b/src/State/include/State/weapon_state.h @@ -28,10 +28,11 @@ struct StateType }; typedef StateType::Type WeaponStateType; +// ########################################### LoadedState ########################################### class LoadedState : public State { public: - LoadedState(const std::string weapon_name); + LoadedState(); ~LoadedState(); void Update(const double&) override; @@ -43,20 +44,20 @@ class LoadedState : public State private: void AttackAnimation(); - std::string weapon_name_; double last_attack_time_; double animation_speed_; bool cooldown_; bool interrupt_; bool destroyed_; WeaponStatePtr requested_state_; - std::shared_ptr animation_; + std::unique_ptr animation_; }; +// ########################################### OutOfAmmoState ########################################### class OutOfAmmoState : public State { public: - OutOfAmmoState(const std::string weapon_name); + OutOfAmmoState(); ~OutOfAmmoState(); void Update(const double&) override; @@ -68,20 +69,20 @@ class OutOfAmmoState : public State private: void NoAttackAnimation(); - std::string weapon_name_; double last_attack_time_; double animation_speed_; bool cooldown_; bool interrupt_; bool destroyed_; WeaponStatePtr requested_state_; - std::shared_ptr animation_; + std::unique_ptr animation_; }; +// ########################################### ReloadingState ########################################### class ReloadingState : public State { public: - ReloadingState(const std::string weapon_name); + ReloadingState(); ~ReloadingState(); void Update(const double&) override; @@ -92,13 +93,12 @@ class ReloadingState : public State private: void ReloadAnimation(); - std::string weapon_name_; double last_attack_time_; double animation_speed_; bool cooldown_; bool interrupt_; bool destroyed_; - std::shared_ptr animation_; + std::unique_ptr animation_; }; } // namespace wolfenstein diff --git a/src/State/src/enemy_state.cpp b/src/State/src/enemy_state.cpp index ec674a2..3d2d347 100644 --- a/src/State/src/enemy_state.cpp +++ b/src/State/src/enemy_state.cpp @@ -1,38 +1,11 @@ #include "State/enemy_state.h" #include "Characters/enemy.h" #include "TextureManager/texture_manager.h" -#include + namespace wolfenstein { -namespace { -const std::unordered_map> - enemy_data_{{"soldier", - {{"idle", "soldier_idle"}, - {"walk", "soldier_walk"}, - {"attack", "soldier_attack"}, - {"death", "soldier_death"}, - {"pain", "soldier_pain"}}}, - {"caco_demon", - {{"idle", "caco_demon_idle"}, - {"walk", "caco_demon_walk"}, - {"attack", "caco_demon_attack"}, - {"death", "caco_demon_death"}, - {"pain", "caco_demon_pain"}}}, - {"cyber_demon", - {{"idle", "cyber_demon_idle"}, - {"walk", "cyber_demon_walk"}, - {"attack", "cyber_demon_attack"}, - {"death", "cyber_demon_death"}, - {"pain", "cyber_demon_pain"}}}}; -} // namespace - -IdleState::IdleState(const std::string bot_name) - : bot_name_(bot_name), current_frame(0), counter(0), interval(0) { - animation_speed_ = 0.3; - textures = TextureManager::GetInstance().GetTextureCollection( - enemy_data_.at(bot_name_).at("idle")); -} +// ########################################### IdleState ########################################### +IdleState::IdleState() : current_frame(0), counter(0), interval(0) {} IdleState::~IdleState() {} @@ -44,7 +17,7 @@ void IdleState::Update(const double& delta_time) { } interval += delta_time; if (interval > 5) { - context_->TransitionTo(std::make_shared(bot_name_)); + context_->TransitionTo(std::make_shared()); } } @@ -53,6 +26,12 @@ void IdleState::Reset() { counter = 0; } +void IdleState::OnContextSet() { + animation_speed_ = 0.3; + textures = TextureManager::GetInstance().GetTextureCollection( + context_->GetBotName() + "_idle"); +} + int IdleState::GetCurrentFrame() const { return textures[current_frame]; } @@ -61,12 +40,8 @@ EnemyStateType IdleState::GetType() const { return EnemyStateType::Idle; } -WalkState::WalkState(const std::string bot_name) - : bot_name_(bot_name), current_frame(0), counter(0), interval(0) { - animation_speed_ = 0.3; - textures = TextureManager::GetInstance().GetTextureCollection( - enemy_data_.at(bot_name_).at("walk")); -} +// ########################################### WalkState ########################################### +WalkState::WalkState() : current_frame(0), counter(0), interval(0) {} WalkState::~WalkState() {} @@ -78,7 +53,7 @@ void WalkState::Update(const double& delta_time) { } interval += delta_time; if (interval > 5) { - context_->TransitionTo(std::make_shared(bot_name_)); + context_->TransitionTo(std::make_shared()); } } @@ -87,6 +62,12 @@ void WalkState::Reset() { counter = 0; } +void WalkState::OnContextSet() { + animation_speed_ = 0.3; + textures = TextureManager::GetInstance().GetTextureCollection( + context_->GetBotName() + "_walk"); +} + int WalkState::GetCurrentFrame() const { return textures[current_frame]; } @@ -95,12 +76,8 @@ EnemyStateType WalkState::GetType() const { return EnemyStateType::Walk; } -AttackState::AttackState(const std::string bot_name) - : bot_name_(bot_name), current_frame(0), counter(0), interval(0) { - animation_speed_ = 0.3; - textures = TextureManager::GetInstance().GetTextureCollection( - enemy_data_.at(bot_name_).at("attack")); -} +// ########################################### AttackState ########################################### +AttackState::AttackState() : current_frame(0), counter(0), interval(0) {} AttackState::~AttackState() {} @@ -112,7 +89,7 @@ void AttackState::Update(const double& delta_time) { } interval += delta_time; if (interval > 5) { - context_->TransitionTo(std::make_shared(bot_name_)); + context_->TransitionTo(std::make_shared()); } } @@ -121,6 +98,12 @@ void AttackState::Reset() { counter = 0; } +void AttackState::OnContextSet() { + animation_speed_ = 0.3; + textures = TextureManager::GetInstance().GetTextureCollection( + context_->GetBotName() + "_attack"); +} + int AttackState::GetCurrentFrame() const { return textures[current_frame]; } @@ -129,12 +112,8 @@ EnemyStateType AttackState::GetType() const { return EnemyStateType::Attack; } -PainState::PainState(const std::string bot_name) - : bot_name_(bot_name), current_frame(0), counter(0), interval(0) { - animation_speed_ = 0.3; - textures = TextureManager::GetInstance().GetTextureCollection( - enemy_data_.at(bot_name_).at("pain")); -} +// ########################################### PainState ########################################### +PainState::PainState() : current_frame(0), counter(0), interval(0) {} PainState::~PainState() {} @@ -146,7 +125,7 @@ void PainState::Update(const double& delta_time) { } interval += delta_time; if (interval > 5) { - context_->TransitionTo(std::make_shared(bot_name_)); + context_->TransitionTo(std::make_shared()); } } @@ -155,6 +134,12 @@ void PainState::Reset() { counter = 0; } +void PainState::OnContextSet() { + animation_speed_ = 0.3; + textures = TextureManager::GetInstance().GetTextureCollection( + context_->GetBotName() + "_pain"); +} + int PainState::GetCurrentFrame() const { return textures[current_frame]; } @@ -163,12 +148,8 @@ EnemyStateType PainState::GetType() const { return EnemyStateType::Pain; } -DeathState::DeathState(const std::string bot_name) - : bot_name_(bot_name), current_frame(0), counter(0), interval(0) { - animation_speed_ = 0.3; - textures = TextureManager::GetInstance().GetTextureCollection( - enemy_data_.at(bot_name_).at("death")); -} +// ########################################### DeathState ########################################### +DeathState::DeathState() : current_frame(0), counter(0), interval(0) {} DeathState::~DeathState() {} @@ -180,7 +161,7 @@ void DeathState::Update(const double& delta_time) { } interval += delta_time; if (interval > 5) { - context_->TransitionTo(std::make_shared(bot_name_)); + context_->TransitionTo(std::make_shared()); } } @@ -189,6 +170,12 @@ void DeathState::Reset() { counter = 0; } +void DeathState::OnContextSet() { + animation_speed_ = 0.3; + textures = TextureManager::GetInstance().GetTextureCollection( + context_->GetBotName() + "_death"); +} + int DeathState::GetCurrentFrame() const { return textures[current_frame]; } diff --git a/src/State/src/weapon_state.cpp b/src/State/src/weapon_state.cpp index 9b44f95..a0e8171 100644 --- a/src/State/src/weapon_state.cpp +++ b/src/State/src/weapon_state.cpp @@ -1,30 +1,16 @@ #include "State/weapon_state.h" +#include "Animation/time_based_single_animation.h" #include "State/state.h" #include "Strike/weapon.h" -#include "TextureManager/texture_manager.h" #include "TimeManager/time_manager.h" -#include #include #include -#include namespace wolfenstein { -namespace { - -const std::unordered_map> - weapon_data_{{"mp5", - {{"loaded", "mp5_loaded"}, - {"outofammo", "mp5_outofammo"}, - {"reload", "mp5_reload"}}}}; - -} - // ########################################### LoadedState ########################################### -LoadedState::LoadedState(const std::string weapon_name) - : weapon_name_(weapon_name), - last_attack_time_(0), +LoadedState::LoadedState() + : last_attack_time_(0), cooldown_(false), interrupt_(false), destroyed_(false) {} @@ -66,7 +52,7 @@ void LoadedState::AttackAnimation() { } if (context_->GetAmmo() == 0) { std::shared_ptr> out_of_ammo_state = - std::make_shared(weapon_name_); + std::make_shared(); context_->TransitionTo(out_of_ammo_state); } cooldown_ = false; @@ -78,10 +64,8 @@ void LoadedState::Reset() { void LoadedState::OnContextSet() { animation_speed_ = context_->GetAttackSpeed(); - auto textures = TextureManager::GetInstance().GetTextureCollection( - weapon_data_.at(weapon_name_).at("loaded")); - animation_ = std::make_shared( - textures, animation_speed_ / textures.size()); + animation_ = std::make_unique( + context_->GetWeaponName() + "_loaded", context_->GetAttackSpeed()); } void LoadedState::TransitionRequest(std::shared_ptr>& state) { @@ -100,9 +84,8 @@ int LoadedState::GetCurrentFrame() const { } // ########################################### OutOfAmmoState ########################################### -OutOfAmmoState::OutOfAmmoState(const std::string weapon_name) - : weapon_name_(weapon_name), - last_attack_time_(0), +OutOfAmmoState::OutOfAmmoState() + : last_attack_time_(0), cooldown_(false), interrupt_(false), destroyed_(false) {} @@ -151,10 +134,8 @@ void OutOfAmmoState::Reset() { void OutOfAmmoState::OnContextSet() { animation_speed_ = context_->GetAttackSpeed(); - auto textures = TextureManager::GetInstance().GetTextureCollection( - weapon_data_.at(weapon_name_).at("outofammo")); - animation_ = std::make_shared( - textures, animation_speed_ / textures.size()); + animation_ = std::make_unique( + context_->GetWeaponName() + "_outofammo", context_->GetAttackSpeed()); } void OutOfAmmoState::TransitionRequest(std::shared_ptr>& state) { @@ -173,9 +154,8 @@ int OutOfAmmoState::GetCurrentFrame() const { } // ########################################### ReloadingState ########################################### -ReloadingState::ReloadingState(const std::string weapon_name) - : weapon_name_(weapon_name), - last_attack_time_(0), +ReloadingState::ReloadingState() + : last_attack_time_(0), cooldown_(false), interrupt_(false), destroyed_(false) {} @@ -212,7 +192,7 @@ void ReloadingState::ReloadAnimation() { } context_->Charge(); std::shared_ptr> loaded_state = - std::make_shared(weapon_name_); + std::make_shared(); context_->TransitionTo(loaded_state); } @@ -222,10 +202,8 @@ void ReloadingState::Reset() { void ReloadingState::OnContextSet() { animation_speed_ = context_->GetReloadSpeed(); - auto textures = TextureManager::GetInstance().GetTextureCollection( - weapon_data_.at(weapon_name_).at("reload")); - animation_ = std::make_shared( - textures, animation_speed_ / textures.size()); + animation_ = std::make_unique( + context_->GetWeaponName() + "_reload", animation_speed_); } int ReloadingState::GetCurrentFrame() const { diff --git a/src/Strike/include/Strike/weapon.h b/src/Strike/include/Strike/weapon.h index ab5de29..657538c 100644 --- a/src/Strike/include/Strike/weapon.h +++ b/src/Strike/include/Strike/weapon.h @@ -31,7 +31,6 @@ struct WeaponConfig class Weapon : public IStrike, public std::enable_shared_from_this { public: - enum class StateType { Loaded, OutOfAmmo, Reloading }; Weapon(std::string weapon_name); ~Weapon(); void Init(); diff --git a/src/Strike/src/weapon.cpp b/src/Strike/src/weapon.cpp index 77720e2..3755503 100644 --- a/src/Strike/src/weapon.cpp +++ b/src/Strike/src/weapon.cpp @@ -19,7 +19,7 @@ auto GetWeaponConfig = [](const std::string& weapon_name) -> WeaponConfig { Weapon::Weapon(std::string weapon_name) : weapon_properties_(GetWeaponConfig(weapon_name)), ammo_(weapon_properties_.ammo_capacity) { - state_ = std::make_shared(weapon_properties_.weapon_name); + state_ = std::make_shared(); } Weapon::~Weapon() {} @@ -37,8 +37,7 @@ void Weapon::Charge() { } void Weapon::Reload() { - WeaponStatePtr reloading_state = - std::make_shared(weapon_properties_.weapon_name); + WeaponStatePtr reloading_state = std::make_shared(); state_->TransitionRequest(reloading_state); }