Skip to content

Commit

Permalink
Stamina
Browse files Browse the repository at this point in the history
  • Loading branch information
rewin123 committed Dec 10, 2023
1 parent 3d5c410 commit a899f79
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 83 deletions.
151 changes: 113 additions & 38 deletions src/level_ui.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use bevy::prelude::*;

use crate::{storyteller::LevelTimer, GameStuff};
use crate::{storyteller::LevelTimer, GameStuff, player::Stamina, GameSet};

pub struct LevelUiPlugin;

impl Plugin for LevelUiPlugin {
fn build(&self, app: &mut App) {
app.add_event::<CreateLevelUi>()
.add_systems(Update, create_level_ui_system);
.add_systems(Update, create_level_ui_system)
.add_systems(Update, show_stamina.in_set(GameSet::Playing));
}
}

Expand All @@ -17,9 +18,13 @@ pub struct CreateLevelUi;
#[derive(Component)]
pub struct LevelUi;


#[derive(Component)]
pub struct TaskText;

#[derive(Component)]
pub struct StaminaState;

fn create_level_ui_system(
mut commands: Commands,
_asset_server: Res<AssetServer>,
Expand All @@ -34,53 +39,123 @@ fn create_level_ui_system(

//Spawn top info bar
commands
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,

top: Val::Px(0.0),
left: Val::Px(0.0),
..default()
},
..default()
}).with_children(|parent| {
parent.spawn((
NodeBundle {
style: Style {
width: Val::Percent(80.0),
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
min_height: Val::Px(150.0),
..default()
},
background_color: BackgroundColor(Color::rgba(0.0, 0.0, 0.0, 0.5)),
..default()
},
LevelUi,
GameStuff,
))
.with_children(|parent| {
parent.spawn((
TextBundle::from_section("", text_style.clone()),
LevelUi,
LevelTimer,
));

parent.spawn((
TextBundle::from_section(
"",
text_style.clone()
),
LevelUi,
TaskText
));

spawn_bar(parent);
});
});

ev_create_level_ui.clear();
}

fn spawn_bar(parent: &mut ChildBuilder) {
parent
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Px(100.0),
justify_content: JustifyContent::Center,
height: Val::Px(10.0),
width: Val::Px(200.0),
align_items: AlignItems::Center,
align_self: AlignSelf::Stretch,
top: Val::Px(0.0),
left: Val::Px(0.0),
..default()
justify_content: JustifyContent::Center,
flex_direction: FlexDirection::Row,
..Default::default()
},
..default()
..Default::default()
})
.with_children(|parent| {

parent.spawn(TextBundle::from_section("Stamina", TextStyle::default()));

parent
.spawn((
NodeBundle {
style: Style {
width: Val::Percent(50.0),
height: Val::Px(100.0),
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,

align_self: AlignSelf::Stretch,
..default()
},
background_color: BackgroundColor(Color::rgba(0.0, 0.0, 0.0, 0.5)),
..default()
.spawn(NodeBundle {
style: Style {
width: Val::Px(100.),
height: Val::Px(10.),
padding: UiRect::all(Val::Px(1.)),
align_items: AlignItems::Stretch,
top: Val::Px(2.0),
left: Val::Px(6.0),
..Default::default()
},
LevelUi,
GameStuff,
))
background_color: Color::BLACK.into(),
..Default::default()
})
.with_children(|parent| {
parent.spawn((
TextBundle::from_section("", text_style.clone()),
LevelUi,
LevelTimer,
));

parent.spawn((
TextBundle::from_section("", text_style.clone()),
LevelUi,
TaskText,
NodeBundle {
style: Style {
width : Val::Percent(50.0),
..Default::default()
},
background_color: Color::GREEN.into(),
..Default::default()
},
StaminaState,
));
});
});

ev_create_level_ui.clear();
}

fn show_stamina(
mut query: Query<(&mut Style, &mut BackgroundColor), With<StaminaState>>,
staminas : Query<&Stamina>
) {
let Ok(stamina) = staminas.get_single() else {
warn!("Stamina not found");
return;
};

let Ok((mut style, mut background_color)) = query.get_single_mut() else {
warn!("Stamina ui not found");
return;
};

style.width = Val::Percent(stamina.value * 100.0);

if stamina.blocked {
background_color.0 = Color::ORANGE_RED;
} else {
background_color.0 = Color::GREEN;
}
}
80 changes: 70 additions & 10 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ use crate::{
get_sprite_rotation,
physics::Velocity,
sprite_material::{create_plane_mesh, SpriteExtension, SpriteMaterial},
GameStuff,
GameStuff, GameSet,
};

const DOG_PATH: &str = "test/dog.png";

pub const DOG_SPEED: f32 = 45.0 * 1000.0 / 3600.0; // Sheepdog accepts 35 km/h in reality (but fastest dog can do 67 km/h o.0)
pub const DOG_ACCELERATION: f32 = DOG_SPEED * 4.0;

