feat: ✨ ui
This commit is contained in:
parent
cca4b205f5
commit
2b0edc9c50
5 changed files with 157 additions and 42 deletions
|
@ -2,29 +2,34 @@ use bevy::{core_pipeline::bloom::Bloom, prelude::*};
|
|||
|
||||
use crate::{player::Player, CAMERA_DECAY_RATE};
|
||||
|
||||
pub fn setup_camera(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
Camera2d,
|
||||
Camera {
|
||||
hdr: true,
|
||||
..default()
|
||||
},
|
||||
Bloom::NATURAL,
|
||||
));
|
||||
}
|
||||
|
||||
pub fn update_camera(
|
||||
mut camera: Query<&mut Transform, (With<Camera2d>, Without<Player>)>,
|
||||
player: Query<&Transform, (With<Player>, Without<Camera2d>)>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let mut camera = camera.get_single_mut().unwrap();
|
||||
let player = player.get_single().unwrap();
|
||||
|
||||
let Vec3 { x, y, .. } = player.translation;
|
||||
let direction = Vec3::new(x, y, camera.translation.z);
|
||||
|
||||
camera
|
||||
.translation
|
||||
.smooth_nudge(&direction, CAMERA_DECAY_RATE, time.delta_secs());
|
||||
#[derive(Debug, Component)]
|
||||
pub struct UserCamera;
|
||||
|
||||
impl UserCamera {
|
||||
pub fn setup(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
Camera2d,
|
||||
Camera {
|
||||
hdr: true,
|
||||
..default()
|
||||
},
|
||||
Bloom::NATURAL,
|
||||
));
|
||||
}
|
||||
|
||||
pub fn update(
|
||||
mut camera: Query<&mut Transform, (With<Camera2d>, Without<Player>)>,
|
||||
player: Query<&Transform, (With<Player>, Without<Camera2d>)>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let mut camera = camera.get_single_mut().unwrap();
|
||||
let player = player.get_single().unwrap();
|
||||
|
||||
let Vec3 { x, y, .. } = player.translation;
|
||||
let direction = Vec3::new(x, y, camera.translation.z);
|
||||
|
||||
camera
|
||||
.translation
|
||||
.smooth_nudge(&direction, CAMERA_DECAY_RATE, time.delta_secs());
|
||||
}
|
||||
}
|
||||
|
|
26
src/lib.rs
26
src/lib.rs
|
@ -1,13 +1,16 @@
|
|||
use bevy::{
|
||||
app::{App, Startup, Update},
|
||||
diagnostic::FrameTimeDiagnosticsPlugin,
|
||||
DefaultPlugins,
|
||||
};
|
||||
use camera::{setup_camera, update_camera};
|
||||
use camera::UserCamera;
|
||||
use player::Player;
|
||||
use ui::{FPSText, FishCounterText, KeybindingsText};
|
||||
use water::Water;
|
||||
|
||||
mod camera;
|
||||
mod player;
|
||||
mod ui;
|
||||
mod water;
|
||||
|
||||
pub const CHARACTER_SPEED: f32 = 300.0;
|
||||
|
@ -17,10 +20,27 @@ pub const FISHING_AREA_RADIUS: f32 = 100.0;
|
|||
pub fn start_game() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_plugins(FrameTimeDiagnosticsPlugin)
|
||||
.add_systems(
|
||||
Startup,
|
||||
(setup_camera, Water::setup_water, Player::setup_player),
|
||||
(
|
||||
FPSText::setup,
|
||||
UserCamera::setup,
|
||||
Water::setup,
|
||||
Player::setup,
|
||||
FishCounterText::setup,
|
||||
KeybindingsText::setup,
|
||||
),
|
||||
)
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
FPSText::update,
|
||||
UserCamera::update,
|
||||
Player::r#move,
|
||||
Water::fishing,
|
||||
FishCounterText::update,
|
||||
),
|
||||
)
|
||||
.add_systems(Update, (update_camera, Player::move_player, Water::fishing))
|
||||
.run();
|
||||
}
|
||||
|
|
|
@ -3,37 +3,39 @@ use bevy::prelude::*;
|
|||
use crate::CHARACTER_SPEED;
|
||||
|
||||
#[derive(Debug, Component)]
|
||||
pub struct Player;
|
||||
pub struct Player {
|
||||
pub fish_count: usize,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
pub fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
pub fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
let image = asset_server.load::<Image>("character.png");
|
||||
commands.spawn((
|
||||
Sprite::from_image(image),
|
||||
Transform::from_xyz(0.0, 0.0, 0.0),
|
||||
Player,
|
||||
Player { fish_count: 0 },
|
||||
));
|
||||
}
|
||||
|
||||
pub fn move_player(
|
||||
pub fn r#move(
|
||||
time: Res<Time>,
|
||||
mut character_transform: Query<&mut Transform, With<Player>>,
|
||||
mut player_query: Query<&mut Transform, With<Player>>,
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
) {
|
||||
let mut character_transform = character_transform.get_single_mut().unwrap();
|
||||
let mut player = player_query.get_single_mut().unwrap();
|
||||
|
||||
let translation = CHARACTER_SPEED * time.delta_secs();
|
||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||
character_transform.translation.y += translation;
|
||||
player.translation.y += translation;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyS) {
|
||||
character_transform.translation.y -= translation;
|
||||
player.translation.y -= translation;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||
character_transform.translation.x -= translation;
|
||||
player.translation.x -= translation;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyD) {
|
||||
character_transform.translation.x += translation;
|
||||
player.translation.x += translation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
86
src/ui.rs
Normal file
86
src/ui.rs
Normal file
|
@ -0,0 +1,86 @@
|
|||
use bevy::{
|
||||
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
use crate::player::Player;
|
||||
|
||||
#[derive(Debug, Component)]
|
||||
pub struct FPSText;
|
||||
|
||||
impl FPSText {
|
||||
pub fn setup(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
Text::new("FPS Uninitialized"),
|
||||
TextLayout::new_with_justify(JustifyText::Center),
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
..default()
|
||||
},
|
||||
FPSText,
|
||||
));
|
||||
}
|
||||
|
||||
pub fn update(
|
||||
diagnostics: Res<DiagnosticsStore>,
|
||||
mut fps_text_query: Query<&mut Text, With<FPSText>>,
|
||||
) {
|
||||
if let Some(fps) = diagnostics.get(&FrameTimeDiagnosticsPlugin::FPS) {
|
||||
if let Some(fps) = fps.smoothed() {
|
||||
if let Ok(mut fps_text) = fps_text_query.get_single_mut() {
|
||||
*fps_text = format!("FPS = {:.2}", fps).into();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Component)]
|
||||
pub struct FishCounterText;
|
||||
|
||||
impl FishCounterText {
|
||||
pub fn setup(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
Text2d::new("Fish Count = 0"),
|
||||
TextLayout::new_with_justify(JustifyText::Center),
|
||||
Transform::from_xyz(0.0, 0.0, 1.0),
|
||||
FishCounterText,
|
||||
));
|
||||
}
|
||||
|
||||
pub fn update(
|
||||
mut fish_counter_text_transform_query: Query<
|
||||
&mut Transform,
|
||||
(With<FishCounterText>, Without<Player>),
|
||||
>,
|
||||
mut fish_counter_text_query: Query<&mut Text2d, With<FishCounterText>>,
|
||||
player_transform_query: Query<&Transform, (With<Player>, Without<FishCounterText>)>,
|
||||
player_query: Query<&Player, With<Player>>,
|
||||
) {
|
||||
let mut fish_counter_text_transform =
|
||||
fish_counter_text_transform_query.get_single_mut().unwrap();
|
||||
let mut fish_counter_text = fish_counter_text_query.get_single_mut().unwrap();
|
||||
let player = player_query.get_single().unwrap();
|
||||
|
||||
let player_position = player_transform_query.get_single().unwrap();
|
||||
let mut new_fish_counter_text_position = player_position.translation.truncate().extend(1.0);
|
||||
new_fish_counter_text_position.x += 50.0;
|
||||
new_fish_counter_text_position.y += 50.0;
|
||||
|
||||
fish_counter_text_transform.translation = new_fish_counter_text_position;
|
||||
*fish_counter_text = format!("Fish Count = {}", player.fish_count).into();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Component)]
|
||||
pub struct KeybindingsText;
|
||||
|
||||
impl KeybindingsText {
|
||||
pub fn setup(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
Text2d::new("Press E When You Are Near To Lake"),
|
||||
TextLayout::new_with_justify(JustifyText::Center),
|
||||
Transform::from_xyz(0.0, -300.0, 1.0),
|
||||
));
|
||||
}
|
||||
}
|
10
src/water.rs
10
src/water.rs
|
@ -6,7 +6,7 @@ use crate::{player::Player, FISHING_AREA_RADIUS};
|
|||
pub struct Water;
|
||||
|
||||
impl Water {
|
||||
pub fn setup_water(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
pub fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
let image = asset_server.load::<Image>("water.png");
|
||||
let sprite = Sprite::from_image(image);
|
||||
commands.spawn((sprite.clone(), Transform::from_xyz(500.0, 0.0, -1.0), Water));
|
||||
|
@ -14,11 +14,13 @@ impl Water {
|
|||
}
|
||||
|
||||
pub fn fishing(
|
||||
player_query: Query<&Transform, With<Player>>,
|
||||
player_transform_query: Query<&Transform, With<Player>>,
|
||||
mut player_query: Query<&mut Player, With<Player>>,
|
||||
water_query: Query<&Transform, With<Water>>,
|
||||
keyboard: Res<ButtonInput<KeyCode>>,
|
||||
) {
|
||||
let player_transform = player_query.get_single().unwrap();
|
||||
let player_transform = player_transform_query.get_single().unwrap();
|
||||
let mut player = player_query.get_single_mut().unwrap();
|
||||
|
||||
for water_query in &water_query {
|
||||
let distance = player_transform
|
||||
|
@ -27,7 +29,7 @@ impl Water {
|
|||
|
||||
if distance < FISHING_AREA_RADIUS {
|
||||
if keyboard.just_pressed(KeyCode::KeyE) {
|
||||
println!("You caught a fish");
|
||||
player.fish_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue