Compare commits
1 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
71b5d38c07 |
File diff suppressed because it is too large
Load Diff
|
|
@ -4,3 +4,4 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bevy = "0.14.2"
|
||||||
|
|
|
||||||
|
|
@ -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!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
54
src/main.rs
54
src/main.rs
|
|
@ -1,3 +1,55 @@
|
||||||
|
mod components;
|
||||||
|
|
||||||
|
use bevy::app::{App, FixedUpdate};
|
||||||
|
use bevy::prelude::*;
|
||||||
|
use components::*;
|
||||||
|
|
||||||
fn main() {
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue