Skip to content

Commit

Permalink
Add simple_weapons for enemy and update shooting manager to calculate…
Browse files Browse the repository at this point in the history
… damage
  • Loading branch information
bilalkah committed Nov 30, 2024
1 parent 003b605 commit 8ae016e
Show file tree
Hide file tree
Showing 15 changed files with 328 additions and 31 deletions.
22 changes: 16 additions & 6 deletions src/Camera/src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

#include "Camera/camera.h"
#include "Camera/ray.h"
#include "Characters/enemy.h"
#include "Core/scene.h"
#include "GameObjects/game_object.h"
#include "Math/vector.h"

#include <cmath>
Expand Down Expand Up @@ -139,12 +141,20 @@ void Camera2D::Calculate(const std::shared_ptr<IGameObject>& object) {
objects_[object->GetId()] = object_ray_pair;

// Calculate if the object is in the crosshair
if (object_distance < crosshair_ray_->perpendicular_distance &&
camera_angle_left <= 0 && camera_angle_right >= 0) {
crosshair_ray_->is_hit = true;
crosshair_ray_->perpendicular_distance = object_distance;
crosshair_ray_->object_id = object->GetId();
crosshair_ray_->hit_point = object_pose;
if (object->GetObjectType() == ObjectType::CHARACTER_ENEMY) {
const auto bot = std::dynamic_pointer_cast<Enemy>(object);
if (object_distance < crosshair_ray_->perpendicular_distance &&
camera_angle_left <= 0 && camera_angle_right >= 0 &&
bot->IsAlive()) {
crosshair_ray_->is_hit = true;
crosshair_ray_->perpendicular_distance =
(object_ray_pair.first.perpendicular_distance +
object_ray_pair.second.perpendicular_distance) /
2;
crosshair_ray_->distance = object_distance;
crosshair_ray_->object_id = bot->GetId();
crosshair_ray_->hit_point = object_pose;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Characters/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ target_link_libraries(
weapon
camera
single_raycaster
simple_weapon
)

8 changes: 6 additions & 2 deletions src/Characters/include/Characters/enemy.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "GameObjects/game_object.h"
#include "Math/vector.h"
#include "State/enemy_state.h"
#include "Strike/simple_weapon.h"
#include "Strike/strike.h"
#include <memory>
#include <string>
namespace wolfenstein {
Expand All @@ -31,8 +33,6 @@ struct StateConfig
AnimationTime animation_time;
double follow_range_max;
double follow_range_min;
double attack_range;
double attack_rate;
};

class EnemyFactory;
Expand All @@ -47,6 +47,8 @@ class Enemy : public ICharacter,
void TransitionTo(EnemyStatePtr state);
bool IsPlayerInShootingRange() const;
bool IsAttacked() const;
bool IsAlive() const;
void Shoot();

void SetNextPose(vector2d pose);
void SetAttacked(bool value);
Expand All @@ -67,6 +69,7 @@ class Enemy : public ICharacter,
double GetHeight() const override;
StateConfig GetStateConfig() const;
Ray GetCrosshairRay() const;
std::shared_ptr<SimpleWeapon> GetWeapon() const;

friend class EnemyFactory;

Expand All @@ -86,6 +89,7 @@ class Enemy : public ICharacter,
vector2d next_pose;
StateConfig state_config_;
EnemyStatePtr state_;
std::shared_ptr<SimpleWeapon> weapon_;
Ray crosshair_ray;
bool is_attacked_;
bool is_alive_;
Expand Down
38 changes: 35 additions & 3 deletions src/Characters/src/enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "Camera/single_raycaster.h"
#include "CollisionManager/collision_manager.h"
#include "Math/vector.h"
#include "Strike/simple_weapon.h"
#include "Strike/strike.h"
#include "Utility/uuid_generator.h"
#include <memory>

Expand All @@ -11,13 +13,29 @@ namespace wolfenstein {
namespace {
auto GetBotStateConfig = [](const std::string& bot_name) -> StateConfig {
if (bot_name == "soldier") {
return {{0.8}, 5.0, 1.0, 5.0, 1.0};
return {{0.8}, 5.0, 1.0};
}
else if (bot_name == "caco_demon") {
return {{0.8}, 5.0, 1.0, 5.0, 1.0};
return {{0.8}, 5.0, 1.0};
}
else if (bot_name == "cyber_demon") {
return {{0.8}, 5.0, 1.0, 5.0, 1.0};
return {{0.8}, 5.0, 1.0};
}
else {
throw std::invalid_argument("Invalid bot name");
}
};

auto GetBotWeapon =
[](const std::string& bot_name) -> std::shared_ptr<SimpleWeapon> {
if (bot_name == "soldier") {
return std::make_shared<Rifle>();
}
else if (bot_name == "caco_demon") {
return std::make_shared<Melee>();
}
else if (bot_name == "cyber_demon") {
return std::make_shared<LaserGun>();
}
else {
throw std::invalid_argument("Invalid bot name");
Expand All @@ -35,6 +53,7 @@ Enemy::Enemy(std::string bot_name, CharacterConfig config)
id_(UuidGenerator::GetInstance().GenerateUuid().bytes()),
next_pose(position_.pose),
state_config_(GetBotStateConfig(bot_name)),
weapon_(GetBotWeapon(bot_name)),
crosshair_ray(Ray{}),
is_attacked_(false),
is_alive_(true) {}
Expand All @@ -52,11 +71,20 @@ bool Enemy::IsAttacked() const {
return is_attacked_;
}

bool Enemy::IsAlive() const {
return is_alive_;
}

void Enemy::Shoot() {
weapon_->Attack();
}

void Enemy::Update(double delta_time) {
if (!is_alive_) {
return;
}
crosshair_ray = SingleRayCasterService::GetInstance().Cast(position_.pose);
weapon_->SetCrosshairRay(crosshair_ray);
state_->Update(delta_time);
if (next_pose != position_.pose) {
Move(delta_time);
Expand Down Expand Up @@ -149,6 +177,10 @@ Ray Enemy::GetCrosshairRay() const {
return crosshair_ray;
}

std::shared_ptr<SimpleWeapon> Enemy::GetWeapon() const {
return weapon_;
}

std::shared_ptr<Enemy> EnemyFactory::CreateEnemy(std::string bot_name,
CharacterConfig config) {
auto enemy_ptr = std::make_shared<Enemy>(bot_name, config);
Expand Down
1 change: 1 addition & 0 deletions src/Characters/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void Player::Update(double delta_time) {
subscriber(position_);
}
camera_->Update();
weapon_->SetCrossHair(camera_->GetCrosshairRay());
}

void Player::SetPose(const vector2d& pose) {
Expand Down
47 changes: 47 additions & 0 deletions src/ShootingManager/include/ShootingManager/shooting_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @file shooting_helper.h
* @author Bilal Kahraman ([email protected])
* @brief
* @version 0.1
* @date 2024-11-28
*
* @copyright Copyright (c) 2024
*
*/

#ifndef SHOOTING_MANAGER_INCLUDE_SHOOTING_MANAGER_SHOOTING_HELPER_H
#define SHOOTING_MANAGER_INCLUDE_SHOOTING_MANAGER_SHOOTING_HELPER_H

#include <cmath>

namespace wolfenstein {

inline double LinearSlope(const std::pair<double, double> damage_limits,
const double range, const double distance) {
const auto linear_slope_formula =
[](const std::pair<double, double> damage_limits, const double range,
const double distance) {
return ((range - distance) / range) *
(damage_limits.first - damage_limits.second) +
damage_limits.second;
};
return linear_slope_formula(damage_limits, range, distance);
}

inline double ExponentialSlope(const std::pair<double, double> damage_limits,
const double range, const double distance) {

const auto exp_slope_formula =
[](const std::pair<double, double> damage_limits, const double range,
const double distance) {
return std::fmin(std::fmax(std::exp((range - distance) / 1.5),
damage_limits.second),
damage_limits.first);
};

return exp_slope_formula(damage_limits, range, distance);
}

} // namespace wolfenstein

#endif // SHOOTING_MANAGER_INCLUDE_SHOOTING_MANAGER_SHOOTING_HELPER_H
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "Characters/player.h"
#include "Math/vector.h"
#include "NavigationManager/navigation_manager.h"
#include "Strike/simple_weapon.h"
#include "Strike/strike.h"
#include <memory>
#include <vector>

Expand All @@ -33,12 +35,12 @@ class ShootingManager

void InitManager(std::shared_ptr<Map> map, std::shared_ptr<Player> player,
std::vector<std::shared_ptr<Enemy>> enemies);
void PlayerShoot();
void EnemyShoot();
void PlayerShoot(const std::shared_ptr<Weapon> weapon);
void EnemyShoot(const std::shared_ptr<SimpleWeapon> weapon);

private:
ShootingManager() = default;
double CalculateDamage(const std::shared_ptr<Enemy> enemy);
double CalculateDamage(const std::shared_ptr<Weapon> weapon);

static ShootingManager* instance_;
std::shared_ptr<Player> player_;
Expand Down
40 changes: 32 additions & 8 deletions src/ShootingManager/src/shooting_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "ShootingManager/shooting_manager.h"
#include "Math/vector.h"
#include "ShootingManager/shooting_helper.h"
#include "Strike/simple_weapon.h"
#include "Strike/weapon.h"
#include <memory>

namespace wolfenstein {

Expand All @@ -20,13 +24,17 @@ void ShootingManager::InitManager(std::shared_ptr<Map> map,
enemies_ = enemies;
}

void ShootingManager::PlayerShoot() {
void ShootingManager::PlayerShoot(const std::shared_ptr<Weapon> weapon) {
auto enemy = std::find_if(
enemies_.begin(), enemies_.end(), [this](const auto& enemy) {
return enemy->GetId() == player_->GetCrosshairRay()->object_id;
});
if (enemy != enemies_.end()) {
(*enemy)->DecreaseHealth(CalculateDamage(*enemy));
const auto damage = CalculateDamage(weapon);
if (damage == 0) {
return;
}
(*enemy)->DecreaseHealth(damage);
(*enemy)->SetAttacked(true);
std::cout << "Enemy Name: " << (*enemy)->GetBotName()
<< " Enemy health: " << (*enemy)->GetHealth() << std::endl;
Expand All @@ -38,14 +46,30 @@ void ShootingManager::PlayerShoot() {
}
}

void ShootingManager::EnemyShoot() {
std::cout << "Enemy shoot" << std::endl;
player_->DecreaseHealth(10);
std::cout << "Player health: " << player_->GetHealth() << std::endl;
void ShootingManager::EnemyShoot(const std::shared_ptr<SimpleWeapon> weapon) {
std::cout << "Enemy shoot with " << weapon->GetWeaponName() << std::endl;
const auto damage =
LinearSlope(weapon->GetAttackDamage(), weapon->GetAttackRange(),
weapon->GetCrosshair().distance);
player_->DecreaseHealth(damage);
std::cout << "Damage dealt: " << damage
<< " Player health: " << player_->GetHealth() << std::endl;
}

double ShootingManager::CalculateDamage(const std::shared_ptr<Enemy> enemy) {
return 10;
double ShootingManager::CalculateDamage(const std::shared_ptr<Weapon> weapon) {
if (weapon->GetCrosshair()->distance > weapon->GetAttackRange()) {
return 0;
}
if (weapon->GetWeaponName() == "mp5") {
return LinearSlope(weapon->GetAttackDamage(), weapon->GetAttackRange(),
weapon->GetCrosshair()->distance);
}
if (weapon->GetWeaponName() == "shotgun") {
return ExponentialSlope(weapon->GetAttackDamage(),
weapon->GetAttackRange(),
weapon->GetCrosshair()->distance);
}
return 0;
}

} // namespace wolfenstein
6 changes: 5 additions & 1 deletion src/State/src/enemy_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ void WalkState::Reset() {
}

void WalkState::OnContextSet() {
attack_rate_ = context_->GetWeapon()->GetAttackRate();
attack_range_ = context_->GetWeapon()->GetAttackRange();
animation_ = std::make_unique<TBSAnimation>(
context_->GetBotName() + "_walk", animation_speed_);
}
Expand All @@ -121,7 +123,7 @@ AttackState::~AttackState() {}
void AttackState::Update(const double& delta_time) {
animation_->Update(delta_time);
if (attack_counter_ == 0.0) {
ShootingManager::GetInstance().EnemyShoot();
context_->Shoot();
}
if (context_->IsAttacked()) {
context_->TransitionTo(std::make_shared<PainState>());
Expand All @@ -139,6 +141,7 @@ void AttackState::Reset() {
}

void AttackState::OnContextSet() {
animation_speed_ = context_->GetWeapon()->GetAttackSpeed();
animation_ = std::make_unique<TBSAnimation>(
context_->GetBotName() + "_attack", animation_speed_);
}
Expand Down Expand Up @@ -197,6 +200,7 @@ void DeathState::Update(const double& delta_time) {
counter += delta_time;
}
else {
NavigationManager::GetInstance().ResetPath(context_->GetId());
context_->SetDeath();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/State/src/weapon_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void LoadedState::PullTrigger() {
trigger_pulled_ = true;
trigger_pull_time_ = 0;
context_->DecreaseAmmo();
ShootingManager::GetInstance().PlayerShoot();
ShootingManager::GetInstance().PlayerShoot(context_);
}

// ########################################### OutOfAmmoState ###########################################
Expand Down
14 changes: 14 additions & 0 deletions src/Strike/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ target_link_libraries(
weapon_state
)

add_library(
simple_weapon
STATIC
src/simple_weapon.cpp
)
target_include_directories(simple_weapon PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(
simple_weapon
PUBLIC
shooting_manager
ray
)
Loading

0 comments on commit 8ae016e

Please sign in to comment.