Skip to content

Commit

Permalink
Improve difficulty levels
Browse files Browse the repository at this point in the history
  • Loading branch information
pwalig committed Jul 3, 2024
1 parent d3d45df commit 6e69129
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/cgv_st_07_win.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@
<PreprocessorDefinitions>_DEBUG;_CONSOLE;GLEW_STATIC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>glew\include;glfw\include;.;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down
33 changes: 24 additions & 9 deletions src/gameplay_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
#include <engine.h>
#include <enemy_generator.h>

#define DEBUG

#ifdef DEBUG
#include <stdexcept>
#endif

game::gameplay_manager* game::gameplay_manager::instance = nullptr;
glm::vec3* game::gameplay_manager::player_position = nullptr;
bool game::gameplay_manager::game_paused = false;
double game::gameplay_manager::_time_scale_buffor = 1.0f;
float game::gameplay_manager::difficulty_float = 0.0f;
float game::gameplay_manager::difficulty_float = 1.0f;

game::gameplay_manager::gameplay_manager()
{
Expand Down Expand Up @@ -83,15 +89,24 @@ game::gameplay_manager::~gameplay_manager()
if (instance == this) instance = nullptr;
}

float game::gameplay_manager::get_difficulty_mulitplier(float influence, const bool& inverse)
float game::gameplay_manager::multiply_by_difficulty(const float& value, const float& influence, const bool& inverse)
{
if (difficulty_float == 0.0f) {
if (inverse) return std::numeric_limits<float>::max();
else return 0.0f;
}
#ifdef DEBUG
if (influence > 1.0f || influence < 0.0f) throw std::runtime_error("influence was not in range (0.0f ; 1.0f)");
#endif
float multiplier = difficulty_float;
if (inverse) multiplier = 1.0f / multiplier;
float new_value = value * multiplier;
return (new_value * influence) + (value * (1.0f - influence));
}

int game::gameplay_manager::multiply_by_difficulty(const int& value, float influence, const bool& inverse)
{
if (influence > 1.0f) influence = 1.0f;
if (influence < 0.0f) influence = 0.0f;
float new_value = 1.0f + (difficulty_float * influence);
if (new_value > 100.0f) new_value = 100.0f;
if (new_value < 0.01f) new_value = 0.01f;
if (inverse) return 1.0f / new_value;
else return new_value;
return (int)multiply_by_difficulty((float)value, influence, inverse);
}

