Add info box
This commit is contained in:
parent
52b862df01
commit
31ab81b6d1
69
src/game.rs
69
src/game.rs
@ -4,8 +4,9 @@ use crossterm::event::{Event, KeyCode};
|
|||||||
use ratatui::{
|
use ratatui::{
|
||||||
Frame,
|
Frame,
|
||||||
buffer::{Buffer, Cell},
|
buffer::{Buffer, Cell},
|
||||||
layout::Rect,
|
layout::{Constraint, Layout, Rect},
|
||||||
style::Color,
|
style::Color,
|
||||||
|
text::{Line, Text},
|
||||||
widgets::{Block, Widget},
|
widgets::{Block, Widget},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -81,14 +82,14 @@ impl Tile {
|
|||||||
..self
|
..self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn may_enter(&self) -> bool {
|
fn may_enter(&self) -> bool {
|
||||||
match self.style {
|
match self.style {
|
||||||
TileStyle::Wall => false,
|
TileStyle::Wall => false,
|
||||||
_ => true,
|
_ => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dashtination(&self) -> bool {
|
fn dashtination(&self) -> bool {
|
||||||
self.action.is_some()
|
self.action.is_some()
|
||||||
}
|
}
|
||||||
@ -129,7 +130,8 @@ impl GameModel {
|
|||||||
tiles.get_mut(P2::new(2, 2)).map(|tile| {
|
tiles.get_mut(P2::new(2, 2)).map(|tile| {
|
||||||
*tile = Tile::new(TileStyle::Portal(Portal {
|
*tile = Tile::new(TileStyle::Portal(Portal {
|
||||||
kind: PortalKind::Ladder,
|
kind: PortalKind::Ladder,
|
||||||
})).with_action(GameEvent::Navigate(NavigationTarget::Parent))
|
}))
|
||||||
|
.with_action(GameEvent::Navigate(NavigationTarget::Parent))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,15 +140,24 @@ impl GameModel {
|
|||||||
tiles.get_mut(P2::new(10, 2 + i as isize)).map(|tile| {
|
tiles.get_mut(P2::new(10, 2 + i as isize)).map(|tile| {
|
||||||
*tile = Tile::new(TileStyle::Portal(Portal {
|
*tile = Tile::new(TileStyle::Portal(Portal {
|
||||||
kind: PortalKind::Portal,
|
kind: PortalKind::Portal,
|
||||||
})).with_action(GameEvent::Navigate(NavigationTarget::Path(entry.clone())))
|
}))
|
||||||
|
.with_action(
|
||||||
|
GameEvent::Navigate(NavigationTarget::Path(entry.clone())),
|
||||||
|
)
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
tiles.get_mut(P2::new(10, 2 + i as isize)).map(|tile| {
|
tiles
|
||||||
*tile = Tile::new(TileStyle::Box)
|
.get_mut(P2::new(10, 2 + i as isize))
|
||||||
});
|
.map(|tile| *tile = Tile::new(TileStyle::Box));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (j, c) in entry.file_name().unwrap().to_string_lossy().chars().enumerate() {
|
for (j, c) in entry
|
||||||
|
.file_name()
|
||||||
|
.unwrap()
|
||||||
|
.to_string_lossy()
|
||||||
|
.chars()
|
||||||
|
.enumerate()
|
||||||
|
{
|
||||||
tiles
|
tiles
|
||||||
.get_mut(P2::new(11 + j as isize, 2 + i as isize))
|
.get_mut(P2::new(11 + j as isize, 2 + i as isize))
|
||||||
.map(|tile| *tile = Tile::new(TileStyle::Char(c)));
|
.map(|tile| *tile = Tile::new(TileStyle::Char(c)));
|
||||||
@ -166,7 +177,6 @@ impl GameModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn update_game(self, event: GameEvent) -> Result<Self, PathBuf> {
|
fn update_game(self, event: GameEvent) -> Result<Self, PathBuf> {
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
GameEvent::Quit => Err(self.path),
|
GameEvent::Quit => Err(self.path),
|
||||||
GameEvent::Nop => Ok(self),
|
GameEvent::Nop => Ok(self),
|
||||||
@ -199,11 +209,15 @@ impl GameModel {
|
|||||||
Ok(Self { player_pos, ..self })
|
Ok(Self { player_pos, ..self })
|
||||||
}
|
}
|
||||||
GameEvent::Interact => {
|
GameEvent::Interact => {
|
||||||
let opt_action = self.room.tiles.get(self.player_pos).and_then(|tile| tile.action.clone());
|
let opt_action = self
|
||||||
|
.room
|
||||||
|
.tiles
|
||||||
|
.get(self.player_pos)
|
||||||
|
.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)
|
Ok(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -218,18 +232,37 @@ impl GameModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, frame: &mut Frame) {
|
pub fn render(&self, frame: &mut Frame) {
|
||||||
let camera_block = Block::bordered().title("camera");
|
let layout =
|
||||||
let camera_area = camera_block.inner(frame.area());
|
Layout::vertical([Constraint::Fill(1), Constraint::Length(3)]).split(frame.area());
|
||||||
|
|
||||||
|
let camera_block_area = layout[0];
|
||||||
|
let info_block_area = layout[1];
|
||||||
|
|
||||||
|
let camera_block = Block::bordered()
|
||||||
|
.title("camera")
|
||||||
|
.title_bottom(Line::from("move: h, j, k, l enter: e up: r quit: q").right_aligned());
|
||||||
|
let camera_area = camera_block.inner(camera_block_area);
|
||||||
let camera = CameraWidget::new(self);
|
let camera = CameraWidget::new(self);
|
||||||
|
|
||||||
frame.render_widget(camera_block, frame.area());
|
let info_block = Block::bordered().title("cwd");
|
||||||
|
let info_area = info_block.inner(info_block_area);
|
||||||
|
let info = Text::raw(self.path.to_string_lossy());
|
||||||
|
|
||||||
|
frame.render_widget(camera_block, camera_block_area);
|
||||||
frame.render_widget(camera, camera_area);
|
frame.render_widget(camera, camera_area);
|
||||||
|
|
||||||
|
frame.render_widget(info_block, info_block_area);
|
||||||
|
frame.render_widget(info, info_area);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_tiles(&self) -> Grid<TileStyle> {
|
fn render_tiles(&self) -> Grid<TileStyle> {
|
||||||
let mut tiles = Grid::from_fn(self.room.tiles.dims.x,
|
let mut tiles = Grid::from_fn(self.room.tiles.dims.x, self.room.tiles.dims.y, |x, y| {
|
||||||
self.room.tiles.dims.y, |x, y| {
|
self.room
|
||||||
self.room.tiles.get(P2::new(x as isize, y as isize)).unwrap().style.clone()
|
.tiles
|
||||||
|
.get(P2::new(x as isize, y as isize))
|
||||||
|
.unwrap()
|
||||||
|
.style
|
||||||
|
.clone()
|
||||||
});
|
});
|
||||||
|
|
||||||
tiles
|
tiles
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user