Skip to content

Commit

Permalink
Add basic member functions to character (#12)
Browse files Browse the repository at this point in the history
* Add basic member functions to character
Remove bot and weapon name from states
Remove texture maps
Remove unnecessary dependencies

* Fix threading problem and make weapon states synchronized with game clock

* Remove hardcoded style from texture manager

* Add middle class for enemy state for enemy specific members

* Update animation

* Create single ray caster service for bots

* Add object id field in ray

* Move camera into player instead of having them separate

* Create shooting manager

* Update enemy states and use shooting logic
  • Loading branch information
bilalkah authored Nov 27, 2024
1 parent f79930a commit b2d7d94
Show file tree
Hide file tree
Showing 40 changed files with 1,164 additions and 619 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ add_subdirectory(src/Graphics)
add_subdirectory(src/Map)
add_subdirectory(src/Math)
add_subdirectory(src/NavigationManager)
add_subdirectory(src/ShootingManager)
add_subdirectory(src/Strike)
add_subdirectory(src/State)
add_subdirectory(src/TextureManager)
Expand Down
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
)

1 change: 1 addition & 0 deletions src/Animation/include/Animation/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class IAnimation
virtual void Update(const double& delta_time) = 0;
virtual void Reset() = 0;
virtual int GetCurrentFrame() const = 0;
virtual bool IsAnimationFinishedOnce() const = 0;
};

} // namespace wolfenstein
Expand Down
6 changes: 6 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,18 +24,23 @@ 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;
void Reset() override;

int GetCurrentFrame() const override;
bool IsAnimationFinishedOnce() const override;

private:
std::vector<uint16_t> textures;
int textures_size;
int current_frame;
double animation_speed;
double counter;
bool is_animation_finished_once;
};

} // namespace wolfenstein
Expand Down
27 changes: 23 additions & 4 deletions src/Animation/src/time_based_single_animation.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
#include "Animation/time_based_single_animation.h"

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

TBSAnimation::TBSAnimation(const std::vector<uint16_t>& textures,
const double animation_speed)
: textures(textures),
textures_size(textures.size()),
current_frame(0),
animation_speed(animation_speed),
counter(0) {}
counter(0),
is_animation_finished_once(false) {}

TBSAnimation::TBSAnimation(const std::string collection_name,
const double animation_speed)
: textures(
TextureManager::GetInstance().GetTextureCollection(collection_name)),
textures_size(textures.size()),
current_frame(0),
animation_speed(animation_speed / textures_size),
counter(0),
is_animation_finished_once(false) {}

void TBSAnimation::Update(const double& delta_time) {
counter += delta_time;
if (counter > animation_speed) {
current_frame = (current_frame + 1) % textures.size();
if (counter >= animation_speed) {
current_frame = (current_frame + 1) % textures_size;
counter = 0;
if (!is_animation_finished_once && current_frame == textures_size - 1) {
is_animation_finished_once = true;
}
}
}

Expand All @@ -26,4 +41,8 @@ int TBSAnimation::GetCurrentFrame() const {
return textures[current_frame];
}

bool TBSAnimation::IsAnimationFinishedOnce() const {
return is_animation_finished_once;
}

} // namespace wolfenstein
21 changes: 17 additions & 4 deletions src/Camera/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,22 @@ target_link_libraries(
PUBLIC
game_object
ray
character
vector
map
scene
texture_manager
vector
)

