feat: ✨ market
This commit is contained in:
parent
79f115c6ef
commit
9f52e24582
6 changed files with 92 additions and 7 deletions
11
src/lib.rs
11
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();
|
||||
|
|
41
src/market.rs
Normal file
41
src/market.rs
Normal file
|
@ -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<AssetServer>) {
|
||||
let image = asset_server.load::<Image>("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<Player>>,
|
||||
mut player_query: Query<&mut Player, With<Player>>,
|
||||
market_query: Query<&Transform, With<Market>>,
|
||||
keyboard: Res<ButtonInput<KeyCode>>,
|
||||
) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
|
|
37
src/ui.rs
37
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<Arc<RwLock<usize>>> = LazyLock::new(|| Arc::new(RwLock::new(0)));
|
||||
static MONEY: LazyLock<Arc<RwLock<usize>>> = 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<MoneyText>>,
|
||||
player_query: Query<&Player, With<Player>>,
|
||||
) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue