Skip to content

Commit

Permalink
Ft menu and result (#17)
Browse files Browse the repository at this point in the history
* Add new font

* Remove unused shotgun assets

* Add textures for menu and result screen

* Add check to player for alive and damage taken

* Use font, change interface and visualize damage taken and hud

* add texture to texture manager, create menu and result renderer

* Update game
  • Loading branch information
bilalkah authored Dec 1, 2024
1 parent 975ac12 commit 4da74d4
Show file tree
Hide file tree
Showing 34 changed files with 532 additions and 32 deletions.
3 changes: 3 additions & 0 deletions assets/font/EternalAncient.ttf
Git LFS file not shown
Binary file removed assets/sprites/weapon/shotgun/0.png
Binary file not shown.
Binary file removed assets/sprites/weapon/shotgun/1.png
Binary file not shown.
Binary file removed assets/sprites/weapon/shotgun/2.png
Binary file not shown.
Binary file removed assets/sprites/weapon/shotgun/3.png
Binary file not shown.
Binary file removed assets/sprites/weapon/shotgun/4.png
Binary file not shown.
Binary file removed assets/sprites/weapon/shotgun/5.png
Binary file not shown.
3 changes: 3 additions & 0 deletions assets/textures/damage_taken.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/textures/game_over.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/textures/menu_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/menu_background_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/textures/win.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/Camera/include/Camera/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct Camera2DConfig
{
Camera2DConfig(int width, double fov, double depth)
: width(width), fov(fov), depth(depth) {}

int width;
double fov;
double depth;
Expand Down
6 changes: 6 additions & 0 deletions src/Characters/include/Characters/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Player : public ICharacter, public IGameObject

void Update(double delta_time) override;

void SetWeapon(std::shared_ptr<Weapon> weapon);
void SetPose(const vector2d& pose) override;
ObjectType GetObjectType() const override;
vector2d GetPose() const override;
Expand All @@ -44,13 +45,18 @@ class Player : public ICharacter, public IGameObject
double GetWidth() const override;
double GetHeight() const override;
std::shared_ptr<Ray> GetCrosshairRay() const;
std::pair<bool, double> IsDamaged() const;
bool IsAlive() const;
void SubscribeToPlayerPosition(std::function<void(Position2D)> subscriber);

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

bool is_alive_{true};
bool damaged_{false};
double damage_counter_{0.0};
Position2D position_;
double rotation_speed_;
double translation_speed_;
Expand Down
31 changes: 29 additions & 2 deletions src/Characters/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Player::Player(CharacterConfig& config, std::shared_ptr<Camera2D>& camera)
translation_speed_(config.translation_speed) {
id_ = UuidGenerator::GetInstance().GenerateUuid().bytes();
camera_ = camera;
weapon_ = std::make_shared<Weapon>("shotgun");
weapon_ = std::make_shared<Weapon>("mp5");
WeaponStatePtr loaded_state = std::make_shared<LoadedState>();
weapon_->TransitionTo(loaded_state);
}
Expand All @@ -31,6 +31,18 @@ void Player::Update(double delta_time) {
}
}(delta_time);

[this](double delta_time) {
if (!damaged_) {
damage_counter_ = 0;
}
else {
damage_counter_ += delta_time;
if (damage_counter_ >= 1.0) {
damaged_ = false;
}
}
}(delta_time);

ShootOrReload();
weapon_->Update(delta_time);
Move(delta_time);
Expand All @@ -42,6 +54,10 @@ void Player::Update(double delta_time) {
weapon_->SetCrossHair(camera_->GetCrosshairRay());
}

void Player::SetWeapon(std::shared_ptr<Weapon> weapon) {
weapon_ = weapon;
}

void Player::SetPose(const vector2d& pose) {
position_.pose = pose;
}
Expand All @@ -65,7 +81,10 @@ void Player::IncreaseHealth(double amount) {

void Player::DecreaseHealth(double amount) {
health_ -= amount;
health_ = std::max(health_, 0.0);
if (health_ <= 0.0) {
is_alive_ = false;
}
damaged_ = true;
}

double Player::GetHealth() const {
Expand Down Expand Up @@ -94,6 +113,14 @@ std::shared_ptr<Ray> Player::GetCrosshairRay() const {
return camera_->GetCrosshairRay();
}

std::pair<bool, double> Player::IsDamaged() const {
return {damaged_, damage_counter_};
}

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

void Player::SubscribeToPlayerPosition(
std::function<void(Position2D)> updator) {
player_position_subscribers_.push_back(updator);
Expand Down
2 changes: 2 additions & 0 deletions src/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ target_link_libraries(
renderer_interface
renderer_2d
renderer_3d
renderer_menu
renderer_result
)
9 changes: 8 additions & 1 deletion src/Core/include/Core/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "Graphics/renderer_2d.h"
#include "Graphics/renderer_3d.h"
#include "Graphics/renderer_interface.h"
#include "Graphics/renderer_menu.h"
#include "Graphics/renderer_result.h"
#include "Map/map.h"
#include "Math/vector.h"
#include "TextureManager/texture_manager.h"
Expand Down Expand Up @@ -62,21 +64,26 @@ class Game

private:
void Init();
void CheckEvent();
void CheckGameEvent();
void CheckMenuEvent();
void Update();
void Render();
void PrepareEnemies();
void PrepareDynamicObjects();
void PrepareStaticObjects();

std::shared_ptr<IRenderer> renderer_;
std::shared_ptr<Menu> menu_;
std::shared_ptr<RendererContext> renderer_context_;
std::shared_ptr<RendererResult> renderer_result_;
std::shared_ptr<Scene> scene_;
std::shared_ptr<Player> player_;
std::shared_ptr<Map> map_;

GeneralConfig config_;
bool is_running_;
bool is_menu_;
bool is_result_;
RenderType render_type_;
};

Expand Down
1 change: 0 additions & 1 deletion src/Core/include/Core/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "Characters/player.h"
#include "GameObjects/game_object.h"
#include "Map/map.h"
#include "CollisionManager/collision_manager.h"

namespace wolfenstein {

Expand Down
82 changes: 76 additions & 6 deletions src/Core/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "State/enemy_state.h"
#include "TextureManager/texture_manager.h"
#include "TimeManager/time_manager.h"
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_video.h>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -43,11 +44,14 @@ void Game::Init() {
config_.padding, config_.scale,
config_.fps, config_.view_distance,
config_.fov, config_.fullscreen};

renderer_context_ = std::make_shared<RendererContext>(
"Wolfenstein", render_config, camera_);

renderer_ = std::make_shared<Renderer3D>(renderer_context_);
renderer_->SetScene(scene_);

menu_ = std::make_shared<Menu>(renderer_context_);

CharacterConfig player_config = {Position2D({3, 1.5}, 1.50), 2.0, 0.4, 0.4,
1.0};
Expand Down Expand Up @@ -78,19 +82,54 @@ void Game::Init() {
ShootingManager::GetInstance().InitManager(map_, player_,
scene_->GetEnemies());
NavigationManager::GetInstance().InitManager(map_, scene_->GetEnemies());

is_running_ = true;
TimeManager::GetInstance().InitClock();
is_running_ = false;
is_result_ = false;
is_menu_ = true;
}

void Game::CheckMenuEvent() {

SDL_Event event;
while (SDL_PollEvent(&event)) {
// When user close the window
if (event.type == SDL_QUIT ||
(event.type == SDL_EventType::SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_ESCAPE)) {
is_running_ = false;
is_menu_ = false;
}

// When user press a key
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_SPACE) {
player_->SetWeapon(menu_->GetSelectedWeapon());
is_menu_ = false;
is_running_ = true;
}
// left arrow
if (event.key.keysym.sym == SDLK_LEFT) {
menu_->ChangeSelection(-1);
}
// right arrow
if (event.key.keysym.sym == SDLK_RIGHT) {
menu_->ChangeSelection(1);
}
}
}
}

void Game::CheckEvent() {
void Game::CheckGameEvent() {

SDL_Event event;
while (SDL_PollEvent(&event)) {

// When user close the window
if (event.type == SDL_QUIT ||
(event.type == SDL_EventType::SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_ESCAPE)) {
is_running_ = false;
is_menu_ = false;
}

// When user alt tab the window
Expand All @@ -110,22 +149,53 @@ void Game::CheckEvent() {
render_type_ == RenderType::TEXTURE) {
render_type_ = RenderType::LINE;
renderer_ = std::make_shared<Renderer2D>(renderer_context_);
renderer_->SetScene(scene_);
}
else if (event.key.keysym.sym == SDLK_p &&
render_type_ == RenderType::LINE) {
render_type_ = RenderType::TEXTURE;
renderer_ = std::make_shared<Renderer3D>(renderer_context_);
renderer_->SetScene(scene_);
}
}
}
}

void Game::Run() {

while (is_menu_) {
CheckMenuEvent();
TimeManager::GetInstance().CalculateDeltaTime();
menu_->Render();
SDL_WarpMouseInWindow(nullptr, 400, 300);
}

while (is_running_) {
CheckEvent();
CheckGameEvent();
TimeManager::GetInstance().CalculateDeltaTime();
scene_->Update(TimeManager::GetInstance().GetDeltaTime());
renderer_->RenderScene(scene_);
renderer_->RenderScene();
// Check if player is dead
if (!player_->IsAlive()) {
is_running_ = false;
is_result_ = true;
renderer_result_ = std::make_shared<RendererResult>(
renderer_context_,
TextureManager::GetInstance().GetTexture(10));
}
}

while (is_result_) {
SDL_Event event;
SDL_PollEvent(&event);
if (event.type == SDL_QUIT ||
(event.type == SDL_EventType::SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_ESCAPE)) {
is_result_ = false;
}

TimeManager::GetInstance().CalculateDeltaTime();
renderer_result_->Render();
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/Graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,33 @@ target_link_libraries(
PUBLIC
renderer_interface
)

add_library(
renderer_menu
STATIC
src/renderer_menu.cpp
)
target_include_directories(renderer_menu PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(
renderer_menu
PUBLIC
renderer_interface
)

add_library(
renderer_result
STATIC
src/renderer_result.cpp
)
target_include_directories(renderer_result PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(
renderer_result
PUBLIC
renderer_interface
)
2 changes: 1 addition & 1 deletion src/Graphics/include/Graphics/renderer_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Renderer2D : public IRenderer
public:
using IRenderer::IRenderer;
~Renderer2D() = default;
void RenderScene(const std::shared_ptr<Scene>& scene_ptr) override;
void RenderScene() override;

private:
void RenderMap(const std::shared_ptr<Map> map_ptr);
Expand Down
3 changes: 2 additions & 1 deletion src/Graphics/include/Graphics/renderer_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Renderer3D : public IRenderer
public:
using IRenderer::IRenderer;
~Renderer3D() = default;
void RenderScene(const std::shared_ptr<Scene>& scene_ptr) override;
void RenderScene() override;

private:
void RenderBackground();
Expand All @@ -68,6 +68,7 @@ class Renderer3D : public IRenderer
void RenderWeapon(const std::shared_ptr<Player>& player_ptr,
RenderQueue& render_queue);
void RenderTextures(RenderQueue& render_queue);
void RenderHUD(const std::shared_ptr<Player>& player_ptr);
}; // class Renderer3D

} // namespace wolfenstein
Expand Down
Loading

0 comments on commit 4da74d4

Please sign in to comment.