Skip to content

Commit

Permalink
Ft update managers (#14)
Browse files Browse the repository at this point in the history
* Update navigation manager

* Add simple_weapons for enemy and update shooting manager to calculate damage
  • Loading branch information
bilalkah authored Nov 30, 2024
1 parent 19dc60a commit e16a099
Show file tree
Hide file tree
Showing 18 changed files with 366 additions and 36 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
2 changes: 1 addition & 1 deletion src/Core/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ void Game::Init() {
map_ = std::make_shared<Map>();

CollisionManager::GetInstance().InitManager(map_);
NavigationManager::GetInstance().InitManager(map_);
SingleRayCasterService::GetInstance().InitService(map_);
scene_ = std::make_shared<Scene>();
scene_->SetMap(map_);
Expand Down Expand Up @@ -76,6 +75,7 @@ void Game::Init() {

ShootingManager::GetInstance().InitManager(map_, player_,
scene_->GetEnemies());
NavigationManager::GetInstance().InitManager(map_, scene_->GetEnemies());

is_running_ = true;
TimeManager::GetInstance().InitClock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef NAVIGATION_MANAGER_INCLUDE_NAVIGATION_MANAGER_NAVIGATION_MANAGER_H
#define NAVIGATION_MANAGER_INCLUDE_NAVIGATION_MANAGER_NAVIGATION_MANAGER_H

#include "Characters/character.h"
#include "Characters/enemy.h"
#include "Map/map.h"
#include "Math/vector.h"
#include "path-planning/planning/grid_base/astar/astar.h"
Expand All @@ -30,14 +30,16 @@ class NavigationManager
NavigationManager& operator=(const NavigationManager&) = delete;
~NavigationManager() = default;

void InitManager(std::shared_ptr<Map> map);
void InitManager(std::shared_ptr<Map> map,
std::vector<std::shared_ptr<Enemy>> enemies);
vector2d FindPath(Position2D start, Position2D end, std::string id);
vector2d FindPathToPlayer(Position2D start, std::string id);
std::vector<vector2d> GetPath(std::string id);
void ResetPath(std::string id);
void SubscribePlayerPosition(const Position2D& position);
double EuclideanDistanceToPlayer(const Position2D& position);
double ManhattanDistanceToPlayer(const Position2D& position);
void ApplyDynamicObjects(std::shared_ptr<planning::Map> path_map);

private:
NavigationManager() = default;
Expand All @@ -47,6 +49,7 @@ class NavigationManager
std::shared_ptr<planning::grid_base::AStar> path_planner_;
std::unordered_map<std::string, std::vector<vector2d>> paths_;
Position2D player_position_;
std::vector<std::shared_ptr<Enemy>> enemies_;
};

} // namespace wolfenstein
Expand Down
34 changes: 32 additions & 2 deletions src/NavigationManager/src/navigation_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "NavigationManager/navigation_manager.h"
#include "Characters/enemy.h"
#include "Map/map.h"
#include "Math/vector.h"
#include "NavigationManager/navigation_helper.h"
#include "common_planning.h"
#include <memory>
#include <string>
#include <vector>

Expand All @@ -15,9 +19,11 @@ NavigationManager& NavigationManager::GetInstance() {
return *instance_;
}

void NavigationManager::InitManager(std::shared_ptr<Map> map) {
void NavigationManager::InitManager(
std::shared_ptr<Map> map, std::vector<std::shared_ptr<Enemy>> enemies) {
map_ = map;
path_planner_ = std::make_shared<planning::grid_base::AStar>(0.6, 4);
enemies_ = enemies;
}

//@Note apply caching mechanism later
Expand All @@ -31,8 +37,14 @@ vector2d NavigationManager::FindPath(Position2D start, Position2D end,

planning::Node start_node = FromVector2d(start.pose / res);
planning::Node end_node = FromVector2d(end.pose / res);

std::shared_ptr path_map =
std::make_shared<planning::Map>(*(map_->GetPathFinderMap()));
ApplyDynamicObjects(path_map);
path_map->SetNodeState(start_node, planning::NodeState::kStart);
planning::Path path =
path_planner_->FindPath(start_node, end_node, map_->GetPathFinderMap());
path_planner_->FindPath(start_node, end_node, path_map);

if (path.empty()) {
paths_[id] = {start.pose};
return start.pose;
Expand Down Expand Up @@ -79,4 +91,22 @@ double NavigationManager::ManhattanDistanceToPlayer(
return player_position_.pose.MDistance(position.pose);
}

void NavigationManager::ApplyDynamicObjects(
std::shared_ptr<planning::Map> path_map) {
const auto res = map_->GetResolution();

for (const auto& e : enemies_) {
path_map->SetNodeState(FromVector2d(e->GetPose() / res),
planning::NodeState::kOccupied);
const auto found = paths_.find(e->GetId());
if (found != paths_.end()) {
if (found->second.size() > 0) {
path_map->SetNodeState(
FromVector2d(found->second.front() / res),
planning::NodeState::kOccupied);
}
}
}
}

} // namespace wolfenstein
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
Loading

0 comments on commit e16a099

Please sign in to comment.