use bevy::prelude::*; use std::collections::{HashMap, HashSet}; #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum Item { Ration, Firewood, Bedroll, Lantern, Parcel, Letter, PoetryBook, TinWhistle, Tobacco, JacksLetter, Ribbon, } impl Item { pub fn name(self) -> &'static str { match self { Item::Ration => "Ration", Item::Firewood => "Firewood", Item::Bedroll => "Bedroll", Item::Lantern => "Lantern", Item::Parcel => "Parcel", Item::Letter => "Sealed Letter", Item::PoetryBook => "Songs of the Moor", Item::TinWhistle => "Tin Whistle", Item::Tobacco => "Pipe Tobacco", Item::JacksLetter => "Jack's Letter", Item::Ribbon => "Silk Ribbon", } } } #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum Npc { Hale, Pike, Bess, Edwin, OldTom, Annie, Foreman, Quill, Weaver, Peddler, Miner, Jack, Pawn, } impl Npc { pub fn name(self) -> &'static str { match self { Npc::Hale => "Mr. Hale, Postmaster", Npc::Pike => "Mr. Pike, Grocer", Npc::Bess => "Bess, Innkeeper", Npc::Edwin => "Edwin, Schoolmaster", Npc::OldTom => "Old Tom", Npc::Annie => "Little Sam", Npc::Foreman => "Mill Foreman", Npc::Quill => "Mr. Quill, Bookseller", Npc::Weaver => "Will, Weaver", Npc::Peddler => "Peddler", Npc::Miner => "Collier", Npc::Jack => "Jack Turner", Npc::Pawn => "Pawnbroker", } } } #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum Obj { HomeBed, BalePile, BaleDrop, Signpost, MillInn, CoalInn, } #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum Ev { BrokenCart, Highwayman, Storm, Peddler, Lost, Wolves, } #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum Talk { Npc(Npc), Obj(Obj), Ev(Ev), Camp, } #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)] pub enum Zone { #[default] Ashford, Millbrook, Coalbridge, Road, } impl Zone { pub fn name(self) -> &'static str { match self { Zone::Ashford => "Ashford", Zone::Millbrook => "Millbrook", Zone::Coalbridge => "Coalbridge", Zone::Road => "The Road", } } } #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Sfx { Coin, Door, Page, Bag, Arrive, Home, Lantern, Howl, Growl, } #[derive(Clone, Copy, PartialEq, Debug)] pub enum Req { Sleep { until: f32, camp: bool }, Wait { minutes: f32 }, BreakCamp, } pub type Effect = fn(&mut Game); pub struct Choice { pub label: String, pub next: Option<&'static str>, pub effect: Option, } pub struct Dialog { pub speaker: String, pub text: String, pub choices: Vec, } impl Dialog { pub fn new(speaker: impl Into, text: impl Into) -> Self { Self { speaker: speaker.into(), text: text.into(), choices: Vec::new() } } pub fn opt(mut self, label: impl Into, next: Option<&'static str>, effect: Option) -> Self { self.choices.push(Choice { label: label.into(), next, effect }); self } pub fn go(self, label: impl Into, next: &'static str) -> Self { self.opt(label, Some(next), None) } pub fn act(self, label: impl Into, next: Option<&'static str>, effect: Effect) -> Self { self.opt(label, next, Some(effect)) } pub fn bye(self, label: impl Into) -> Self { self.opt(label, None, None) } } #[derive(Resource)] pub struct Game { pub day: u32, pub minutes: f32, pub hunger: f32, pub energy: f32, pub warmth: f32, pub money: i32, pub items: Vec, pub hearts: HashMap, pub flags: HashSet<&'static str>, pub counters: HashMap<&'static str, i32>, pub zone: Zone, pub dialog: Option<(Talk, &'static str)>, pub toasts: Vec, pub sfx: Vec, pub request: Option, pub camping: bool, pub timelapse: bool, pub raining: bool, } impl Default for Game { fn default() -> Self { Self { day: 1, minutes: 9.0 * 60.0, hunger: 85.0, energy: 90.0, warmth: 80.0, money: 6, items: vec![Item::Ration], hearts: HashMap::new(), flags: HashSet::new(), counters: HashMap::new(), zone: Zone::Ashford, dialog: None, toasts: Vec::new(), sfx: Vec::new(), request: None, camping: false, timelapse: false, raining: false, } } } impl Game { pub fn hour(&self) -> f32 { self.minutes / 60.0 } pub fn is_night(&self) -> bool { let h = self.hour(); !(6.0..20.0).contains(&h) } pub fn has(&self, i: Item) -> bool { self.items.contains(&i) } pub fn count(&self, i: Item) -> usize { self.items.iter().filter(|x| **x == i).count() } pub fn give(&mut self, i: Item) { self.items.push(i); self.toasts.push(format!("+ {}", i.name())); self.sfx.push(Sfx::Bag); } pub fn take(&mut self, i: Item) -> bool { if let Some(p) = self.items.iter().position(|x| *x == i) { self.items.remove(p); true } else { false } } pub fn pay(&mut self, amount: i32) -> bool { if self.money < amount { self.toasts.push("Not enough coin".into()); return false; } self.money -= amount; self.sfx.push(Sfx::Coin); true } pub fn earn(&mut self, amount: i32) { self.money += amount; self.toasts.push(format!("+ {amount}s")); self.sfx.push(Sfx::Coin); } pub fn buy(&mut self, i: Item, price: i32) -> bool { if self.pay(price) { self.give(i); true } else { false } } pub fn heart(&self, n: Npc) -> i32 { *self.hearts.get(&n).unwrap_or(&0) } pub fn bond(&mut self, n: Npc, d: i32) { let h = self.hearts.entry(n).or_insert(0); *h = (*h + d).clamp(0, 5); if d > 0 { self.toasts.push(format!("{} \u{2665}", n.name())); } } pub fn flag(&self, f: &'static str) -> bool { self.flags.contains(f) } pub fn set(&mut self, f: &'static str) { self.flags.insert(f); } pub fn unset(&mut self, f: &'static str) { self.flags.remove(f); } pub fn counter(&self, c: &'static str) -> i32 { *self.counters.get(c).unwrap_or(&0) } pub fn add(&mut self, c: &'static str, d: i32) { *self.counters.entry(c).or_insert(0) += d; } pub fn advance(&mut self, minutes: f32) { self.minutes += minutes; while self.minutes >= 1440.0 { self.minutes -= 1440.0; self.day += 1; } } pub fn clock(&self) -> String { let m = self.minutes as i32; format!("Day {} {:02}:{:02}", self.day, m / 60, m % 60) } }