Can move boats with arrows

This commit is contained in:
Pierre HUBERT 2022-10-08 09:55:07 +02:00
parent 04fac2ddb2
commit c3f2b5e767

View File

@ -44,20 +44,50 @@ pub fn set_boat_layout<B: Backend>(
.unwrap_or_else(|| Duration::from_secs(0)); .unwrap_or_else(|| Duration::from_secs(0));
if event::poll(timeout)? { if event::poll(timeout)? {
let mut move_boat = None;
let event = event::read()?; let event = event::read()?;
if let Event::Key(key) = &event { if let Event::Key(key) = &event {
match key.code { match key.code {
KeyCode::Char('q') => return Ok(ScreenResult::Canceled), KeyCode::Char('q') => return Ok(ScreenResult::Canceled),
// Select next boat
KeyCode::Char('n') => model.curr_boat += model.layout.number_of_boats() - 1,
// Rotate boat
KeyCode::Char('r') => {
model.layout.0[model.curr_boat].direction =
match model.layout.0[model.curr_boat].direction {
BoatDirection::Right => BoatDirection::Down,
_ => BoatDirection::Right,
}
}
// Move boat
KeyCode::Left => move_boat = Some((-1, 0)),
KeyCode::Right => move_boat = Some((1, 0)),
KeyCode::Up => move_boat = Some((0, -1)),
KeyCode::Down => move_boat = Some((0, 1)),
// Submit configuration
KeyCode::Enter => { KeyCode::Enter => {
if model.layout.is_valid(rules) { if model.layout.is_valid(rules) {
return Ok(ScreenResult::Ok(model.layout)); return Ok(ScreenResult::Ok(model.layout));
} }
} }
KeyCode::Char('n') => model.curr_boat += model.layout.number_of_boats() - 1,
_ => {} _ => {}
} }
model.curr_boat %= model.layout.number_of_boats(); model.curr_boat %= model.layout.number_of_boats();
// Apply boat move
if let Some((x, y)) = move_boat {
let new_pos = model.layout.0[model.curr_boat].start.add_x(x).add_y(y);
if new_pos.is_valid(rules) {
model.layout.0[model.curr_boat].start = new_pos;
}
}
} }
// Mouse event // Mouse event
else if let Event::Mouse(mouse) = event { else if let Event::Mouse(mouse) = event {
@ -87,6 +117,7 @@ pub fn set_boat_layout<B: Backend>(
} }
} }
} }
if last_tick.elapsed() >= TICK_RATE { if last_tick.elapsed() >= TICK_RATE {
last_tick = Instant::now(); last_tick = Instant::now();
} }