From ca41f72497723ce1102480182ba048f63867461f Mon Sep 17 00:00:00 2001 From: Guillaume Pasquet Date: Wed, 11 Mar 2020 08:52:35 +0000 Subject: Decouple viewport --- src/main.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index d00db53..6348757 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod entities; mod state; mod tiling; +mod viewport; mod world; use std::env; @@ -17,6 +18,7 @@ use simplelog::*; use entities::Player; use state::State; +use viewport::{CrossTermViewPort, ViewPort}; use world::{Dungeon, DOWN, LEFT, RIGHT, UP}; fn player_name() -> String { @@ -45,10 +47,13 @@ fn main() { let _raw = RawScreen::into_raw_mode(); // Initialise state, create the player and dungeon + let xsize = term_size.0 as usize; + let ysize = (term_size.1 - 2) as usize; let mut state = State::new( Player::new(player_name(), String::from("Warrior"), 30, 10, 10, 20), - Dungeon::new(term_size.0 as usize, (term_size.1 - 2) as usize, 5), + Dungeon::new(xsize, ysize, 5), ); + let mut window = CrossTermViewPort::new(xsize, ysize); state.init(); let input = input(); @@ -57,18 +62,13 @@ fn main() { // Main loop, dispatches events and calls rendering routines. Don't // add any game logic here. loop { - state.render_player(); - state.render_level(); - state.render_entities(); - state.render_player(); - - state.render_ui(); + window.render_state(&state); if let Some(event) = reader.next() { match event { InputEvent::Keyboard(KeyEvent::Char('q')) => break, InputEvent::Keyboard(KeyEvent::Char('?')) => { - state.ui_help(); + window.ui_help(); } InputEvent::Keyboard(KeyEvent::Char('j')) => state.move_player(DOWN).ignore(), InputEvent::Keyboard(KeyEvent::Char('k')) => state.move_player(UP).ignore(), @@ -83,11 +83,11 @@ fn main() { // Stairs InputEvent::Keyboard(KeyEvent::Char('>')) => match state.down_stairs() { Ok(()) => (), - Err(info) => state.notify(info), + Err(info) => window.notify(info), }, InputEvent::Keyboard(KeyEvent::Char('<')) => match state.up_stairs() { Ok(()) => (), - Err(info) => state.notify(info), + Err(info) => window.notify(info), }, _ => (), } -- cgit v1.2.3 From 49ca52844b0fa74bcd92018ef313923e9f355632 Mon Sep 17 00:00:00 2001 From: Guillaume Pasquet Date: Fri, 13 Mar 2020 21:40:45 +0000 Subject: stuff --- src/events.rs | 10 ++++++++ src/main.rs | 63 ++++++++++++++----------------------------------- src/viewport.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 93 insertions(+), 53 deletions(-) create mode 100644 src/events.rs (limited to 'src/main.rs') diff --git a/src/events.rs b/src/events.rs new file mode 100644 index 0000000..0619b18 --- /dev/null +++ b/src/events.rs @@ -0,0 +1,10 @@ +use crate::world::Movement; + +#[derive(Copy, Clone, Debug)] +pub enum ViewportEvent { + Quit, + Help, + MovePlayer(Movement), + DownStairs, + UpStairs, +} diff --git a/src/main.rs b/src/main.rs index 6348757..3f7f81c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,25 +1,24 @@ mod entities; +mod events; mod state; mod tiling; mod viewport; mod world; -use std::env; -use std::fs::File; -use std::io::{stdout, Write}; - -use crossterm::cursor; -use crossterm::execute; -use crossterm::input::{input, InputEvent, KeyEvent}; -use crossterm::screen::{EnterAlternateScreen, LeaveAlternateScreen, RawScreen}; -use crossterm::terminal; use ignore_result::Ignore; use simplelog::*; +use std::env; +use std::fs::File; use entities::Player; +use events::ViewportEvent; use state::State; use viewport::{CrossTermViewPort, ViewPort}; -use world::{Dungeon, DOWN, LEFT, RIGHT, UP}; +use world::Dungeon; + +const DUNGEON_SIZE_X: usize = 20; +const DUNGEON_SIZE_Y: usize = 20; +const DUNGEON_DEPTH: usize = 5; fn player_name() -> String { match env::var_os("USER") { @@ -39,62 +38,34 @@ fn main() { .unwrap(); } - // Initialise the terminal, the raw alternate mode allows direct character - // seeking and hides the prompt. - let term_size = terminal::size().unwrap(); - execute!(stdout(), EnterAlternateScreen).unwrap(); - execute!(stdout(), cursor::Hide).unwrap(); - let _raw = RawScreen::into_raw_mode(); - - // Initialise state, create the player and dungeon - let xsize = term_size.0 as usize; - let ysize = (term_size.1 - 2) as usize; let mut state = State::new( Player::new(player_name(), String::from("Warrior"), 30, 10, 10, 20), - Dungeon::new(xsize, ysize, 5), + Dungeon::new(DUNGEON_SIZE_X, DUNGEON_SIZE_Y, DUNGEON_DEPTH), ); - let mut window = CrossTermViewPort::new(xsize, ysize); + let mut window = CrossTermViewPort::new(); state.init(); - let input = input(); - let mut reader = input.read_sync(); - // Main loop, dispatches events and calls rendering routines. Don't // add any game logic here. loop { window.render_state(&state); - if let Some(event) = reader.next() { + if let Some(event) = window.wait_input() { match event { - InputEvent::Keyboard(KeyEvent::Char('q')) => break, - InputEvent::Keyboard(KeyEvent::Char('?')) => { - window.ui_help(); - } - InputEvent::Keyboard(KeyEvent::Char('j')) => state.move_player(DOWN).ignore(), - InputEvent::Keyboard(KeyEvent::Char('k')) => state.move_player(UP).ignore(), - InputEvent::Keyboard(KeyEvent::Char('h')) => state.move_player(LEFT).ignore(), - InputEvent::Keyboard(KeyEvent::Char('l')) => state.move_player(RIGHT).ignore(), - // Arrow keys for noobs - InputEvent::Keyboard(KeyEvent::Down) => state.move_player(DOWN).ignore(), - InputEvent::Keyboard(KeyEvent::Up) => state.move_player(UP).ignore(), - InputEvent::Keyboard(KeyEvent::Left) => state.move_player(LEFT).ignore(), - InputEvent::Keyboard(KeyEvent::Right) => state.move_player(RIGHT).ignore(), - - // Stairs - InputEvent::Keyboard(KeyEvent::Char('>')) => match state.down_stairs() { + ViewportEvent::Quit => break, + ViewportEvent::MovePlayer(direction) => state.move_player(direction).ignore(), + ViewportEvent::DownStairs => match state.down_stairs() { Ok(()) => (), Err(info) => window.notify(info), }, - InputEvent::Keyboard(KeyEvent::Char('<')) => match state.up_stairs() { + ViewportEvent::UpStairs => match state.up_stairs() { Ok(()) => (), Err(info) => window.notify(info), }, _ => (), } } + // actors actions (normally attack / interact if on same location as the character) } - - execute!(stdout(), LeaveAlternateScreen).unwrap(); - execute!(stdout(), cursor::Show).unwrap(); } diff --git a/src/viewport.rs b/src/viewport.rs index 37dfd21..1fd13aa 100644 --- a/src/viewport.rs +++ b/src/viewport.rs @@ -1,12 +1,17 @@ +use crate::events::ViewportEvent; +use crate::world::{DOWN, LEFT, RIGHT, UP}; +use crossterm::cursor; use crossterm::cursor::MoveTo; +use crossterm::input::{input, InputEvent, KeyEvent, SyncReader}; +use crossterm::screen::{EnterAlternateScreen, LeaveAlternateScreen, RawScreen}; +use crossterm::terminal; use crossterm::{execute, queue, Output}; use log::debug; use std::io::{stdout, Write}; -use crate::entities::{Character, Entity, Player}; +use crate::entities::{Entity, Player}; use crate::state::State; -use crate::tiling::{tile_to_str, Tile, TileGrid, TileType}; -use crate::world::{apply_movement, Dungeon, Generatable, Level, Movement}; +use crate::tiling::tile_to_str; pub trait ViewPort { fn render_state(&mut self, state: &State); @@ -15,13 +20,31 @@ pub trait ViewPort { pub struct CrossTermViewPort { xsize: usize, ysize: usize, - // Use below when switching to moveable window - //start: (usize, usize) + reader: SyncReader, + start: (usize, usize), } impl CrossTermViewPort { - pub fn new(xsize: usize, ysize: usize) -> CrossTermViewPort { - CrossTermViewPort { xsize, ysize } + pub fn new() -> CrossTermViewPort { + // Initialise the terminal, the raw alternate mode allows direct character + // seeking and hides the prompt. + let term_size = terminal::size().unwrap(); + execute!(stdout(), EnterAlternateScreen).unwrap(); + execute!(stdout(), cursor::Hide).unwrap(); + let _raw = RawScreen::into_raw_mode(); + + // Initialise state, create the player and dungeon + let xsize = term_size.0 as usize; + let ysize = (term_size.1 - 2) as usize; + + let input = input(); + + CrossTermViewPort { + xsize, + ysize, + reader: input.read_sync(), + start: (0, 0), + } } fn draw_level(&self, state: &State) { @@ -106,6 +129,35 @@ impl CrossTermViewPort { "quit: q, movement{up(k), down(j), left(h), right(l)}", )) } + + pub fn wait_input(&mut self) -> Option { + if let Some(event) = self.reader.next() { + return match event { + InputEvent::Keyboard(KeyEvent::Char('q')) => Some(ViewportEvent::Quit), + InputEvent::Keyboard(KeyEvent::Char('?')) => { + self.ui_help(); + None + } + InputEvent::Keyboard(KeyEvent::Char('j')) => Some(ViewportEvent::MovePlayer(DOWN)), + InputEvent::Keyboard(KeyEvent::Char('k')) => Some(ViewportEvent::MovePlayer(UP)), + InputEvent::Keyboard(KeyEvent::Char('h')) => Some(ViewportEvent::MovePlayer(LEFT)), + InputEvent::Keyboard(KeyEvent::Char('l')) => Some(ViewportEvent::MovePlayer(RIGHT)), + // Arrow keys for noobs + InputEvent::Keyboard(KeyEvent::Down) => Some(ViewportEvent::MovePlayer(DOWN)), + InputEvent::Keyboard(KeyEvent::Up) => Some(ViewportEvent::MovePlayer(UP)), + InputEvent::Keyboard(KeyEvent::Left) => Some(ViewportEvent::MovePlayer(LEFT)), + InputEvent::Keyboard(KeyEvent::Right) => Some(ViewportEvent::MovePlayer(RIGHT)), + + // Stairs + InputEvent::Keyboard(KeyEvent::Char('>')) => Some(ViewportEvent::DownStairs), + InputEvent::Keyboard(KeyEvent::Char('<')) => Some(ViewportEvent::UpStairs), + + // No match + _ => None, + }; + } + None + } } impl ViewPort for CrossTermViewPort { @@ -116,3 +168,10 @@ impl ViewPort for CrossTermViewPort { self.draw_ui(state); } } + +impl Drop for CrossTermViewPort { + fn drop(&mut self) { + execute!(stdout(), LeaveAlternateScreen).unwrap(); + execute!(stdout(), cursor::Show).unwrap(); + } +} -- cgit v1.2.3 From 9848a92fde2891177cc352064ae9e4ec1098277f Mon Sep 17 00:00:00 2001 From: Guillaume Pasquet Date: Sat, 14 Mar 2020 16:22:54 +0000 Subject: Make it work --- .vscode/launch.json | 1 + src/main.rs | 4 ++-- src/viewport.rs | 35 ++++++++++++++++++++--------------- 3 files changed, 23 insertions(+), 17 deletions(-) (limited to 'src/main.rs') diff --git a/.vscode/launch.json b/.vscode/launch.json index e2e657d..981f449 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -22,6 +22,7 @@ "program": "${workspaceRoot}/target/debug/roguelike", "args": [], "cwd": "${workspaceRoot}", + "terminal": "external" } ] } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3f7f81c..bed0c8d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,8 @@ use state::State; use viewport::{CrossTermViewPort, ViewPort}; use world::Dungeon; -const DUNGEON_SIZE_X: usize = 20; -const DUNGEON_SIZE_Y: usize = 20; +const DUNGEON_SIZE_X: usize = 80; +const DUNGEON_SIZE_Y: usize = 24; const DUNGEON_DEPTH: usize = 5; fn player_name() -> String { diff --git a/src/viewport.rs b/src/viewport.rs index 1fd13aa..7b4fb3e 100644 --- a/src/viewport.rs +++ b/src/viewport.rs @@ -2,7 +2,7 @@ use crate::events::ViewportEvent; use crate::world::{DOWN, LEFT, RIGHT, UP}; use crossterm::cursor; use crossterm::cursor::MoveTo; -use crossterm::input::{input, InputEvent, KeyEvent, SyncReader}; +use crossterm::input::{input, InputEvent, KeyEvent, TerminalInput}; use crossterm::screen::{EnterAlternateScreen, LeaveAlternateScreen, RawScreen}; use crossterm::terminal; use crossterm::{execute, queue, Output}; @@ -15,12 +15,14 @@ use crate::tiling::tile_to_str; pub trait ViewPort { fn render_state(&mut self, state: &State); + fn wait_input(&mut self) -> Option; } pub struct CrossTermViewPort { xsize: usize, ysize: usize, - reader: SyncReader, + raw: RawScreen, + input: TerminalInput, start: (usize, usize), } @@ -31,7 +33,7 @@ impl CrossTermViewPort { let term_size = terminal::size().unwrap(); execute!(stdout(), EnterAlternateScreen).unwrap(); execute!(stdout(), cursor::Hide).unwrap(); - let _raw = RawScreen::into_raw_mode(); + let raw = RawScreen::into_raw_mode().unwrap(); // Initialise state, create the player and dungeon let xsize = term_size.0 as usize; @@ -42,7 +44,8 @@ impl CrossTermViewPort { CrossTermViewPort { xsize, ysize, - reader: input.read_sync(), + raw, + input, start: (0, 0), } } @@ -129,9 +132,20 @@ impl CrossTermViewPort { "quit: q, movement{up(k), down(j), left(h), right(l)}", )) } +} + +impl ViewPort for CrossTermViewPort { + fn render_state(&mut self, state: &State) { + self.draw_level(state); + self.draw_entities(state); + self.draw_player(state); + self.draw_ui(state); + } + + fn wait_input(&mut self) -> Option { + let mut reader = self.input.read_sync(); - pub fn wait_input(&mut self) -> Option { - if let Some(event) = self.reader.next() { + if let Some(event) = reader.next() { return match event { InputEvent::Keyboard(KeyEvent::Char('q')) => Some(ViewportEvent::Quit), InputEvent::Keyboard(KeyEvent::Char('?')) => { @@ -160,15 +174,6 @@ impl CrossTermViewPort { } } -impl ViewPort for CrossTermViewPort { - fn render_state(&mut self, state: &State) { - self.draw_level(state); - self.draw_entities(state); - self.draw_player(state); - self.draw_ui(state); - } -} - impl Drop for CrossTermViewPort { fn drop(&mut self) { execute!(stdout(), LeaveAlternateScreen).unwrap(); -- cgit v1.2.3