Compare commits
3 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
5ab49b9610 | |
|
|
d0a788ae59 | |
|
|
e130f91f39 |
File diff suppressed because it is too large
Load Diff
|
|
@ -4,3 +4,4 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.14.2"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,87 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use bevy::prelude::Component;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum PurchasableObject {
|
||||
Cursor,
|
||||
Grandma,
|
||||
Farm,
|
||||
Mine,
|
||||
Factory,
|
||||
Bank,
|
||||
Temple,
|
||||
WizardTower,
|
||||
}
|
||||
|
||||
#[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::Cursor => "Cursor",
|
||||
PurchasableObject::Grandma => "Grandma",
|
||||
PurchasableObject::Farm => "Farm",
|
||||
PurchasableObject::Mine => "Mine",
|
||||
PurchasableObject::Factory => "Factory",
|
||||
PurchasableObject::Bank => "Bank",
|
||||
PurchasableObject::Temple => "Temple",
|
||||
PurchasableObject::WizardTower => "Wizard Tower",
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct PlayerStats {
|
||||
pub cookies: f64,
|
||||
}
|
||||
|
||||
impl PlayerStats {
|
||||
pub fn new() -> PlayerStats {
|
||||
return PlayerStats { cookies: 0.0 };
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_current_price(object: &PurchasableObject, num_owned: &u32) -> f64 {
|
||||
get_base_price(object) * (1.0 + get_price_scaling_factor(object) * f64::from(*num_owned))
|
||||
}
|
||||
|
||||
pub fn get_base_price(object: &PurchasableObject) -> f64 {
|
||||
match object {
|
||||
PurchasableObject::Cursor => 15.0,
|
||||
PurchasableObject::Grandma => 100.0,
|
||||
PurchasableObject::Farm => 1_100.0,
|
||||
PurchasableObject::Mine => 21_000.0,
|
||||
PurchasableObject::Factory => 130_000.0,
|
||||
PurchasableObject::Bank => 1_400_000.0,
|
||||
PurchasableObject::Temple => 20_000_000.0,
|
||||
PurchasableObject::WizardTower => 330_000_000.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_price_scaling_factor(object: &PurchasableObject) -> f64 {
|
||||
match object {
|
||||
_ => 0.15,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_base_returns(object: &PurchasableObject) -> f64 {
|
||||
match object {
|
||||
PurchasableObject::Cursor => 0.1,
|
||||
PurchasableObject::Grandma => 1.0,
|
||||
PurchasableObject::Farm => 8.0,
|
||||
PurchasableObject::Mine => 47.0,
|
||||
PurchasableObject::Factory => 260.0,
|
||||
PurchasableObject::Bank => 1_400.0,
|
||||
PurchasableObject::Temple => 7_800.0,
|
||||
PurchasableObject::WizardTower => 44_000.0,
|
||||
}
|
||||
}
|
||||
86
src/main.rs
86
src/main.rs
|
|
@ -1,3 +1,85 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
mod components;
|
||||
|
||||
use bevy::app::{App, FixedUpdate};
|
||||
use bevy::prelude::*;
|
||||
use components::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum InputAction {
|
||||
TapCookie,
|
||||
Purchase(PurchasableObject),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
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(time: Res<Time>, 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) * f64::from(time.delta_seconds());
|
||||
}
|
||||
|
||||
stats.single_mut().cookies += new_cash;
|
||||
}
|
||||
|
||||
fn display(inv: Query<&Purchased>, stats: Query<&PlayerStats>) {
|
||||
println!("money: {}", stats.single().cookies);
|
||||
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::Space => Some(InputAction::TapCookie),
|
||||
KeyCode::KeyC => Some(InputAction::Purchase(PurchasableObject::Cursor)),
|
||||
KeyCode::KeyG => Some(InputAction::Purchase(PurchasableObject::Grandma)),
|
||||
_ => None,
|
||||
})
|
||||
.for_each(|action| {
|
||||
let mut player_stats = stats.get_single_mut().unwrap();
|
||||
let item = if let InputAction::Purchase(item) = action {
|
||||
item
|
||||
} else {
|
||||
player_stats.cookies += 1.0;
|
||||
return;
|
||||
};
|
||||
|
||||
for mut object in &mut pur_items {
|
||||
if object.object == item {
|
||||
let price = get_current_price(&item, &object.count);
|
||||
if price <= player_stats.cookies {
|
||||
object.count += 1;
|
||||
player_stats.cookies -= price;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if get_base_price(&item) <= player_stats.cookies {
|
||||
println!("{}, {}", get_base_price(&item), player_stats.cookies);
|
||||
player_stats.cookies -= get_base_price(&item);
|
||||
commands.spawn(Purchased {
|
||||
object: item,
|
||||
count: 1,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue