handle incoming buzzer messages
This commit is contained in:
parent
3bb58b5719
commit
d9d7325e04
@ -84,4 +84,8 @@ impl Screen for LoginScreen {
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn handle_messages(&mut self, ctx: &mut Context) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,14 @@ impl State {
|
||||
Self::PlayerScreen(player_screen) => player_screen.handle_frame(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_messages(&mut self, ctx: &mut Context) {
|
||||
match self {
|
||||
Self::Init => {}
|
||||
State::Login(login_screen) => login_screen.handle_messages(ctx),
|
||||
State::PlayerScreen(player_screen) => player_screen.handle_messages(ctx),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum StateTransition {
|
||||
@ -51,6 +59,8 @@ async fn main() {
|
||||
state.transition_state(StateTransition::Init);
|
||||
|
||||
loop {
|
||||
state.handle_messages(&mut context);
|
||||
|
||||
if let Some(transition) = state.handle_frame(&mut context) {
|
||||
state = state.transition_state(transition);
|
||||
}
|
||||
|
@ -26,6 +26,28 @@ impl PlayerScreen {
|
||||
}
|
||||
|
||||
impl Screen for PlayerScreen {
|
||||
fn handle_messages(&mut self, ctx: &mut Context) {
|
||||
let msg = ctx.recv_msg();
|
||||
|
||||
if let Some(msg) = msg {
|
||||
match msg {
|
||||
crate::model::GameResponse::JoinResponse { .. } => {}
|
||||
crate::model::GameResponse::AckBuzzer => {
|
||||
info!("Button pressed ack");
|
||||
self.button_pressed = true;
|
||||
}
|
||||
crate::model::GameResponse::ResetBuzzer => {
|
||||
info!("Button press reset");
|
||||
self.button_pressed = false;
|
||||
}
|
||||
crate::model::GameResponse::UpdatePountTotal { score } => {
|
||||
info!("Score updated to: {}", score);
|
||||
self.score = score;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_frame(&mut self, ctx: &mut Context) -> Option<StateTransition> {
|
||||
clear_background(BEIGE);
|
||||
|
||||
@ -35,6 +57,14 @@ impl Screen for PlayerScreen {
|
||||
RED
|
||||
};
|
||||
|
||||
draw_text(
|
||||
&format!("Score: {}", self.score),
|
||||
screen_width() * 0.5,
|
||||
screen_height() * 0.25,
|
||||
50.0,
|
||||
BLACK,
|
||||
);
|
||||
|
||||
let button_size = (screen_width() * 0.33).min(screen_height() * 0.20);
|
||||
draw_circle(
|
||||
screen_width() * 0.5,
|
||||
@ -49,7 +79,7 @@ impl Screen for PlayerScreen {
|
||||
button_color,
|
||||
);
|
||||
|
||||
if is_mouse_button_pressed(MouseButton::Left) {
|
||||
if !self.button_pressed && is_mouse_button_pressed(MouseButton::Left) {
|
||||
let loc = Vec2::from(mouse_position());
|
||||
let normalize = loc - Vec2::new(screen_width() * 0.5, screen_height() * 0.5);
|
||||
|
||||
@ -60,7 +90,6 @@ impl Screen for PlayerScreen {
|
||||
let time = unix_timestamp.as_secs_f64();
|
||||
|
||||
info!("Button pressed @ {}", time);
|
||||
self.button_pressed = true;
|
||||
let buzz_in = BuzzIn { time };
|
||||
|
||||
ctx.send_msg(&ClientMessage::BuzzIn(buzz_in));
|
||||
|
@ -3,4 +3,5 @@ use crate::{StateTransition, context::Context};
|
||||
pub trait Screen {
|
||||
fn init_frame(&mut self) {}
|
||||
fn handle_frame(&mut self, ctx: &mut Context) -> Option<StateTransition>;
|
||||
fn handle_messages(&mut self, ctx: &mut Context);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user