purchasing items

This commit is contained in:
Andrei Stoica 2024-11-18 15:42:43 -05:00
parent 0112efc1fd
commit e130f91f39
4 changed files with 4431 additions and 1 deletions

4286
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"
[dependencies]
bevy = "0.14.2"

69
src/components.rs Normal file
View File

@ -0,0 +1,69 @@
use std::fmt::Display;
use bevy::prelude::Component;
#[derive(Debug, PartialEq)]
pub enum PurchasableObject {
Cookie,
Cursor,
Grandma,
}
#[derive(Component, Debug)]
pub struct Purchased {
pub object: PurchasableObject,
pub count: u32,
}
impl Display for PurchasableObject {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
PurchasableObject::Cookie => "Cookie",
PurchasableObject::Cursor => "Cursor",
PurchasableObject::Grandma => "Grandma",
}
)
}
}
#[derive(Component, Debug)]
pub struct PlayerStats {
pub money: f64,
}
impl PlayerStats {
pub fn new() -> PlayerStats {
return PlayerStats { money: 10.0 };
}
}
pub fn get_current_price(object: &PurchasableObject, num_owned: &u32) -> f64 {
get_base_price(object) * get_scaling_factor(object) * f64::from(*num_owned)
}
pub fn get_base_price(object: &PurchasableObject) -> f64 {
match object {
PurchasableObject::Cookie => 10.0,
PurchasableObject::Cursor => 100.0,
PurchasableObject::Grandma => 1000.0,
}
}
pub fn get_scaling_factor(object: &PurchasableObject) -> f64 {
match object {
PurchasableObject::Cookie => 1.10,
PurchasableObject::Cursor => 1.15,
PurchasableObject::Grandma => 1.20,
}
}
pub fn get_base_returns(object: &PurchasableObject) -> f64 {
match object {
PurchasableObject::Cookie => 1.0,
PurchasableObject::Cursor => 2.0,
PurchasableObject::Grandma => 5.0,
}
}

View File

@ -1,3 +1,77 @@
mod components;
use bevy::app::{App, FixedUpdate};
use bevy::prelude::*;
use components::*;
fn main() {
println!("Hello, world!");
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(FixedUpdate, (get_cash, display, handle_input))
.run();
}
fn setup(mut commands: Commands) {
commands.spawn(PlayerStats::new());
commands.spawn(Purchased {
object: PurchasableObject::Cookie,
count: 1,
});
}
fn get_cash(inv: Query<&Purchased>, mut stats: Query<&mut PlayerStats>) {
let mut new_cash: f64 = 0.;
for Purchased { object, count } in &inv {
new_cash += get_base_returns(&object) * f64::from(*count);
}
stats.single_mut().money += new_cash;
}
fn display(inv: Query<&Purchased>, stats: Query<&PlayerStats>) {
println!("money: {}", stats.single().money);
println!("items:");
for Purchased { object, count } in &inv {
println!("\t{}: {}", object, count);
}
}
fn handle_input(
keys: Res<ButtonInput<KeyCode>>,
mut commands: Commands,
mut pur_items: Query<&mut Purchased>,
mut stats: Query<&mut PlayerStats>,
) {
keys.get_just_pressed()
.filter_map(|key| match key {
KeyCode::KeyP => Some(PurchasableObject::Cookie),
KeyCode::KeyC => Some(PurchasableObject::Cursor),
KeyCode::KeyG => Some(PurchasableObject::Grandma),
_ => None,
})
.for_each(|item| {
let mut player_stats = stats.get_single_mut().unwrap();
for mut object in &mut pur_items {
if object.object == item {
let price = get_current_price(&item, &object.count);
if price <= player_stats.money {
object.count += 1;
player_stats.money -= price;
}
return;
}
}
if get_base_price(&item) <= player_stats.money {
println!("{}, {}", get_base_price(&item), player_stats.money);
player_stats.money -= get_base_price(&item);
commands.spawn(Purchased {
object: item,
count: 1,
});
}
});
}