forge
Explore
Log in

Don't you dare go Hollow

courier

Clone
★ 0

0 watching0 forksRustAGPL-3.05.3 MiB6d9eeb9Tahinli21 days ago3 commits

game.rs307 lines, 7.3 KiBHistoryRawDownload
1use bevy::prelude::*;
2use std::collections::{HashMap, HashSet};
3
4#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
5pub enum Item {
6 Ration,
7 Firewood,
8 Bedroll,
9 Lantern,
10 Parcel,
11 Letter,
12 PoetryBook,
13 TinWhistle,
14 Tobacco,
15 JacksLetter,
16 Ribbon,
17}
18
19impl Item {
20 pub fn name(self) -> &'static str {
21 match self {
22 Item::Ration => "Ration",
23 Item::Firewood => "Firewood",
24 Item::Bedroll => "Bedroll",
25 Item::Lantern => "Lantern",
26 Item::Parcel => "Parcel",
27 Item::Letter => "Sealed Letter",
28 Item::PoetryBook => "Songs of the Moor",
29 Item::TinWhistle => "Tin Whistle",
30 Item::Tobacco => "Pipe Tobacco",
31 Item::JacksLetter => "Jack's Letter",
32 Item::Ribbon => "Silk Ribbon",
33 }
34 }
35}
36
37#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
38pub enum Npc {
39 Hale,
40 Pike,
41 Bess,
42 Edwin,
43 OldTom,
44 Annie,
45 Foreman,
46 Quill,
47 Weaver,
48 Peddler,
49 Miner,
50 Jack,
51 Pawn,
52}
53
54impl Npc {
55 pub fn name(self) -> &'static str {
56 match self {
57 Npc::Hale => "Mr. Hale, Postmaster",
58 Npc::Pike => "Mr. Pike, Grocer",
59 Npc::Bess => "Bess, Innkeeper",
60 Npc::Edwin => "Edwin, Schoolmaster",
61 Npc::OldTom => "Old Tom",
62 Npc::Annie => "Little Sam",
63 Npc::Foreman => "Mill Foreman",
64 Npc::Quill => "Mr. Quill, Bookseller",
65 Npc::Weaver => "Will, Weaver",
66 Npc::Peddler => "Peddler",
67 Npc::Miner => "Collier",
68 Npc::Jack => "Jack Turner",
69 Npc::Pawn => "Pawnbroker",
70 }
71 }
72}
73
74#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
75pub enum Obj {
76 HomeBed,
77 BalePile,
78 BaleDrop,
79 Signpost,
80 MillInn,
81 CoalInn,
82}
83
84#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
85pub enum Ev {
86 BrokenCart,
87 Highwayman,
88 Storm,
89 Peddler,
90 Lost,
91 Wolves,
92}
93
94#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
95pub enum Talk {
96 Npc(Npc),
97 Obj(Obj),
98 Ev(Ev),
99 Camp,
100}
101
102#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
103pub enum Zone {
104 #[default]
105 Ashford,
106 Millbrook,
107 Coalbridge,
108 Road,
109}
110
111impl Zone {
112 pub fn name(self) -> &'static str {
113 match self {
114 Zone::Ashford => "Ashford",
115 Zone::Millbrook => "Millbrook",
116 Zone::Coalbridge => "Coalbridge",
117 Zone::Road => "The Road",
118 }
119 }
120}
121
122#[derive(Clone, Copy, PartialEq, Eq, Debug)]
123pub enum Sfx {
124 Coin,
125 Door,
126 Page,
127 Bag,
128 Arrive,
129 Home,
130 Lantern,
131 Howl,
132 Growl,
133}
134
135#[derive(Clone, Copy, PartialEq, Debug)]
136pub enum Req {
137 Sleep { until: f32, camp: bool },
138 Wait { minutes: f32 },
139 BreakCamp,
140}
141
142pub type Effect = fn(&mut Game);
143
144pub struct Choice {
145 pub label: String,
146 pub next: Option<&'static str>,
147 pub effect: Option<Effect>,
148}
149
150pub struct Dialog {
151 pub speaker: String,
152 pub text: String,
153 pub choices: Vec<Choice>,
154}
155
156impl Dialog {
157 pub fn new(speaker: impl Into<String>, text: impl Into<String>) -> Self {
158 Self { speaker: speaker.into(), text: text.into(), choices: Vec::new() }
159 }
160 pub fn opt(mut self, label: impl Into<String>, next: Option<&'static str>, effect: Option<Effect>) -> Self {
161 self.choices.push(Choice { label: label.into(), next, effect });
162 self
163 }
164 pub fn go(self, label: impl Into<String>, next: &'static str) -> Self {
165 self.opt(label, Some(next), None)
166 }
167 pub fn act(self, label: impl Into<String>, next: Option<&'static str>, effect: Effect) -> Self {
168 self.opt(label, next, Some(effect))
169 }
170 pub fn bye(self, label: impl Into<String>) -> Self {
171 self.opt(label, None, None)
172 }
173}
174
175#[derive(Resource)]
176pub struct Game {
177 pub day: u32,
178 pub minutes: f32,
179 pub hunger: f32,
180 pub energy: f32,
181 pub warmth: f32,
182 pub money: i32,
183 pub items: Vec<Item>,
184 pub hearts: HashMap<Npc, i32>,
185 pub flags: HashSet<&'static str>,
186 pub counters: HashMap<&'static str, i32>,
187 pub zone: Zone,
188 pub dialog: Option<(Talk, &'static str)>,
189 pub toasts: Vec<String>,
190 pub sfx: Vec<Sfx>,
191 pub request: Option<Req>,
192 pub camping: bool,
193 pub timelapse: bool,
194 pub raining: bool,
195}
196
197impl Default for Game {
198 fn default() -> Self {
199 Self {
200 day: 1,
201 minutes: 9.0 * 60.0,
202 hunger: 85.0,
203 energy: 90.0,
204 warmth: 80.0,
205 money: 6,
206 items: vec![Item::Ration],
207 hearts: HashMap::new(),
208 flags: HashSet::new(),
209 counters: HashMap::new(),
210 zone: Zone::Ashford,
211 dialog: None,
212 toasts: Vec::new(),
213 sfx: Vec::new(),
214 request: None,
215 camping: false,
216 timelapse: false,
217 raining: false,
218 }
219 }
220}
221
222impl Game {
223 pub fn hour(&self) -> f32 {
224 self.minutes / 60.0
225 }
226 pub fn is_night(&self) -> bool {
227 let h = self.hour();
228 !(6.0..20.0).contains(&h)
229 }
230 pub fn has(&self, i: Item) -> bool {
231 self.items.contains(&i)
232 }
233 pub fn count(&self, i: Item) -> usize {
234 self.items.iter().filter(|x| **x == i).count()
235 }
236 pub fn give(&mut self, i: Item) {
237 self.items.push(i);
238 self.toasts.push(format!("+ {}", i.name()));
239 self.sfx.push(Sfx::Bag);
240 }
241 pub fn take(&mut self, i: Item) -> bool {
242 if let Some(p) = self.items.iter().position(|x| *x == i) {
243 self.items.remove(p);
244 true
245 } else {
246 false
247 }
248 }
249 pub fn pay(&mut self, amount: i32) -> bool {
250 if self.money < amount {
251 self.toasts.push("Not enough coin".into());
252 return false;
253 }
254 self.money -= amount;
255 self.sfx.push(Sfx::Coin);
256 true
257 }
258 pub fn earn(&mut self, amount: i32) {
259 self.money += amount;
260 self.toasts.push(format!("+ {amount}s"));
261 self.sfx.push(Sfx::Coin);
262 }
263 pub fn buy(&mut self, i: Item, price: i32) -> bool {
264 if self.pay(price) {
265 self.give(i);
266 true
267 } else {
268 false
269 }
270 }
271 pub fn heart(&self, n: Npc) -> i32 {
272 *self.hearts.get(&n).unwrap_or(&0)
273 }
274 pub fn bond(&mut self, n: Npc, d: i32) {
275 let h = self.hearts.entry(n).or_insert(0);
276 *h = (*h + d).clamp(0, 5);
277 if d > 0 {
278 self.toasts.push(format!("{} \u{2665}", n.name()));
279 }
280 }
281 pub fn flag(&self, f: &'static str) -> bool {
282 self.flags.contains(f)
283 }
284 pub fn set(&mut self, f: &'static str) {
285 self.flags.insert(f);
286 }
287 pub fn unset(&mut self, f: &'static str) {
288 self.flags.remove(f);
289 }
290 pub fn counter(&self, c: &'static str) -> i32 {
291 *self.counters.get(c).unwrap_or(&0)
292 }
293 pub fn add(&mut self, c: &'static str, d: i32) {
294 *self.counters.entry(c).or_insert(0) += d;
295 }
296 pub fn advance(&mut self, minutes: f32) {
297 self.minutes += minutes;
298 while self.minutes >= 1440.0 {
299 self.minutes -= 1440.0;
300 self.day += 1;
301 }
302 }
303 pub fn clock(&self) -> String {
304 let m = self.minutes as i32;
305 format!("Day {} {:02}:{:02}", self.day, m / 60, m % 60)
306 }
307}