Commit fec0cb00 authored by Rahix's avatar Rahix 🦀
Browse files

Keep track of the players sanity

Let's hope we don't make them loose it D:
parent 209e3c24
pub struct Player;
pub struct Player {
pub sanity: f32,
}
impl Player {
pub fn new() -> Player {
Player { sanity: 0.6 }
}
}
......@@ -11,7 +11,7 @@ pub fn create_player(world: &mut legion::World, level: &svg_loader::SvgLevel) ->
.expect("no player spawns in this map");
let spawn = spawn_locations.choose(&mut rand::thread_rng()).unwrap();
world.push((
components::Player,
components::Player::new(),
components::Position::new(spawn.x, spawn.y),
components::Movable::new(),
colliders::Collider::new_circle_collider(50.0),
......
......@@ -17,6 +17,8 @@ pub struct InGameState {
impl InGameState {
pub fn new(level: svg_loader::SvgLevel) -> InGameState {
let sanity_bar = utils::get_element_by_id("sanity-amount").unwrap();
let mut rendering = resources::Rendering::new("game-canvas").unwrap();
let foreground = rendering.register_image(level.foreground_image.clone());
let background = rendering.register_image(level.background_image.clone());
......@@ -40,10 +42,12 @@ impl InGameState {
.add_system(colliders::update_collision_world_system())
.flush()
.add_thread_local(systems::move_movable_system())
.add_thread_local(systems::sanity_goes_up_and_down_system())
.flush()
.add_thread_local(systems::move_camera_to_player_system())
.flush()
.add_thread_local(systems::camera_system())
.add_thread_local(systems::update_sanity_bar_system(sanity_bar))
.flush()
.add_thread_local(systems::draw_level_layer_system(background))
.add_thread_local(systems::draw_tmp_player_system())
......
......@@ -2,10 +2,12 @@ mod camera;
mod draw_colliders;
mod level;
mod moving;
mod player;
mod tmp_player;
pub use camera::camera_system;
pub use draw_colliders::draw_debug_colliders_system;
pub use level::draw_level_layer_system;
pub use moving::{move_camera_to_player_system, move_movable_system};
pub use player::{sanity_goes_up_and_down_system, update_sanity_bar_system};
pub use tmp_player::draw_tmp_player_system;
use crate::components;
use crate::resources;
use legion::IntoQuery;
#[legion::system(for_each)]
#[read_component(components::Player)]
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;
}
#[legion::system]
#[read_component(components::Player)]
pub fn update_sanity_bar(
world: &legion::world::SubWorld,
#[resource] player: &resources::Player,
#[state] sanity_bar: &mut web_sys::Element,
) {
let mut players = <&components::Player>::query();
let player = players.get(world, player.0).unwrap();
sanity_bar
.set_attribute("width", &(player.sanity * 1000.0).to_string())
.unwrap();
}
......@@ -12,8 +12,8 @@
<canvas id="game-canvas" width="1920" height="1080"></canvas>
<svg id="ingame-ui" class="game-gui" viewBox="0 0 1920 1080" style="display: none">
<text x="960" y="80" class="sanity">SANITY</text>
<rect x="460" y="100" width="600", height="40" class="sanity-amount" />
<rect x="460" y="100" width="1000", height="40" class="sanity-outline" />
<rect x="460" y="100" width="600" height="40" class="sanity-amount" id="sanity-amount" />
<rect x="460" y="100" width="1000" height="40" class="sanity-outline" />
</svg>
<svg id="heaven-ui" class="game-gui" viewBox="0 0 1920 1080" style="display: none">
<!-- GUI for the "heaven" -->
......
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