Skip to content

Commit bdd8adb

Browse files
committed
Reimplement snake using the new input API
1 parent 9b602cd commit bdd8adb

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

lighthouse-client/examples/snake.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clap::Parser;
22
use futures::{Stream, lock::Mutex, StreamExt};
33
use lighthouse_client::{Lighthouse, Result, TokioWebSocket, LIGHTHOUSE_URL, protocol::{Authentication, Color, Frame, ServerMessage, LIGHTHOUSE_RECT, LIGHTHOUSE_SIZE}};
4-
use lighthouse_protocol::{Delta, Model, Pos};
4+
use lighthouse_protocol::{Delta, InputEvent, KeyEvent, Pos};
55
use tracing::{info, debug};
66
use tokio::{task, time};
77
use std::{collections::{VecDeque, HashSet}, sync::Arc, time::Duration};
@@ -142,16 +142,16 @@ async fn run_updater(lh: Lighthouse<TokioWebSocket>, shared_state: Arc<Mutex<Sta
142142
}
143143
}
144144

145-
async fn run_controller(mut stream: impl Stream<Item = Result<ServerMessage<Model>>> + Unpin, shared_state: Arc<Mutex<State>>) -> Result<()> {
145+
async fn run_controller(mut stream: impl Stream<Item = Result<ServerMessage<InputEvent>>> + Unpin, shared_state: Arc<Mutex<State>>) -> Result<()> {
146146
while let Some(msg) = stream.next().await {
147-
if let Model::InputEvent(event) = msg?.payload {
148-
if event.is_down {
147+
match msg?.payload {
148+
InputEvent::Key(KeyEvent { key, down, .. }) if down => {
149149
// Map the key code to a direction vector
150-
let opt_dir = match event.key {
151-
Some(37) => Some(Delta::<i32>::LEFT),
152-
Some(38) => Some(Delta::<i32>::UP),
153-
Some(39) => Some(Delta::<i32>::RIGHT),
154-
Some(40) => Some(Delta::<i32>::DOWN),
150+
let opt_dir = match key.as_str() {
151+
"ArrowLeft" => Some(Delta::<i32>::LEFT),
152+
"ArrowUp" => Some(Delta::<i32>::UP),
153+
"ArrowRight" => Some(Delta::<i32>::RIGHT),
154+
"ArrowDown" => Some(Delta::<i32>::DOWN),
155155
_ => None,
156156
};
157157

@@ -162,6 +162,7 @@ async fn run_controller(mut stream: impl Stream<Item = Result<ServerMessage<Mode
162162
state.snake.rotate_head(dir);
163163
}
164164
}
165+
_ => {},
165166
}
166167
}
167168

@@ -193,7 +194,7 @@ async fn main() -> Result<()> {
193194
let lh = Lighthouse::connect_with_tokio_to(&args.url, auth).await?;
194195
info!("Connected to the Lighthouse server");
195196

196-
let stream = lh.stream_model().await?;
197+
let stream = lh.stream_input().await?;
197198

198199
let updater_handle = task::spawn(run_updater(lh, state.clone()));
199200
let controller_handle = task::spawn(run_controller(stream, state));

0 commit comments

Comments
 (0)