Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Health Bar #53

Merged
merged 18 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 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 @@ -346,6 +348,56 @@ 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; // height = box height
let bar_x = vmin.x - 5.0; // Left

let player_health = entry.player_health as f32;
let clamped_player_health =
player_health.max(0.0).min(HEALTH_BAR_MAX_HEALTH);
Fotonnn marked this conversation as resolved.
Show resolved Hide resolved
let health_percentage =
clamped_player_health / HEALTH_BAR_MAX_HEALTH;
let filled_height = bar_height * health_percentage;
let width = settings.health_bar_width;
let border_bar_y = vmax.y;
let border_thickness = 1.5;
let border_color = [0.0, 0.0, 0.0, 0.75];
Fotonnn marked this conversation as resolved.
Show resolved Hide resolved

let bar_y = vmax.y - filled_height - 1.5;

if settings.rainbow_health_bar {
let rainbow_color =
view.calculate_rainbow_color(player_health as f32);
view.draw_health_bar(
&draw,
bar_x,
bar_y,
filled_height,
width,
rainbow_color,
border_thickness,
border_color,
bar_height,
border_bar_y,
)
} else {
let health_color =
view.calculate_health_color(health_percentage);
view.draw_health_bar(
&draw,
bar_x,
bar_y,
filled_height,
width,
health_color,
border_thickness,
border_color,
bar_height,
border_bar_y,
)
}
}
}
}
EspBoxType::Box3D => {
Expand Down
13 changes: 12 additions & 1 deletion controller/src/settings/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ fn default_esp_skeleton_thickness() -> f32 {
fn default_esp_boxes_thickness() -> f32 {
3.0
}

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

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

#[serde(default = "default_health_bar_width")]
pub health_bar_width: f32,
Fotonnn marked this conversation as resolved.
Show resolved Hide resolved

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

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

Expand Down
9 changes: 9 additions & 0 deletions controller/src/settings/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ 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.slider_config(obfstr!("Width"), 1.0, 10.0)
.build(&mut settings.health_bar_width);
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
55 changes: 55 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,58 @@ impl ViewController {
}
}
}

pub fn draw_health_bar(
&self,
draw: &imgui::DrawListMut,
bar_x: f32,
bar_y: f32,
filled_height: f32,
bar_width: f32,
health_color: [f32; 4],
border_thickness: f32,
border_color: [f32; 4],
bar_height: f32,
border_bar_y: f32,
) {
draw.add_rect_filled_multicolor(
Fotonnn marked this conversation as resolved.
Show resolved Hide resolved
[bar_x, bar_y],
[bar_x + bar_width, bar_y + filled_height],
health_color,
health_color,
health_color,
health_color,
);

// fix border
let border_top_y = border_bar_y - border_thickness;
let border_bottom_y = border_top_y - bar_height;

draw.add_rect(
[bar_x - border_thickness, border_top_y], // top left
[bar_x + bar_width + border_thickness, border_bottom_y], // bottom right
border_color,
)
.thickness(border_thickness)
.build();
}

pub fn calculate_rainbow_color(&self, value: f32) -> [f32; 4] {
Fotonnn marked this conversation as resolved.
Show resolved Hide resolved
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, 0.75]
Fotonnn marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn calculate_health_color(&self, health_percentage: f32) -> [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, 0.75]
}
}