Commit 0fa58a8e authored by Simon Goller's avatar Simon Goller Committed by neosam
Browse files

Convert player sanity to progress

Using a range between 0.0 and `max` is more flexible than a range
between 0.0 and 1.0.  This makes it possible to create items which
extend max health so the player can take more damange before it's
game over.
parent fd64e440
use crate::data_types;
#[derive(Debug, Clone)]
pub struct Player {
pub sanity: f32,
pub sanity: data_types::Progress,
pub collected_hours: u32,
}
impl Player {
pub fn new() -> Player {
Player {
sanity: 1.0,
sanity: data_types::Progress::with_full_value(30.0),
collected_hours: 0,
}
}
......
pub mod progress;
pub use progress::Progress;
#[derive(Debug, Clone)]
pub struct Progress {
pub max: f32,
pub value: f32,
}
impl Progress {
pub fn with_full_value(max: f32) -> Self {
Progress { value: max, max }
}
pub fn relative_value(&self) -> f32 {
self.value / self.max
}
}
......@@ -6,6 +6,7 @@ pub mod angel_shifts;
pub mod colliders;
pub mod colors;
pub mod components;
pub mod data_types;
pub mod entities;
pub mod gamestate;
pub mod resources;
......
......@@ -29,7 +29,10 @@ impl HeavenState {
// Update the angel stats GUI elements
utils::get_element_by_id::<web_sys::Element>("heaven-sanity")
.unwrap()
.set_inner_html(&format!("{}%", (player.sanity * 100.0).round() as usize));
.set_inner_html(&format!(
"{}%",
(player.sanity.relative_value() * 100.0).round() as usize
));
utils::get_element_by_id::<web_sys::Element>("heaven-collected-hours")
.unwrap()
.set_inner_html(&format!("{}", player.collected_hours));
......
......@@ -9,7 +9,7 @@ pub fn sanity_goes_up_and_down(
player: &mut components::Player,
#[resource] clock: &resources::Clock,
) {
player.sanity = (clock.wall_time() as f32).sin() * 0.5 + 0.5;
player.sanity.value = (clock.wall_time() as f32).sin() * 15.0 + 15.0;
}
#[legion::system]
......@@ -23,7 +23,10 @@ pub fn update_sanity_bar(
let player = players.get(world, player.0).unwrap();
sanity_bar
.set_attribute("width", &(player.sanity * 1000.0).to_string())
.set_attribute(
"width",
&(player.sanity.relative_value() * 1000.0).to_string(),
)
.unwrap();
}
......@@ -37,7 +40,7 @@ pub fn player_sanity_check(
let mut players = <&mut components::Player>::query();
let player = players.get_mut(world, player.0).unwrap();
if player.sanity <= 0.0 {
if player.sanity.value <= 0.0 {
game_manager.request_game_over();
}
}
......@@ -46,7 +46,7 @@ pub fn reduce_sanity_obstacle(
let player = <&mut components::Player>::query()
.get_mut(world, player.0)
.unwrap();
player.sanity -= 0.15 * clock.frame_delta();
player.sanity.value -= 4.5 * clock.frame_delta();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment