-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement gadgets in the backend #33
base: main
Are you sure you want to change the base?
Changes from all commits
938ec4f
ec9b561
7e128b8
2757a72
42abbcf
e1b9761
9f1ac5c
22a30db
60937c6
b007a9e
823ee28
853bd5b
7b30e14
9de649d
8c1b926
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::collections::HashSet; | ||
use std::mem; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(specta::Type, Clone, Serialize, Deserialize, Debug)] | ||
pub enum MrXGadget { | ||
AlternativeFacts { stop_id: String }, | ||
Midjourney { image: Vec<u8> }, | ||
NotFound, | ||
Teleport, | ||
Shifter, | ||
} | ||
|
||
#[derive(specta::Type, Clone, Serialize, Deserialize, Debug)] | ||
pub enum DetectiveGadget { | ||
Stop { stop_id: String }, | ||
OutOfOrder, | ||
Shackles, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct GadgetState<T> { | ||
can_be_used: bool, | ||
cooldown: Option<f32>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be cleaner to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The cooldown is the number of seconds remaining, using a |
||
used: HashSet<mem::Discriminant<T>>, | ||
} | ||
|
||
impl<T> GadgetState<T> { | ||
pub fn new() -> Self { | ||
Self { | ||
can_be_used: false, | ||
cooldown: None, | ||
used: HashSet::new(), | ||
} | ||
} | ||
|
||
pub fn update_time(&mut self, delta: f32) { | ||
if let Some(cooldown) = self.cooldown.as_mut() { | ||
*cooldown -= delta; | ||
if *cooldown < 0.0 { | ||
self.cooldown = None; | ||
} | ||
} | ||
} | ||
|
||
pub fn remaining(&self) -> Option<f32> { | ||
self.cooldown | ||
} | ||
|
||
pub fn try_use(&mut self, gadget: &T, cooldown: f32) -> bool { | ||
if self.can_be_used && self.cooldown.is_none() && self.used.insert(mem::discriminant(gadget)) { | ||
self.cooldown = Some(cooldown); | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
pub fn allow_use(&mut self) { | ||
self.can_be_used = true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would speak against using just one enum? You could add a function
is_usable_by(team: Team) -> bool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main advantage is that it's harder for the frontend to accidentally send a gadget from the wrong team. Using a single enum might be cleaner though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another advantage is that there are separate countdowns for Mr. X and Detective gadgets.