pub const RUN_K: f32 = 1.5;
pub const STAMINA_INCREASE: f32 = 1.0 / 2.5;
pub const STAMINA_DECREASE: f32 = 1.0 / 5.0 + STAMINA_INCREASE;

pub struct PlayerPlugin;

#[derive(Default, Hash, PartialEq, Eq, Debug, States, Clone)]
Expand All @@ -31,7 +35,7 @@ impl Plugin for PlayerPlugin {
app.add_event::<SpawnPlayer>()
.add_event::<Bark>()
.add_state::<MovementStyle>()
.add_systems(Update, spawn_player_by_event)
.add_systems(Update, spawn_player_by_event.in_set(GameSet::Playing))
.add_systems(
Update,
player_movemnt_by_wasd.run_if(in_state(MovementStyle::WASD)),
Expand All @@ -40,11 +44,17 @@ impl Plugin for PlayerPlugin {
Update,
player_movemnt_by_mouse.run_if(in_state(MovementStyle::Mouse)),
)
.add_systems(Update, (change_movement_style, bark))
.add_systems(Update, (set_cam_distance, camera_movement));
.add_systems(Update, (change_movement_style, bark).in_set(GameSet::Playing))
.add_systems(Update, (set_cam_distance, camera_movement, stamina_increse).in_set(GameSet::Playing));
}
}

#[derive(Component)]
pub struct Stamina {
pub value: f32,
pub blocked: bool
}

fn change_movement_style(
mut next_state: ResMut<NextState<MovementStyle>>,
current_state: Res<State<MovementStyle>>,
Expand All @@ -59,6 +69,19 @@ fn change_movement_style(
}
}

fn stamina_increse(
mut stamina_query: Query<&mut Stamina>,
time: Res<Time>,
) {
for mut stamina in &mut stamina_query {
stamina.value += STAMINA_INCREASE * time.delta_seconds();
if stamina.value > 1.0 {
stamina.value = 1.0;
stamina.blocked = false;
}
}
}

#[derive(Component)]
pub struct Player;

Expand Down Expand Up @@ -115,18 +138,23 @@ fn spawn_player_by_event(
Dog,
Velocity::default(),
GameStuff,
Stamina {
value: 1.0,
blocked: false
},
));
}
event_reader.clear();
}

fn player_movemnt_by_mouse(
mut player_query: Query<(&Transform, &mut Velocity), With<Player>>,
mut player_query: Query<(&Transform, &mut Velocity, &mut Stamina), With<Player>>,
time: Res<Time>,
q_window: Query<&Window, With<PrimaryWindow>>,
q_camera: Query<(&Camera, &GlobalTransform)>,
input: Res<Input<KeyCode>>
) {
let Ok((transform, mut vel)) = player_query.get_single_mut() else {
let Ok((transform, mut vel, mut stamine)) = player_query.get_single_mut() else {
return;
};

Expand All @@ -147,9 +175,23 @@ fn player_movemnt_by_mouse(
return;
};

let mut use_stamina = input.pressed(KeyCode::ShiftLeft);
if stamine.blocked {
use_stamina = false;
}

if use_stamina {
stamine.value -= time.delta_seconds() * STAMINA_DECREASE;
if stamine.value < 0.0 {
stamine.blocked = true;
}
}

let speed_k = if use_stamina { 1.0 } else {RUN_K};

let globel_cursor = ray.get_point(distance);

let speed: f32 = DOG_SPEED;
let speed: f32 = DOG_SPEED * speed_k;
let accel: f32 = DOG_ACCELERATION;

let dir = (globel_cursor - transform.translation).normalize_or_zero();
Expand Down Expand Up @@ -182,15 +224,14 @@ pub fn bark(
}

fn player_movemnt_by_wasd(
mut player_query: Query<&mut Velocity, With<Player>>,
mut player_query: Query<(&mut Velocity, &mut Stamina), With<Player>>,
input: Res<Input<KeyCode>>,
time: Res<Time>,
) {
let Ok(mut player) = player_query.get_single_mut() else {
let Ok((mut player, mut stamina)) = player_query.get_single_mut() else {
return;
};

let speed = DOG_SPEED;
let accel = DOG_ACCELERATION;

let mut dir = Vec3::ZERO;
Expand All @@ -211,6 +252,25 @@ fn player_movemnt_by_wasd(
dir += Vec3::new(1.0, 0.0, 0.0);
}

let mut use_stamina = input.pressed(KeyCode::ShiftLeft);

if stamina.blocked {
use_stamina = false;
}

if use_stamina {
stamina.value -= time.delta_seconds() * STAMINA_DECREASE;
if stamina.value < 0.0 {
stamina.blocked = true;
}
}

let speed = if use_stamina {
DOG_SPEED * RUN_K
} else {
DOG_SPEED
};

dir = dir.normalize_or_zero();

let target_speed = dir * speed;
Expand Down
Loading

0 comments on commit a899f79

Please sign in to comment.