void game::gameplay_manager::framebuffer_size_callback(GLFWwindow* window, int width, int height) {
Expand Down
5 changes: 3 additions & 2 deletions src/gameplay_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ namespace game {
static void full_screen();
~gameplay_manager();

static float difficulty_float; // ranges from -1 to 100, =0 -> normal difficulty, >0 -> hard, <0 -> easy
static float get_difficulty_mulitplier(float influence = 1.0f, const bool& inverse = false);
static float difficulty_float; // ranges from 0.01 to 100, =1 -> normal difficulty, >1 -> hard, <1 -> easy
static float multiply_by_difficulty(const float& value, const float& influence = 1.0f, const bool& inverse = false);
static int multiply_by_difficulty(const int& value, float influence = 1.0f, const bool& inverse = false);

private:
static double _time_scale_buffor;
Expand Down
4 changes: 2 additions & 2 deletions src/hit_scan_damage_gun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ game::hit_scan_damage_gun::hit_scan_damage_gun(const int& max_damage_, const int
weapon::hit_scan(
physics::ray(position, direction, layer), // ray
[this](game::entity* ent) { // on hit
int damage = RandomT<int>(this->min_damage * game::gameplay_manager::get_difficulty_mulitplier(0.6f, true),
this->max_damage * game::gameplay_manager::get_difficulty_mulitplier(0.6f, true));
int damage = RandomT<int>(game::gameplay_manager::multiply_by_difficulty(this->min_damage, 0.6f, true),
game::gameplay_manager::multiply_by_difficulty(this->max_damage, 0.6f, true));
ent->damage(damage);
game::damage_number* dm = new game::damage_number(damage);
dm->uit.color = glm::vec4((float)damage / (float)max_damage, 0.0f, 0.0f, 1.0f);
Expand Down
17 changes: 9 additions & 8 deletions src/player_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void game::player::update()

void game::player::damage(int damage)
{
this->entity::damage(game::gameplay_manager::get_difficulty_mulitplier(0.6f) * damage);
this->entity::damage(game::gameplay_manager::multiply_by_difficulty(damage, 0.6f));

// update healt bar
game::player_ui* ui = scripts_system::find_script_of_type<game::player_ui>("hud");
Expand All @@ -136,7 +136,7 @@ void game::player::damage(int damage)

void game::player::heal(int healing)
{
this->entity::heal(game::gameplay_manager::get_difficulty_mulitplier(0.6f, true) * healing);
this->entity::heal(game::gameplay_manager::multiply_by_difficulty(healing, 0.6f, true));

// update healt bar
game::player_ui* ui = scripts_system::find_script_of_type<game::player_ui>("hud");
Expand All @@ -163,6 +163,7 @@ void game::player::jump()

void game::player::land(physics::collision_info ci)
{
if ((ci.other->layer != COLLISION_LAYERS_ENVIRIONMENT) && (ci.other->layer != COLLISION_LAYERS_DEFAULT)) return;
if (ci.normal.y > 0.5f) {
floor_normal = ci.normal;
if (!ready_to_jump) {
Expand All @@ -178,21 +179,21 @@ void game::player::dash()
glm::vec3 move_dir = rotatation_between(VEC3_UP, floor_normal) * (rb.rotation * glm::vec3(move_in.normalized().x, 0.0f, move_in.normalized().y));
if (ready_to_dash) {
if (glm::length(move_dir) > 0.0f) {
max_speed = game::gameplay_manager::get_difficulty_mulitplier(0.2f, true) * 24.0f;
max_speed = game::gameplay_manager::multiply_by_difficulty(24.0f, 0.1f, true);
float y_vel = glm::dot(rb.velocity, floor_normal); // velocity along the normal
rb.velocity -= floor_normal * y_vel; // set velocity along the normal to 0
rb.velocity = move_dir * max_speed;
rb.velocity += floor_normal * y_vel; // set velocity along the normal back to y_vel
ready_to_dash = false;
dash_timer.start(game::gameplay_manager::get_difficulty_mulitplier(0.2f, true) * 0.1f);
dash_cooldown.start(game::gameplay_manager::get_difficulty_mulitplier(0.2f) * 1.4f);
dash_timer.start(game::gameplay_manager::multiply_by_difficulty(0.1f, 0.1f, true));
dash_cooldown.start(game::gameplay_manager::multiply_by_difficulty(1.4f, 0.2f));
}
else if (glm::length(rb.velocity) > 0.0f) {
max_speed = game::gameplay_manager::get_difficulty_mulitplier(0.2f, true) * 24.0f;
max_speed = game::gameplay_manager::multiply_by_difficulty(24.0f, 0.1f, true);
rb.velocity = glm::normalize(rb.velocity) * max_speed;
ready_to_dash = false;
dash_timer.start(game::gameplay_manager::get_difficulty_mulitplier(0.2f, true) * 0.1f);
dash_cooldown.start(game::gameplay_manager::get_difficulty_mulitplier(0.2f) * 1.4f);
dash_timer.start(game::gameplay_manager::multiply_by_difficulty(0.1f, 0.1f, true));
dash_cooldown.start(game::gameplay_manager::multiply_by_difficulty(1.4f, 0.2f));
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/projectile_damage_gun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#include "scripts_system.h"
#include "random_t.h"
#include <damage_number.h>
#include <gameplay_manager.h>

game::projectile_damage_gun::projectile_damage_gun(const int& max_damage_, const int& min_damage_) : max_damage(max_damage_), min_damage(min_damage_)
{
this->shoot = [this](const glm::vec3& position, const glm::vec3& direction, const int& layer) {
game::projectile* proj = scripts_system::instantiate<game::projectile, float>(0.15f);
proj->on_hit = [this](game::entity* ent) {
int damage = RandomT<int>(this->min_damage, this->max_damage);
int damage = RandomT<int>(game::gameplay_manager::multiply_by_difficulty(this->min_damage, 0.6f, true),
game::gameplay_manager::multiply_by_difficulty(this->max_damage, 0.6f, true));
ent->damage(damage);
game::damage_number* dm = new game::damage_number(damage);
dm->uit.color = glm::vec4((float)damage / (float)max_damage, 0.0f, 0.0f, 1.0f);
Expand Down
8 changes: 4 additions & 4 deletions src/settings_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ game::settings_menu::settings_menu(const std::function<void()>& on_close) :
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 1080.0f, -1.0f)), glm::vec3(30.0f, 50.0f, 1.0f))),
volume_text("VOLUME", "../assets/fonts/bitmap/handwiriting-readable.png",
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 1000.0f, -1.0f)), glm::vec3(17.0f, 30.0f, 1.0f))),
difficulty_text("DIFFICULTY : x" + std::to_string(game::gameplay_manager::get_difficulty_mulitplier(1.0f)), "../assets/fonts/bitmap/handwiriting-readable.png",
difficulty_text("DIFFICULTY : x" + std::to_string(game::gameplay_manager::difficulty_float), "../assets/fonts/bitmap/handwiriting-readable.png",
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 850.0f, -1.0f)), glm::vec3(17.0f, 30.0f, 1.0f))),
mouse_sensitivity_text("MOUSE SENSITIVITY : " + std::to_string(input_system::global_mouse_sensitivity * 300.0f), "../assets/fonts/bitmap/handwiriting-readable.png",
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 700.0f, -1.0f)), glm::vec3(17.0f, 30.0f, 1.0f))),
Expand All @@ -33,7 +33,7 @@ game::settings_menu::settings_menu(const std::function<void()>& on_close) :
// DEFAULTS
this->mouse_sensitivity.value = input_system::global_mouse_sensitivity * 300.0f;
this->mouse_sensitivity.update_visual();
this->difficulty.value = std::sqrt((game::gameplay_manager::difficulty_float + 1.0f) / 10.0f);
this->difficulty.value = std::sqrt(game::gameplay_manager::difficulty_float / 10.0f);
this->difficulty.update_visual();

// STYLE
Expand All @@ -60,8 +60,8 @@ game::settings_menu::settings_menu(const std::function<void()>& on_close) :
this->mouse_sensitivity_text.text = "MOUSE SENSITIVITY : " + std::to_string(new_sensitivity);
};
this->difficulty.on_value_changed = [this](float new_difficulty) {
game::gameplay_manager::difficulty_float = new_difficulty * new_difficulty * 10.0f - 1.0f;
this->difficulty_text.text = "DIFFICULTY : x" + std::to_string(game::gameplay_manager::get_difficulty_mulitplier(1.0f));
game::gameplay_manager::difficulty_float = new_difficulty * new_difficulty * 10.0f;
this->difficulty_text.text = "DIFFICULTY : x" + std::to_string(game::gameplay_manager::difficulty_float);
};
this->graphics.on_click.subscribe([on_close]() {
new graphics_menu([on_close]() { new game::settings_menu([on_close]() { on_close(); }); });
Expand Down

0 comments on commit 6e69129

Please sign in to comment.