Skip to content

Commit

Permalink
Add basic member functions to character
Browse files Browse the repository at this point in the history
Remove bot and weapon name from states
Remove texture maps
Remove unnecessary dependencies
  • Loading branch information
bilalkah committed Sep 27, 2024
1 parent f79930a commit 8036e25
Show file tree
Hide file tree
Showing 21 changed files with 206 additions and 164 deletions.
5 changes: 5 additions & 0 deletions src/Animation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ add_library(
target_include_directories(animation PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(
animation
PUBLIC
texture_manager
)

3 changes: 3 additions & 0 deletions src/Animation/include/Animation/time_based_single_animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "Animation/animation.h"
#include <cstdint>
#include <string>
#include <vector>

namespace wolfenstein {
Expand All @@ -23,6 +24,8 @@ class TBSAnimation : public IAnimation
public:
TBSAnimation(const std::vector<uint16_t>& textures,
const double animation_speed);
TBSAnimation(const std::string collection_name,
const double animation_speed);
~TBSAnimation() = default;

void Update(const double& delta_time) override;
Expand Down
10 changes: 9 additions & 1 deletion src/Animation/src/time_based_single_animation.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Animation/time_based_single_animation.h"

#include "TextureManager/texture_manager.h"
namespace wolfenstein {

TBSAnimation::TBSAnimation(const std::vector<uint16_t>& textures,
Expand All @@ -9,6 +9,14 @@ TBSAnimation::TBSAnimation(const std::vector<uint16_t>& 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) {
Expand Down
5 changes: 1 addition & 4 deletions src/Camera/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ target_link_libraries(
PUBLIC
game_object
ray
character
vector
map
scene
texture_manager
vector
)
3 changes: 0 additions & 3 deletions src/Camera/include/Camera/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <memory>
#include <optional>
Expand Down
1 change: 0 additions & 1 deletion src/Camera/src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "Camera/camera.h"
#include "Camera/ray.h"
#include "Math/vector.h"
#include "TextureManager/texture_manager.h"

#include <cmath>
#include <string>
Expand Down
8 changes: 4 additions & 4 deletions src/Characters/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

5 changes: 5 additions & 0 deletions src/Characters/include/Characters/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ struct CharacterConfig
Position2D initial_position;
double translation_speed;
double rotation_speed;
double width;
double height;
};

class ICharacter
Expand All @@ -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

Expand Down
24 changes: 20 additions & 4 deletions src/Characters/include/Characters/enemy.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,62 @@
#include "Math/vector.h"
#include "State/enemy_state.h"
#include <memory>
#include <string>

namespace wolfenstein {

class EnemyFactory;
class Enemy : public ICharacter,
public IGameObject,
public std::enable_shared_from_this<Enemy>
{
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<Enemy> CreateEnemy(std::string bot_name,
CharacterConfig config);
};

} // namespace wolfenstein
Expand Down
4 changes: 4 additions & 0 deletions src/Characters/include/Characters/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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> weapon_;
std::vector<std::function<void(Position2D)>> player_position_subscribers_;
Expand Down
41 changes: 30 additions & 11 deletions src/Characters/src/enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_;
}
Expand All @@ -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();
Expand Down Expand Up @@ -85,4 +97,11 @@ double Enemy::GetHeight() const {
return height;
}

std::shared_ptr<Enemy> EnemyFactory::CreateEnemy(std::string bot_name,
CharacterConfig config) {
auto enemy_ptr = std::make_shared<Enemy>(bot_name, config);
enemy_ptr->TransitionTo(std::make_shared<IdleState>());
return enemy_ptr;
}

} // namespace wolfenstein
15 changes: 14 additions & 1 deletion src/Characters/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Weapon>("mp5");
WeaponStatePtr loaded_state = std::make_shared<LoadedState>("mp5");
WeaponStatePtr loaded_state = std::make_shared<LoadedState>();
weapon_->TransitionTo(loaded_state);
}

Expand Down Expand Up @@ -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_;
}
Expand Down
1 change: 1 addition & 0 deletions src/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_link_libraries(
map
navigation_manager
collision_manager
character
)

add_library(
Expand Down
25 changes: 14 additions & 11 deletions src/Core/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ void Game::Init() {
config_.view_distance};
camera_ = std::make_shared<Camera2D>(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>(player_config);

player_->SubscribeToPlayerPosition(std::bind(
Expand Down Expand Up @@ -116,16 +117,18 @@ void Game::Run() {
}

void Game::PrepareEnemies() {
auto caco_demon = std::make_shared<Enemy>(
CharacterConfig(Position2D({13.5, 2.5}, 1.50), 0.8, 0.4),
std::make_shared<IdleState>("caco_demon"), 0.4, 0.8);

auto soldier = std::make_shared<Enemy>(
CharacterConfig(Position2D({9, 7}, 1.50), 0.8, 0.4),
std::make_shared<IdleState>("soldier"), 0.3, 0.6);
auto cyber_demon = std::make_shared<Enemy>(
CharacterConfig(Position2D({23.0, 4}, 1.50), 0.8, 0.4),
std::make_shared<IdleState>("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);
Expand Down
9 changes: 4 additions & 5 deletions src/Core/src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ void Scene::AddObject(std::shared_ptr<IGameObject> object) {
objects.push_back(object);
if (object->GetObjectType() == ObjectType::CHARACTER_ENEMY) {
auto enemy = std::dynamic_pointer_cast<Enemy>(object);
enemy->Init();
enemies.push_back(enemy);
}
}
Expand All @@ -23,10 +22,10 @@ void Scene::SetPlayer(std::shared_ptr<Player> 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);
}
Expand Down
Loading

0 comments on commit 8036e25

Please sign in to comment.