refactored controller module to allow expansion
This commit is contained in:
parent
772b85b0b7
commit
2b496865f7
3 changed files with 35 additions and 36 deletions
|
@ -1,11 +1,5 @@
|
||||||
use termion::{event::Key, input::TermRead, raw::IntoRawMode};
|
|
||||||
|
|
||||||
use super::grid::Grid;
|
use super::grid::Grid;
|
||||||
use std::{
|
use std::{error::Error, fmt::Display};
|
||||||
error::Error,
|
|
||||||
fmt::Display,
|
|
||||||
io::{stdin, stdout},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub enum Move {
|
pub enum Move {
|
||||||
LEFT,
|
LEFT,
|
||||||
|
@ -34,31 +28,4 @@ pub trait Controller {
|
||||||
fn next_move(&mut self, grid: &Grid) -> Result<Move, ControllerError>;
|
fn next_move(&mut self, grid: &Grid) -> Result<Move, ControllerError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PlayerController {
|
pub mod player;
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PlayerController {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Controller for PlayerController {
|
|
||||||
fn next_move(&mut self, _grid: &Grid) -> Result<Move, ControllerError> {
|
|
||||||
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!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
32
src/lib/controller/player.rs
Normal file
32
src/lib/controller/player.rs
Normal file
|
@ -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<Move, ControllerError> {
|
||||||
|
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!()
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{error::Error, fmt::Display};
|
use std::{error::Error, fmt::Display};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
controller::{Controller, ControllerError, Move, PlayerController},
|
controller::{player::PlayerController, Controller, ControllerError, Move},
|
||||||
grid::Grid,
|
grid::Grid,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue