From 2b496865f7ac5d88bcfe1c646c99a2bf107ff933 Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Mon, 4 Apr 2022 16:22:59 +0300 Subject: [PATCH] refactored controller module to allow expansion --- src/lib/controller.rs | 37 ++---------------------------------- src/lib/controller/player.rs | 32 +++++++++++++++++++++++++++++++ src/lib/game.rs | 2 +- 3 files changed, 35 insertions(+), 36 deletions(-) create mode 100644 src/lib/controller/player.rs diff --git a/src/lib/controller.rs b/src/lib/controller.rs index 64ab607..ecd4cb1 100644 --- a/src/lib/controller.rs +++ b/src/lib/controller.rs @@ -1,11 +1,5 @@ -use termion::{event::Key, input::TermRead, raw::IntoRawMode}; - use super::grid::Grid; -use std::{ - error::Error, - fmt::Display, - io::{stdin, stdout}, -}; +use std::{error::Error, fmt::Display}; pub enum Move { LEFT, @@ -34,31 +28,4 @@ pub trait Controller { fn next_move(&mut self, grid: &Grid) -> Result; } -pub struct PlayerController { - // -} - -impl PlayerController { - pub fn new() -> Self { - Self {} - } -} - -impl Controller for PlayerController { - fn next_move(&mut self, _grid: &Grid) -> Result { - let stdin = stdin(); - let mut _stdout = stdout().into_raw_mode().unwrap(); - for c in stdin.keys() { - let movement = match c.unwrap() { - Key::Char('q') => return Err(ControllerError::ExitSignal), - Key::Left => Move::LEFT, - Key::Right => Move::RIGHT, - Key::Up => Move::UP, - Key::Down => Move::DOWN, - _ => continue, - }; - return Ok(movement); - } - unreachable!() - } -} +pub mod player; diff --git a/src/lib/controller/player.rs b/src/lib/controller/player.rs new file mode 100644 index 0000000..7f3ad2a --- /dev/null +++ b/src/lib/controller/player.rs @@ -0,0 +1,32 @@ +use std::io::{stdin, stdout}; +use termion::{event::Key, input::TermRead, raw::IntoRawMode}; + +use super::{Controller, ControllerError, Move}; +use crate::lib::grid::Grid; + +pub struct PlayerController; + +impl PlayerController { + pub fn new() -> Self { + Self + } +} + +impl Controller for PlayerController { + fn next_move(&mut self, _grid: &Grid) -> Result { + let stdin = stdin(); + let mut _stdout = stdout().into_raw_mode().unwrap(); + for c in stdin.keys() { + let movement = match c.unwrap() { + Key::Char('q') => return Err(ControllerError::ExitSignal), + Key::Left => Move::LEFT, + Key::Right => Move::RIGHT, + Key::Up => Move::UP, + Key::Down => Move::DOWN, + _ => continue, + }; + return Ok(movement); + } + unreachable!() + } +} diff --git a/src/lib/game.rs b/src/lib/game.rs index 23ffde1..1cdcbe7 100644 --- a/src/lib/game.rs +++ b/src/lib/game.rs @@ -1,7 +1,7 @@ use std::{error::Error, fmt::Display}; use super::{ - controller::{Controller, ControllerError, Move, PlayerController}, + controller::{player::PlayerController, Controller, ControllerError, Move}, grid::Grid, };