feat: ✨ market
This commit is contained in:
parent
79f115c6ef
commit
9f52e24582
6 changed files with 92 additions and 7 deletions
BIN
assets/market.png
Normal file
BIN
assets/market.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 223 B |
11
src/lib.rs
11
src/lib.rs
|
@ -7,18 +7,21 @@ use bevy::{
|
||||||
DefaultPlugins,
|
DefaultPlugins,
|
||||||
};
|
};
|
||||||
use camera::UserCamera;
|
use camera::UserCamera;
|
||||||
|
use market::Market;
|
||||||
use player::Player;
|
use player::Player;
|
||||||
use ui::{FPSText, FishCounterText, KeybindingsText};
|
use ui::{FPSText, FishCounterText, KeybindingsText, MoneyText};
|
||||||
use water::Water;
|
use water::Water;
|
||||||
|
|
||||||
mod camera;
|
mod camera;
|
||||||
|
mod market;
|
||||||
mod player;
|
mod player;
|
||||||
mod ui;
|
mod ui;
|
||||||
mod water;
|
mod water;
|
||||||
|
|
||||||
pub const CHARACTER_SPEED: f32 = 1000.0;
|
pub const CHARACTER_SPEED: f32 = 1000.0;
|
||||||
pub const CAMERA_DECAY_RATE: f32 = 3.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() {
|
pub fn start_game() {
|
||||||
App::new()
|
App::new()
|
||||||
|
@ -38,8 +41,10 @@ pub fn start_game() {
|
||||||
FPSText::setup,
|
FPSText::setup,
|
||||||
UserCamera::setup,
|
UserCamera::setup,
|
||||||
Water::setup,
|
Water::setup,
|
||||||
|
Market::setup,
|
||||||
Player::setup,
|
Player::setup,
|
||||||
FishCounterText::setup,
|
FishCounterText::setup,
|
||||||
|
MoneyText::setup,
|
||||||
KeybindingsText::setup,
|
KeybindingsText::setup,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -50,7 +55,9 @@ pub fn start_game() {
|
||||||
UserCamera::update,
|
UserCamera::update,
|
||||||
Player::r#move.before(FishCounterText::update),
|
Player::r#move.before(FishCounterText::update),
|
||||||
Water::fishing,
|
Water::fishing,
|
||||||
|
Market::sell.chain().before(MoneyText::update),
|
||||||
FishCounterText::update,
|
FishCounterText::update,
|
||||||
|
MoneyText::update,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.run();
|
.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)]
|
#[derive(Debug, Component)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
pub fish_count: usize,
|
pub fish_count: usize,
|
||||||
|
pub money: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
|
@ -13,7 +14,10 @@ impl Player {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Sprite::from_image(image),
|
Sprite::from_image(image),
|
||||||
Transform::from_xyz(0.0, 0.0, 0.0),
|
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);
|
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 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)]
|
#[derive(Debug, Component)]
|
||||||
pub struct FPSText;
|
pub struct FPSText;
|
||||||
|
@ -84,9 +85,41 @@ pub struct KeybindingsText;
|
||||||
impl KeybindingsText {
|
impl KeybindingsText {
|
||||||
pub fn setup(mut commands: Commands) {
|
pub fn setup(mut commands: Commands) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Text2d::new("Press E When You Are Near To Lake"),
|
Text2d::new("E is Interaction Key"),
|
||||||
TextLayout::new_with_justify(JustifyText::Center),
|
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 bevy::prelude::*;
|
||||||
|
|
||||||
use crate::{player::Player, FISHING_AREA_RADIUS};
|
use crate::{player::Player, INTERACTION_AREA_RADIUS};
|
||||||
|
|
||||||
#[derive(Debug, Component)]
|
#[derive(Debug, Component)]
|
||||||
pub struct Water;
|
pub struct Water;
|
||||||
|
@ -27,7 +27,7 @@ impl Water {
|
||||||
.translation
|
.translation
|
||||||
.distance(water_query.translation);
|
.distance(water_query.translation);
|
||||||
|
|
||||||
if distance < FISHING_AREA_RADIUS {
|
if distance < INTERACTION_AREA_RADIUS {
|
||||||
if keyboard.just_pressed(KeyCode::KeyE) {
|
if keyboard.just_pressed(KeyCode::KeyE) {
|
||||||
player.fish_count += 1;
|
player.fish_count += 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue