Skip to content

Commit

Permalink
Add shotgun with animation
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalkah committed Aug 29, 2024
1 parent 3a9297c commit c776729
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 10 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/Strike)
add_subdirectory(src/TextureManager)
add_subdirectory(src/TimeManager)
add_subdirectory(src/Utility)
Expand Down
2 changes: 1 addition & 1 deletion app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

int main() {
using namespace wolfenstein;
GeneralConfig config(2560, 1440, 0, 50, 120, 10.0, ToRadians(60.0), true);
GeneralConfig config(800, 600, 0, 50, 120, 10.0, ToRadians(60.0), false);

Game game(config);
game.Run();
Expand Down
1 change: 1 addition & 0 deletions src/Characters/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ target_link_libraries(
collision_manager
animation
SDL2
shotgun
)

2 changes: 1 addition & 1 deletion src/Characters/include/Characters/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef CHARACTERS_INCLUDE_CHARACTER_H
#define CHARACTERS_INCLUDE_CHARACTER_H

#include "GameObjects/game_object.h"
#include "Math/vector.h"

namespace wolfenstein {

Expand Down
1 change: 1 addition & 0 deletions src/Characters/include/Characters/enemy.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "Animation/walk_animation.h"
#include "Characters/character.h"
#include "GameObjects/game_object.h"
#include "Math/vector.h"

namespace wolfenstein {
Expand Down
7 changes: 6 additions & 1 deletion src/Characters/include/Characters/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
#define CHARACTERS_PLAYER_H

#include "Characters/character.h"
#include "GameObjects/game_object.h"
#include "Strike/shotgun.h"
#include <functional>
#include <memory>

namespace wolfenstein {

Expand All @@ -32,19 +35,21 @@ class Player : public ICharacter, public IGameObject
void SetPosition(Position2D position) override;
Position2D GetPosition() const override;
std::string GetId() const override;
int GetTextureId() const override { return -1; }
int GetTextureId() const override { return shotgun_->GetTextureId(); }
double GetWidth() const override { return -1; }
double GetHeight() const override { return -1; }
void SubscribeToPlayerPosition(std::function<void(Position2D)> subscriber);

private:
void Move(double delta_time);
void Rotate(double delta_time);
void Shoot();

Position2D position_;
double rotation_speed_;
double translation_speed_;
std::string id_;
std::shared_ptr<Shotgun> shotgun_;
std::vector<std::function<void(Position2D)>> player_position_subscribers_;
};

Expand Down
8 changes: 8 additions & 0 deletions src/Characters/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ Player::Player(CharacterConfig& config)
rotation_speed_(config.rotation_speed),
translation_speed_(config.translation_speed) {
id_ = UuidGenerator::GetInstance().GenerateUuid().bytes();
shotgun_ = std::make_shared<Shotgun>();
}

void Player::Update(double delta_time) {
Shoot();
Move(delta_time);
Rotate(delta_time);
for (auto& subscriber : player_position_subscribers_) {
Expand Down Expand Up @@ -99,4 +101,10 @@ void Player::Rotate(double delta_time) {
}
}

void Player::Shoot() {
if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON_LMASK) {
shotgun_->Attack();
}
}

} // namespace wolfenstein
1 change: 0 additions & 1 deletion src/Core/include/Core/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class Game

std::shared_ptr<Renderer> renderer_;
std::shared_ptr<Scene> scene_;
std::shared_ptr<TimeManager> time_manager_;
std::shared_ptr<Camera2D> camera_;
std::shared_ptr<Player> player_;
std::shared_ptr<Map> map_;
Expand Down
9 changes: 4 additions & 5 deletions src/Core/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Graphics/renderer.h"
#include "Math/vector.h"
#include "NavigationManager/navigation_manager.h"
#include "TimeManager/time_manager.h"
#include <SDL2/SDL_video.h>
#include <functional>
#include <vector>
Expand All @@ -32,8 +33,6 @@ void Game::Init() {
scene_ = std::make_shared<Scene>();
scene_->SetMap(map_);

time_manager_ = std::make_shared<TimeManager>();

RenderConfig render_config = {config_.screen_width, config_.screen_height,
config_.padding, config_.scale,
config_.fps, config_.view_distance,
Expand Down Expand Up @@ -89,7 +88,7 @@ void Game::Init() {
vector2d(12.1, 10.9),
std::make_shared<TBSAnimation>(animation_green_light), 0.2, 0.9));
is_running_ = true;
time_manager_->InitClock();
TimeManager::GetInstance().InitClock();
}

void Game::CheckEvent() {
Expand Down Expand Up @@ -130,8 +129,8 @@ void Game::CheckEvent() {
void Game::Run() {
while (is_running_) {
CheckEvent();
time_manager_->CalculateDeltaTime();
scene_->Update(time_manager_->GetDeltaTime());
TimeManager::GetInstance().CalculateDeltaTime();
scene_->Update(TimeManager::GetInstance().GetDeltaTime());
camera_->Update(scene_);
switch (render_type_) {
case RenderType::TEXTURE:
Expand Down
2 changes: 2 additions & 0 deletions src/Graphics/include/Graphics/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class Renderer
int CalculateHorizontalSlice(const double& angle,
const std::shared_ptr<Camera2D> camera_ptr);
std::tuple<int, int, int> CalculateVerticalSlice(const double& distance);
void RenderWeapon(const std::shared_ptr<Player>& player_ptr,
RenderQueue& render_queue);
void RenderTextures(RenderQueue& render_queue);

// Render 2D
Expand Down
17 changes: 17 additions & 0 deletions src/Graphics/src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void Renderer::RenderScene(const std::shared_ptr<Scene>& scene_ptr,
RenderBackground();
RenderWalls(scene_ptr->GetMap(), camera_ptr, render_queue);
RenderObjects(scene_ptr->GetObjects(), camera_ptr, render_queue);
RenderWeapon(scene_ptr->GetPlayer(), render_queue);
RenderTextures(render_queue);
SDL_RenderPresent(renderer_);
}
Expand Down Expand Up @@ -196,6 +197,22 @@ std::tuple<int, int, int> Renderer::CalculateVerticalSlice(
return std::make_tuple(line_height, draw_start, draw_end);
}

void Renderer::RenderWeapon(const std::shared_ptr<Player>& player_ptr,
RenderQueue& render_queue) {

auto texture_id = player_ptr->GetTextureId();
const auto texture_height =
TextureManager::GetInstance().GetTexture(texture_id).height;
const auto texture_width =
TextureManager::GetInstance().GetTexture(texture_id).width;
const auto width_slice = config_.width / 6;
const auto height_slice = config_.height / 3;
SDL_Rect src_rect{0, 0, texture_width, texture_height};
SDL_Rect dest_rect{3 * width_slice - width_slice / 2, 2 * height_slice,
width_slice, height_slice};
render_queue.push({texture_id, src_rect, dest_rect, 0.0});
}

void Renderer::RenderTextures(RenderQueue& render_queue) {
while (!render_queue.empty()) {
auto renderable_texture = render_queue.top();
Expand Down
15 changes: 15 additions & 0 deletions src/Strike/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
add_library(
shotgun
STATIC
src/shotgun.cpp
)
target_include_directories(shotgun PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(
shotgun
PUBLIC
animation
time_manager
)

42 changes: 42 additions & 0 deletions src/Strike/include/Strike/shotgun.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @file shotgun.h
* @author Bilal Kahraman ([email protected])
* @brief
* @version 0.1
* @date 2024-08-29
*
* @copyright Copyright (c) 2024
*
*/

#ifndef STRIKE_INCLUDE_STRIKE_SHOTGUN_H
#define STRIKE_INCLUDE_STRIKE_SHOTGUN_H

#include "Animation/time_based_single_animation.h"
#include "Strike/weapon.h"
#include <memory>

namespace wolfenstein {

class Shotgun : public IWeapon
{
public:
Shotgun();
~Shotgun() = default;

void Attack() override;
int GetTextureId() const;

private:
void AttackAnimation();
double attack_speed_;
double attack_damage_;
std::shared_ptr<TBSAnimation> animation_;

bool cooldown_;
double last_attack_time_;
};

} // namespace wolfenstein

#endif // STRIKE_INCLUDE_STRIKE_SHOTGUN_H
27 changes: 27 additions & 0 deletions src/Strike/include/Strike/strike.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file strike.h
* @author Bilal Kahraman ([email protected])
* @brief
* @version 0.1
* @date 2024-08-29
*
* @copyright Copyright (c) 2024
*
*/

#ifndef STRIKE_INCLUDE_STRIKE_STRIKE_H
#define STRIKE_INCLUDE_STRIKE_STRIKE_H

namespace wolfenstein {

class IStrike
{
public:
virtual ~IStrike() = default;

virtual void Attack() = 0;
};

} // namespace wolfenstein

#endif // STRIKE_INCLUDE_STRIKE_STRIKE_H
27 changes: 27 additions & 0 deletions src/Strike/include/Strike/weapon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file weapon.h
* @author Bilal Kahraman ([email protected])
* @brief
* @version 0.1
* @date 2024-08-29
*
* @copyright Copyright (c) 2024
*
*/

#ifndef STRIKE_INCLUDE_STRIKE_WEAPON_H
#define STRIKE_INCLUDE_STRIKE_WEAPON_H

namespace wolfenstein {

class IWeapon
{
public:
virtual ~IWeapon() = default;

virtual void Attack() = 0;
};

} // namespace wolfenstein

#endif // STRIKE_INCLUDE_STRIKE_WEAPON_H
45 changes: 45 additions & 0 deletions src/Strike/src/shotgun.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "Strike/shotgun.h"
#include "TimeManager/time_manager.h"
#include <iostream>
#include <thread>

namespace wolfenstein {

Shotgun::Shotgun() : attack_speed_(0.6), attack_damage_(10) {
std::vector<int> tex_ids{90, 91, 92, 93, 94, 95};
animation_ =
std::make_shared<TBSAnimation>(tex_ids, attack_speed_ / tex_ids.size());
cooldown_ = false;
}

int Shotgun::GetTextureId() const {
return animation_->GetCurrentFrame();
}

void Shotgun::Attack() {
// If the weapon is not in cooldown, attack and set the cooldown true
if (!cooldown_) {
cooldown_ = true;
last_attack_time_ = TimeManager::GetInstance().GetCurrentTime();

// Create a new thread to play the attack animation
// Use attack speed to calculate the time to wait
std::thread attack_thread(&Shotgun::AttackAnimation, this);
attack_thread.detach();
}
}
// BUG: Segmentation fault if game closes while the thread is running
void Shotgun::AttackAnimation() {
auto attack_time = last_attack_time_;
auto current_time = TimeManager::GetInstance().GetCurrentTime();
while (current_time - last_attack_time_ < attack_speed_) {
auto time_elapsed = current_time - attack_time;
animation_->Update(time_elapsed);
attack_time = current_time;
current_time = TimeManager::GetInstance().GetCurrentTime();
}
animation_->Reset();
cooldown_ = false;
}

} // namespace wolfenstein
14 changes: 13 additions & 1 deletion src/TimeManager/include/TimeManager/time_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
#define TIME_MANAGER_INCLUDE_TIME_MANAGER_H_

#include <chrono>
#include <string>

namespace wolfenstein {

class TimeManager
{
public:
TimeManager() = default;
static TimeManager& GetInstance();

TimeManager(const TimeManager&) = delete;
TimeManager& operator=(const TimeManager&) = delete;
~TimeManager() = default;

void InitClock();
void CalculateDeltaTime();
Expand All @@ -29,10 +34,17 @@ class TimeManager
double GetFramePerSecond();
double GetCurrentTime();

std::string GetTime();

private:
TimeManager() = default;
static TimeManager* instance_;

std::chrono::duration<double> delta_time;
std::chrono::time_point<std::chrono::high_resolution_clock>
previos_time_point;
std::chrono::time_point<std::chrono::high_resolution_clock>
initial_time_point;
};

} // namespace wolfenstein
Expand Down
Loading

0 comments on commit c776729

Please sign in to comment.