add_library(
single_raycaster
STATIC
src/single_raycaster.cpp
)
target_include_directories(single_raycaster PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(
single_raycaster
PUBLIC
ray
vector
map
)
13 changes: 8 additions & 5 deletions src/Camera/include/Camera/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@

#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 All @@ -28,20 +24,26 @@ namespace wolfenstein {

struct Camera2DConfig
{
Camera2DConfig(int width, double fov, double depth)
: width(width), fov(fov), depth(depth) {}

int width;
double fov;
double depth;
};

typedef std::pair<Ray, Ray> RayPair;

class Scene;
class Camera2D
{
public:
explicit Camera2D(const Camera2DConfig& config);
explicit Camera2D(const Camera2DConfig& config,
const std::shared_ptr<Scene>& scene);
~Camera2D() = default;

void Update(const std::shared_ptr<Scene>& scene);
void Update();

std::shared_ptr<RayVector> GetRays() const;
std::shared_ptr<Ray> GetCrosshairRay() const;
Expand All @@ -58,6 +60,7 @@ class Camera2D
double WorldAngleToCameraAngle(double angle) const;

Camera2DConfig config_;
std::shared_ptr<Scene> scene_;
Position2D position_;
std::shared_ptr<RayCaster> ray_cast_;
std::shared_ptr<RayVector> rays_;
Expand Down
2 changes: 2 additions & 0 deletions src/Camera/include/Camera/ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "Math/vector.h"

#include <string>
#include <vector>

namespace wolfenstein {
Expand All @@ -35,6 +36,7 @@ struct Ray
double perpendicular_distance;

int wall_id;
std::string object_id;

bool is_hit;
bool is_hit_vertical;
Expand Down
50 changes: 50 additions & 0 deletions src/Camera/include/Camera/single_raycaster.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @file single_raycaster.h
* @author Bilal Kahraman ([email protected])
* @brief
* @version 0.1
* @date 2024-11-05
*
* @copyright Copyright (c) 2024
*
*/

#ifndef CAMERA_INCLUDE_CAMERA_SINGLE_RAYCASTER_H_
#define CAMERA_INCLUDE_CAMERA_SINGLE_RAYCASTER_H_

#include "Camera/ray.h"
#include "Map/map.h"
#include "Math/vector.h"

#include <memory>

namespace wolfenstein {

class SingleRayCasterService
{
public:
static SingleRayCasterService& GetInstance();
SingleRayCasterService(const SingleRayCasterService&) = delete;
SingleRayCasterService& operator=(const SingleRayCasterService&) = delete;
~SingleRayCasterService() = default;

void InitService(const std::shared_ptr<Map>& map_ptr);
Ray Cast(const vector2d& src);

void SubscribePlayerPose(const vector2d& pose);

private:
SingleRayCasterService() = default;

void PrepareRay(const vector2d& src, Ray& ray, vector2d& ray_unit_step,
vector2d& ray_length_1d, vector2i& step,
vector2i& map_check);

vector2d dest{0, 0};
std::shared_ptr<Map> map_ptr_;
static SingleRayCasterService* instance_;
};

} // namespace wolfenstein

#endif // CAMERA_INCLUDE_CAMERA_SINGLE_RAYCASTER_H_
27 changes: 19 additions & 8 deletions src/Camera/src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

#include "Camera/camera.h"
#include "Camera/ray.h"
#include "Core/scene.h"
#include "Math/vector.h"
#include "TextureManager/texture_manager.h"

#include <cmath>
#include <memory>
#include <string>

namespace wolfenstein {
Expand All @@ -26,23 +27,24 @@ void Camera2D::InitRays() {
}
}

Camera2D::Camera2D(const Camera2DConfig& config)
Camera2D::Camera2D(const Camera2DConfig& config,
const std::shared_ptr<Scene>& scene)
: config_(config),
scene_(scene),
ray_cast_(std::make_shared<RayCaster>(config.width / 2, config.fov,
config.depth)) {
InitRays();
}

void Camera2D::Update(const std::shared_ptr<Scene>& scene) {
ray_cast_->Update(scene->GetMap(), position_, rays_);
void Camera2D::Update() {
ray_cast_->Update(scene_->GetMap(), position_, rays_);
crosshair_ray_ = std::make_shared<Ray>(rays_->at(config_.width / 4));
crosshair_ray_->is_hit = false;

// Update object rays
objects_.clear();
for (const auto& object : scene->GetObjects()) {
if (object->GetObjectType() != ObjectType::CHARACTER_PLAYER) {
Calculate(object);
}
for (const auto& object : scene_->GetObjects()) {
Calculate(object);
}
}

Expand Down Expand Up @@ -135,6 +137,15 @@ void Camera2D::Calculate(const std::shared_ptr<IGameObject>& object) {
object_ray_pair.second.wall_id = texture_id;

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;
}
}

double Camera2D::WorldAngleToCameraAngle(double angle) const {
Expand Down
3 changes: 3 additions & 0 deletions src/Camera/src/ray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Ray::Ray()
distance(0),
perpendicular_distance(0),
wall_id(0),
object_id(""),
is_hit(false),
is_hit_vertical(false) {}

Expand All @@ -23,6 +24,7 @@ Ray::Ray(vector2d direction_, double theta_)
distance(0),
perpendicular_distance(0),
wall_id(0),
object_id(""),
is_hit(false),
is_hit_vertical(false) {}

Expand All @@ -37,6 +39,7 @@ void Ray::Reset(const vector2d ray_orig, const double ray_theta) {
distance = 0;
perpendicular_distance = 0;
wall_id = 0;
object_id = "";
is_hit = false;
is_hit_vertical = false;
}
Expand Down
Loading

0 comments on commit b2d7d94

Please sign in to comment.