Replace all unwraps
This commit is contained in:
parent
167ab4f5b8
commit
282d41ceb4
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
target
|
target
|
||||||
|
*.swp
|
||||||
|
|||||||
2
activate
Normal file
2
activate
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export PATH=$PATH:/Users/paul/Source/cdungeon/target/debug
|
||||||
|
alias cdg='cd $(cdungeon)'
|
||||||
67
src/game.rs
67
src/game.rs
@ -1,5 +1,6 @@
|
|||||||
use std::{fs, path::PathBuf};
|
use std::{error::Error, fmt::{self}, fs, path::PathBuf};
|
||||||
|
|
||||||
|
use color_eyre::eyre::Result;
|
||||||
use crossterm::event::{Event, KeyCode};
|
use crossterm::event::{Event, KeyCode};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
Frame,
|
Frame,
|
||||||
@ -95,21 +96,36 @@ impl Tile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum UpdateResult {
|
||||||
|
ShouldExit(PathBuf),
|
||||||
|
NewModel(GameModel),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct GameError {}
|
||||||
|
|
||||||
|
impl fmt::Display for GameError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "ouch game broken")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for GameError {}
|
||||||
|
|
||||||
impl GameModel {
|
impl GameModel {
|
||||||
pub fn new(path: PathBuf) -> Self {
|
pub fn new(path: PathBuf) -> Result<Self> {
|
||||||
let path = path.canonicalize().unwrap();
|
let path = path.canonicalize()?;
|
||||||
let mut entries: Vec<PathBuf> = fs::read_dir(&path)
|
let mut entries: Vec<PathBuf> = fs::read_dir(&path)?
|
||||||
.unwrap()
|
.filter_map(|entry| entry.ok().map(|entry| entry.path()))
|
||||||
.map(|entry| entry.unwrap().path())
|
|
||||||
.collect();
|
.collect();
|
||||||
entries.sort();
|
entries.sort();
|
||||||
let max_entry_length = entries
|
let max_entry_length = entries
|
||||||
.iter()
|
.iter()
|
||||||
.map(|entry| entry.file_name().unwrap().to_string_lossy().chars().count())
|
.filter_map(|entry| entry.file_name().map(|entry| entry.to_string_lossy().chars().count()))
|
||||||
.max()
|
.max()
|
||||||
.unwrap_or(0);
|
.unwrap_or(0);
|
||||||
|
|
||||||
Self {
|
Ok(Self {
|
||||||
player_pos: P2::new(10, 2),
|
player_pos: P2::new(10, 2),
|
||||||
room: Room {
|
room: Room {
|
||||||
tiles: {
|
tiles: {
|
||||||
@ -152,8 +168,7 @@ impl GameModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (j, c) in entry
|
for (j, c) in entry
|
||||||
.file_name()
|
.file_name().ok_or(GameError {})?
|
||||||
.unwrap()
|
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.chars()
|
.chars()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
@ -169,25 +184,26 @@ impl GameModel {
|
|||||||
},
|
},
|
||||||
|
|
||||||
path,
|
path,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(self, event: Event) -> Result<Self, PathBuf> {
|
pub fn update(self, event: Event) -> Result<UpdateResult> {
|
||||||
self.update_game(GameEvent::from_crossterm(event))
|
self.update_game(GameEvent::from_crossterm(event))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_game(self, event: GameEvent) -> Result<Self, PathBuf> {
|
fn update_game(self, event: GameEvent) -> Result<UpdateResult> {
|
||||||
match event {
|
use UpdateResult::*;
|
||||||
GameEvent::Quit => Err(self.path),
|
Ok(match event {
|
||||||
GameEvent::Nop => Ok(self),
|
GameEvent::Quit => ShouldExit(self.path),
|
||||||
|
GameEvent::Nop => NewModel(self),
|
||||||
GameEvent::PlayerMove(direction) => {
|
GameEvent::PlayerMove(direction) => {
|
||||||
let player_pos = self.player_pos + direction;
|
let player_pos = self.player_pos + direction;
|
||||||
if let Some(target_tile) = self.room.tiles.get(player_pos)
|
if let Some(target_tile) = self.room.tiles.get(player_pos)
|
||||||
&& target_tile.may_enter()
|
&& target_tile.may_enter()
|
||||||
{
|
{
|
||||||
Ok(Self { player_pos, ..self })
|
NewModel(Self { player_pos, ..self })
|
||||||
} else {
|
} else {
|
||||||
Ok(self)
|
NewModel(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
GameEvent::PlayerDash(direction) => {
|
GameEvent::PlayerDash(direction) => {
|
||||||
@ -206,7 +222,7 @@ impl GameModel {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Self { player_pos, ..self })
|
NewModel(Self { player_pos, ..self })
|
||||||
}
|
}
|
||||||
GameEvent::Interact => {
|
GameEvent::Interact => {
|
||||||
let opt_action = self
|
let opt_action = self
|
||||||
@ -216,19 +232,20 @@ impl GameModel {
|
|||||||
.and_then(|tile| tile.action.clone());
|
.and_then(|tile| tile.action.clone());
|
||||||
|
|
||||||
if let Some(action) = opt_action {
|
if let Some(action) = opt_action {
|
||||||
self.update_game(action)
|
self.update_game(action)?
|
||||||
} else {
|
} else {
|
||||||
Ok(self)
|
NewModel(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
GameEvent::Navigate(target) => {
|
GameEvent::Navigate(target) => {
|
||||||
let path = match target {
|
let path = match target {
|
||||||
NavigationTarget::Path(path) => path,
|
NavigationTarget::Path(path) => path,
|
||||||
NavigationTarget::Parent => self.path.parent().unwrap().to_path_buf(),
|
NavigationTarget::Parent => self.path.parent().ok_or(GameError {})?.to_path_buf(),
|
||||||
};
|
};
|
||||||
Ok(Self::new(path))
|
|
||||||
|
NewModel(Self::new(path)?)
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, frame: &mut Frame) {
|
pub fn render(&self, frame: &mut Frame) {
|
||||||
@ -260,7 +277,7 @@ impl GameModel {
|
|||||||
self.room
|
self.room
|
||||||
.tiles
|
.tiles
|
||||||
.get(P2::new(x as isize, y as isize))
|
.get(P2::new(x as isize, y as isize))
|
||||||
.unwrap()
|
.expect("programmer error: copying a grid shouldnt yield OOB errors")
|
||||||
.style
|
.style
|
||||||
.clone()
|
.clone()
|
||||||
});
|
});
|
||||||
|
|||||||
@ -11,6 +11,7 @@ use ratatui::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use game::GameModel;
|
use game::GameModel;
|
||||||
|
use game::UpdateResult;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
enable_raw_mode()?;
|
enable_raw_mode()?;
|
||||||
@ -33,14 +34,14 @@ fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main_loop<T: Backend>(terminal: &mut Terminal<T>) -> Result<PathBuf> {
|
fn main_loop<T: Backend>(terminal: &mut Terminal<T>) -> Result<PathBuf> {
|
||||||
let mut model = GameModel::new(PathBuf::from("."));
|
let mut model = GameModel::new(PathBuf::from("."))?;
|
||||||
loop {
|
loop {
|
||||||
terminal.draw(|frame| model.render(frame))?;
|
terminal.draw(|frame| model.render(frame))?;
|
||||||
|
|
||||||
let term_event = event::read()?;
|
let term_event = event::read()?;
|
||||||
match model.update(term_event) {
|
match model.update(term_event)? {
|
||||||
Ok(new_model) => model = new_model,
|
UpdateResult::NewModel(new_model) => model = new_model,
|
||||||
Err(path) => break Ok(path),
|
UpdateResult::ShouldExit(path) => break Ok(path),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user