Compare commits

...

1 Commits
main ... emtpy

Author SHA1 Message Date
Andrei Stoica 71b5d38c07 wip 2024-11-18 15:42:43 -05:00
4 changed files with 4406 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"

66
src/components.rs Normal file
View File

@ -0,0 +1,66 @@
use std::fmt::Display;
use bevy::prelude::Component;
#[derive(Component, Debug)]
pub struct Purchased(pub u32);
#[derive(Component, Debug)]
pub enum PurchasableObject {
Cookie,
Cursor,
Grandma,
}
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,
_ => unimplemented!(),
}
}
pub fn get_scaling_factor(object: &PurchasableObject) -> f64 {
match object {
PurchasableObject::Cookie => 1.10,
PurchasableObject::Cursor => 1.15,
_ => unimplemented!(),
}
}
pub fn get_base_returns(object: &PurchasableObject) -> f64 {
match object {
PurchasableObject::Cookie => 1.,
PurchasableObject::Cursor => 2.,
_ => unimplemented!(),
}
}

View File

@ -1,3 +1,55 @@
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());
}
fn get_cash(inv: Query<(&PurchasableObject, &Purchased)>, mut stats: Query<&mut PlayerStats>) {
let mut new_cash: f64 = 0.;
for (item, Purchased(count)) in &inv {
new_cash += get_base_returns(item) * f64::from(*count);
}
stats.single_mut().money += new_cash;
}
fn display(inv: Query<(&PurchasableObject, &Purchased)>, stats: Query<&PlayerStats>) {
println!("money: {}", stats.single().money);
println!("items:");
for (item, Purchased(count)) in &inv {
println!("\t{}: {}", item, count);
}
}
fn handle_input(
keys: Res<ButtonInput<KeyCode>>,
mut commands: Commands,
mut pur_items: Query<&mut Purchased>,
mut stats: Query<&mut PlayerStats>,
) {
let items = 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();
let cur_count = pur_items.get_mut(item);
});
}