Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ anyhow = "1.0.81"
clap = { version = "4.5.3", features = ["derive", "env"] }
dotenvy = "0.15.7"
futures = "0.3.30"
lighthouse-client = "5.0.1"
lighthouse-client = "5.0.2"
rand = "0.9.0"
tokio = { version = "1.36.0", features = ["rt", "macros", "time"] }
tracing = "0.1.40"
Expand Down
29 changes: 4 additions & 25 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,19 @@ use std::sync::Arc;

use anyhow::Result;
use futures::{lock::Mutex, prelude::*, Stream};
use lighthouse_client::protocol::{Delta, GamepadButtonEvent, GamepadControlEvent, GamepadEvent, InputEvent, KeyEvent, ServerMessage};
use lighthouse_client::protocol::{InputEvent, ServerMessage};
use tracing::debug;

use crate::model::State;

pub async fn run(mut stream: impl Stream<Item = lighthouse_client::Result<ServerMessage<InputEvent>>> + Unpin, shared_state: Arc<Mutex<State>>) -> Result<()> {
while let Some(msg) = stream.next().await {
let opt_dir = match msg?.payload {
InputEvent::Key(KeyEvent { code, down, .. }) if down => match code.as_str() {
"ArrowLeft" => Some(Delta::LEFT),
"ArrowUp" => Some(Delta::UP),
"ArrowRight" => Some(Delta::RIGHT),
"ArrowDown" => Some(Delta::DOWN),
_ => None,
}
InputEvent::Gamepad(GamepadEvent { control, .. }) => match control {
GamepadControlEvent::Button(GamepadButtonEvent { index, down, .. }) if down => match index {
// Per https://w3c.github.io/gamepad/#remapping
12 => Some(Delta::UP),
13 => Some(Delta::DOWN),
14 => Some(Delta::LEFT),
15 => Some(Delta::RIGHT),
_ => None,
},
_ => None,
}
_ => None,
};

let input_event = msg?.payload;
// Update the snake's direction
if let Some(dir) = opt_dir {
if let Some(dir) = input_event.direction() {
debug!("Rotating snake head to {:?}", dir);
let mut state = shared_state.lock().await;
state.snake.rotate_head(dir);
state.snake.rotate_head(dir.into());
}
}

Expand Down