diff --git a/assets/market.png b/assets/market.png new file mode 100644 index 0000000..b68b37a Binary files /dev/null and b/assets/market.png differ diff --git a/src/lib.rs b/src/lib.rs index c44d013..be43844 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,18 +7,21 @@ use bevy::{ DefaultPlugins, }; use camera::UserCamera; +use market::Market; use player::Player; -use ui::{FPSText, FishCounterText, KeybindingsText}; +use ui::{FPSText, FishCounterText, KeybindingsText, MoneyText}; use water::Water; mod camera; +mod market; mod player; mod ui; mod water; pub const CHARACTER_SPEED: f32 = 1000.0; pub const CAMERA_DECAY_RATE: f32 = 3.0; -pub const FISHING_AREA_RADIUS: f32 = 100.0; +pub const INTERACTION_AREA_RADIUS: f32 = 100.0; +pub const FISH_PRICE: usize = 15; pub fn start_game() { App::new() @@ -38,8 +41,10 @@ pub fn start_game() { FPSText::setup, UserCamera::setup, Water::setup, + Market::setup, Player::setup, FishCounterText::setup, + MoneyText::setup, KeybindingsText::setup, ), ) @@ -50,7 +55,9 @@ pub fn start_game() { UserCamera::update, Player::r#move.before(FishCounterText::update), Water::fishing, + Market::sell.chain().before(MoneyText::update), FishCounterText::update, + MoneyText::update, ), ) .run(); diff --git a/src/market.rs b/src/market.rs new file mode 100644 index 0000000..b06948e --- /dev/null +++ b/src/market.rs @@ -0,0 +1,41 @@ +use bevy::prelude::*; + +use crate::{player::Player, FISH_PRICE, INTERACTION_AREA_RADIUS}; + +#[derive(Debug, Component)] +pub struct Market; + +impl Market { + pub fn setup(mut commands: Commands, asset_server: Res) { + let image = asset_server.load::("market.png"); + let sprite = Sprite::from_image(image); + commands.spawn(( + sprite.clone(), + Transform::from_xyz(0.0, 200.0, -1.0), + Market, + )); + } + + pub fn sell( + player_transform_query: Query<&Transform, With>, + mut player_query: Query<&mut Player, With>, + market_query: Query<&Transform, With>, + keyboard: Res>, + ) { + let player_transform = player_transform_query.get_single().unwrap(); + let mut player = player_query.get_single_mut().unwrap(); + + for market_query in &market_query { + let distance = player_transform + .translation + .distance(market_query.translation); + + if distance < INTERACTION_AREA_RADIUS { + if keyboard.just_pressed(KeyCode::KeyE) { + player.money = player.fish_count * FISH_PRICE; + player.fish_count = 0; + } + } + } + } +} diff --git a/src/player.rs b/src/player.rs index 027e938..7d0c500 100644 --- a/src/player.rs +++ b/src/player.rs @@ -5,6 +5,7 @@ use crate::CHARACTER_SPEED; #[derive(Debug, Component)] pub struct Player { pub fish_count: usize, + pub money: usize, } impl Player { @@ -13,7 +14,10 @@ impl Player { commands.spawn(( Sprite::from_image(image), Transform::from_xyz(0.0, 0.0, 0.0), - Player { fish_count: 0 }, + Player { + fish_count: 0, + money: 0, + }, )); } diff --git a/src/ui.rs b/src/ui.rs index aa1350c..74ce3a0 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -9,6 +9,7 @@ use crate::player::Player; const FISH_COUNTER_OFFSET: Vec3 = Vec3::new(50.0, 50.0, 1.0); static FISH_COUNT: LazyLock>> = LazyLock::new(|| Arc::new(RwLock::new(0))); +static MONEY: LazyLock>> = LazyLock::new(|| Arc::new(RwLock::new(0))); #[derive(Debug, Component)] pub struct FPSText; @@ -84,9 +85,41 @@ pub struct KeybindingsText; impl KeybindingsText { pub fn setup(mut commands: Commands) { commands.spawn(( - Text2d::new("Press E When You Are Near To Lake"), + Text2d::new("E is Interaction Key"), TextLayout::new_with_justify(JustifyText::Center), - Transform::from_xyz(0.0, -300.0, 1.0), + Transform::from_xyz(0.0, -100.0, 1.0), )); } } + +#[derive(Debug, Component)] +pub struct MoneyText; + +impl MoneyText { + pub fn setup(mut commands: Commands) { + commands.spawn(( + Text::new("Money = 0"), + TextLayout::new_with_justify(JustifyText::Center), + Node { + position_type: PositionType::Absolute, + top: Val::Px(5.0), + right: Val::Px(5.0), + ..default() + }, + MoneyText, + )); + } + + pub fn update( + mut money_text_query: Query<&mut Text, With>, + player_query: Query<&Player, With>, + ) { + let mut fish_counter_text = money_text_query.get_single_mut().unwrap(); + let player = player_query.get_single().unwrap(); + + if *MONEY.read().unwrap() != player.fish_count { + *MONEY.write().unwrap() = player.fish_count; + *fish_counter_text = format!("Money = {}", player.money).into(); + } + } +} diff --git a/src/water.rs b/src/water.rs index 1a934c9..ec5d21c 100644 --- a/src/water.rs +++ b/src/water.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; -use crate::{player::Player, FISHING_AREA_RADIUS}; +use crate::{player::Player, INTERACTION_AREA_RADIUS}; #[derive(Debug, Component)] pub struct Water; @@ -27,7 +27,7 @@ impl Water { .translation .distance(water_query.translation); - if distance < FISHING_AREA_RADIUS { + if distance < INTERACTION_AREA_RADIUS { if keyboard.just_pressed(KeyCode::KeyE) { player.fish_count += 1; }