Skip to content

Commit

Permalink
Add health bar color ramp
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Jul 21, 2024
1 parent 49d8e2e commit 42edaae
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 13 deletions.
8 changes: 8 additions & 0 deletions assets/config/health_bar.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(
color_ramp: [
Oklcha(Oklcha(lightness: 0.500, chroma: 0.300, hue: 030, alpha: 1.000)),
Oklcha(Oklcha(lightness: 0.500, chroma: 0.300, hue: 030, alpha: 1.000)),
Oklcha(Oklcha(lightness: 0.800, chroma: 0.300, hue: 090, alpha: 1.000)),
Oklcha(Oklcha(lightness: 0.700, chroma: 0.300, hue: 150, alpha: 1.000)),
],
)
1 change: 0 additions & 1 deletion src/core/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ struct CameraConfig {

impl Config for CameraConfig {
const PATH: &'static str = "config/camera.ron";

const EXTENSION: &'static str = "camera.ron";

fn on_load(&mut self, world: &mut World) {
Expand Down
1 change: 0 additions & 1 deletion src/core/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub struct ThemeConfig {

impl Config for ThemeConfig {
const PATH: &'static str = "config/theme.ron";

const EXTENSION: &'static str = "theme.ron";

fn on_load(&mut self, world: &mut World) {
Expand Down
1 change: 0 additions & 1 deletion src/core/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub struct WindowConfig {

impl Config for WindowConfig {
const PATH: &'static str = "config/window.ron";

const EXTENSION: &'static str = "window.ron";

fn on_load(&mut self, world: &mut World) {
Expand Down
1 change: 0 additions & 1 deletion src/game/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub struct ActorConfig {

impl Config for ActorConfig {
const PATH: &'static str = "config/actor.ron";

const EXTENSION: &'static str = "actor.ron";

fn on_load(&mut self, world: &mut World) {
Expand Down
34 changes: 31 additions & 3 deletions src/game/actor/health.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use bevy::ecs::system::EntityCommand;
use bevy::prelude::*;
use serde::Deserialize;
use serde::Serialize;

use crate::core::UpdateSet;
use crate::util::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.configure::<(Health, HealthBar)>();
app.configure::<(Health, ConfigHandle<HealthBarConfig>, HealthBar)>();
}

#[derive(Component, Reflect)]
Expand All @@ -27,6 +29,29 @@ impl Health {
}
}

#[derive(Asset, Reflect, Serialize, Deserialize)]
pub struct HealthBarConfig {
pub color_ramp: Vec<Color>,
}

impl Config for HealthBarConfig {
const PATH: &'static str = "config/health_bar.ron";
const EXTENSION: &'static str = "health_bar.ron";
}

impl HealthBarConfig {
fn color(&self, t: f32) -> Color {
let t = t * (self.color_ramp.len() - 1) as f32;
let lo = t as usize;
let hi = lo + 1;
if hi >= self.color_ramp.len() {
self.color_ramp[self.color_ramp.len() - 1]
} else {
self.color_ramp[lo].mix(&self.color_ramp[hi], t.fract())
}
}
}

/// Reads from the `Health` component on its parent entity.
#[derive(Component, Reflect)]
#[reflect(Component)]
Expand All @@ -42,16 +67,19 @@ impl Configure for HealthBar {
}

fn update_health_bar(
config_handle: Res<ConfigHandle<HealthBarConfig>>,
config: Res<Assets<HealthBarConfig>>,
health_query: Query<&Health>,
mut health_bar_query: Query<(&HealthBar, &Parent, &mut Sprite)>,
) {
let config = r!(config.get(&config_handle.0));

for (health_bar, parent, mut sprite) in &mut health_bar_query {
let health = c!(health_query.get(parent.get()));
let t = health.current / health.max;

sprite.custom_size = Some(Vec2::new(t * health_bar.size.x, health_bar.size.y));
// TODO: Color ramp.
sprite.color = Color::srgb(0.0, 1.0, 0.0);
sprite.color = config.color(t);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/game/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ fn enter_playing(world: &mut World) {
action: id,
},
)]
.into_iter()
.collect(),
.into(),
));
}

Expand Down
1 change: 0 additions & 1 deletion src/game/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ struct StepConfig {

impl Config for StepConfig {
const PATH: &'static str = "config/step.ron";

const EXTENSION: &'static str = "step.ron";

fn on_load(&mut self, world: &mut World) {
Expand Down
2 changes: 2 additions & 0 deletions src/screen/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy_asset_loader::prelude::*;
use iyes_progress::prelude::*;
use pyri_state::prelude::*;

use crate::game::actor::health::HealthBarConfig;
use crate::game::actor::ActorConfig;
use crate::screen::fade_in;
use crate::screen::fade_out;
Expand All @@ -27,6 +28,7 @@ pub(super) fn plugin(app: &mut App) {
Update,
Screen::Loading.on_update((
ActorConfig::progress.track_progress(),
HealthBarConfig::progress.track_progress(),
update_loading.after(TrackedProgressSet),
)),
);
Expand Down
6 changes: 5 additions & 1 deletion src/screen/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy_mod_picking::prelude::*;
use iyes_progress::prelude::*;
use pyri_state::prelude::*;

use crate::game::actor::health::HealthBarConfig;
use crate::game::actor::ActorConfig;
use crate::screen::fade_in;
use crate::screen::fade_out;
Expand All @@ -22,7 +23,10 @@ pub(super) fn plugin(app: &mut App) {
app.configure::<TitleScreenAssets>();
app.add_systems(
Update,
Screen::Title.on_update(ActorConfig::progress.track_progress()),
Screen::Title.on_update((
ActorConfig::progress.track_progress(),
HealthBarConfig::progress.track_progress(),
)),
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ use crate::util::prelude::*;

pub trait Config: Asset + Serialize + for<'de> Deserialize<'de> {
const PATH: &'static str;

const EXTENSION: &'static str;

fn on_load(&mut self, world: &mut World);
fn on_load(&mut self, world: &mut World) {
let _ = world;
}

fn is_ready(&self, asset_server: &AssetServer) -> bool {
let _ = asset_server;
Expand Down

0 comments on commit 42edaae

Please sign in to comment.