Skip to content

Commit

Permalink
Adding a health bar (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fotonnn committed Oct 11, 2023
1 parent b33015b commit 32afe88
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
55 changes: 55 additions & 0 deletions controller/src/enhancements/player.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const HEALTH_BAR_MAX_HEALTH: f32 = 100.0;

use std::{
ffi::CStr,
sync::Arc,
Expand Down Expand Up @@ -349,6 +351,59 @@ impl Enhancement for PlayerESP {
draw.add_rect([vmin.x, vmin.y], [vmax.x, vmax.y], *esp_color)
.thickness(settings.esp_boxes_thickness)
.build();

if settings.esp_health_bar {
let bar_height = vmax.y - vmin.y;

let player_health = entry.player_health as f32;
let clamped_player_health =
player_health.clamp(0.0, HEALTH_BAR_MAX_HEALTH);
let health_percentage =
clamped_player_health / HEALTH_BAR_MAX_HEALTH;
let filled_height = bar_height * health_percentage;

let border_color = [0.0, 0.0, 0.0, esp_color[3]];

let bar_width = if settings.esp_health_bar_size {
6.0
} else {
1.0
};

let bar_x = if settings.esp_health_bar_size {
vmin.x - bar_width + 1.0
} else {
vmin.x + 1.0
};
draw.add_rect(
[vmin.x - bar_width, vmax.y - 1.0],
[vmin.x + 1.0, vmin.y + 1.0],
border_color,
)
.build();

if settings.rainbow_health_bar {
let rainbow_color = view
.calculate_rainbow_color(player_health as f32, *esp_color);
draw.add_rect(
[bar_x, vmax.y - 1.0],
[vmin.x, (vmax.y + 1.0) - filled_height],
rainbow_color,
)
.filled(true)
.build();
} else {
let health_color =
view.calculate_health_color(health_percentage, *esp_color);
draw.add_rect(
[bar_x, vmax.y - 1.0],
[vmin.x, (vmax.y + 1.0) - filled_height],
health_color,
)
.filled(true)
.build();
}
}
}
}
EspBoxType::Box3D => {
Expand Down
10 changes: 9 additions & 1 deletion controller/src/settings/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ fn default_esp_skeleton_thickness() -> f32 {
fn default_esp_boxes_thickness() -> f32 {
3.0
}

fn default_u32<const V: u32>() -> u32 {
V
}
Expand Down Expand Up @@ -90,6 +89,15 @@ pub struct AppSettings {
#[serde(default = "bool_false")]
pub esp_info_health: bool,

#[serde(default = "bool_false")]
pub esp_health_bar: bool,

#[serde(default = "bool_false")]
pub esp_health_bar_size: bool,

#[serde(default = "bool_false")]
pub rainbow_health_bar: bool,

#[serde(default = "bool_false")]
pub esp_info_weapon: bool,

Expand Down
8 changes: 8 additions & 0 deletions controller/src/settings/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ impl SettingsUI {
ui.slider_config(obfstr!("Thickness"), 0.1, 10.0)
.build(&mut settings.esp_boxes_thickness);
}
if settings.esp_box_type == EspBoxType::Box2D {
ui.checkbox(obfstr!("2DBOX: Show Health Bar"), &mut settings.esp_health_bar);
if settings.esp_health_bar {
ui.same_line();
ui.checkbox(obfstr!("Big bar"), &mut settings.esp_health_bar_size);
ui.checkbox(obfstr!("Rainbow Health Bar (Random colors!)"), &mut settings.rainbow_health_bar);
}
}

ui.checkbox(obfstr!("ESP Skeletons"), &mut settings.esp_skeleton);
if settings.esp_skeleton {
Expand Down
20 changes: 20 additions & 0 deletions controller/src/view/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl ViewController {

let mut min2d = Vec2::new(f32::MAX, f32::MAX);
let mut max2d = Vec2::new(-f32::MAX, -f32::MAX);

for point in points {
if let Some(point) = self.world_to_screen(&point, true) {
min2d.x = min2d.x.min(point.x);
Expand Down Expand Up @@ -182,4 +183,23 @@ impl ViewController {
}
}
}

pub fn calculate_rainbow_color(&self, value: f32, esp_color: [f32; 4]) -> [f32; 4] {
let frequency: f32 = 0.1;
let sin_value = |offset: f32| (frequency * value + offset).sin() * 0.5 + 1.0;
let r: f32 = sin_value(0.0);
let g: f32 = sin_value(2.0 * std::f32::consts::PI / 3.0);
let b: f32 = sin_value(4.0 * std::f32::consts::PI / 3.0);
[r, g, b, esp_color[3]]
}

pub fn calculate_health_color(&self, health_percentage: f32, esp_color: [f32; 4]) -> [f32; 4] {
let clamped_percentage = health_percentage.clamp(0.0, 1.0);

let r = 1.0 - clamped_percentage;
let g = clamped_percentage;
let b = 0.0;

[r, g, b, esp_color[3]]
}
}

0 comments on commit 32afe88

Please sign in to comment.