diff --git a/src/game.rs b/src/game.rs index 20e0fb9..7d04bde 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,6 +1,10 @@ use crossterm::event::{Event, KeyCode}; use ratatui::{ - Frame, buffer::{Buffer, Cell}, layout::Rect, style::Color, widgets::{Block, Widget} + Frame, + buffer::{Buffer, Cell}, + layout::Rect, + style::Color, + widgets::{Block, Widget}, }; use crate::geometry::{Grid, P2, V2}; @@ -41,6 +45,12 @@ impl GameEvent { struct Room { size: V2, } +impl Room { + fn in_bounds(&self, player_pos: P2) -> bool { + (0..self.size.x as isize).contains(&player_pos.x) + && (0..self.size.y as isize).contains(&player_pos.y) + } +} impl GameModel { pub fn new() -> Self { @@ -56,10 +66,14 @@ impl GameModel { match GameEvent::from_crossterm(event) { GameEvent::Quit => None, GameEvent::Nop => Some(self), - GameEvent::MovePlayer(direction) => Some(Self { - player_pos: self.player_pos + direction, - ..self - }), + GameEvent::MovePlayer(direction) => { + let player_pos = self.player_pos + direction; + if self.room.in_bounds(player_pos) { + Some(Self { player_pos, ..self }) + } else { + Some(self) + } + } } } @@ -83,15 +97,22 @@ impl GameModel { .get_mut(P2::new(5, 2)) .map(|tile| *tile = Tile::Amphora); - tiles - .get_mut(P2::new(8, 5)) - .map(|tile| *tile = Tile::Frog); + tiles.get_mut(P2::new(8, 5)).map(|tile| *tile = Tile::Frog); + + tiles.get_mut(P2::new(7, 6)).map(|tile| *tile = Tile::Water); + + tiles.get_mut(P2::new(8, 6)).map(|tile| *tile = Tile::Water); + + tiles.get_mut(P2::new(9, 6)).map(|tile| *tile = Tile::Water); + + tiles.get_mut(P2::new(8, 7)).map(|tile| *tile = Tile::Water); + + tiles.get_mut(P2::new(9, 7)).map(|tile| *tile = Tile::Water); tiles .get_mut(self.player_pos) .map(|tile| *tile = Tile::Player); - tiles } } @@ -102,6 +123,7 @@ enum Tile { Player, Amphora, Frog, + Water, } impl Tile { @@ -123,6 +145,11 @@ impl Tile { // Alternative symbol: Ö̶͈ cell.set_symbol("ä̃").set_fg(Color::Green); } + Tile::Water => { + cell.set_symbol("≈") + .set_bg(Color::Blue) + .set_fg(Color::White); + } } } } @@ -142,7 +169,10 @@ impl<'a> Widget for CameraWidget<'a> { for y in 0..area.height { for x in 0..area.width { let cell = &mut buf[(area.left() + x, area.top() + y)]; - if let Some(tile) = tiles.get(P2::new(x as isize, y as isize)) { + let tile_index = P2::new(x as isize, y as isize) + - V2::new(area.width as isize, area.height as isize) / 2 + + (self.model.player_pos - P2::new(0, 0)); + if let Some(tile) = tiles.get(tile_index) { tile.render(cell); } else { cell.set_symbol("."); diff --git a/src/geometry.rs b/src/geometry.rs index c6b22a9..1215c35 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -2,8 +2,8 @@ use std::ops::{Add, Div, Sub}; #[derive(Clone, Copy, Debug, PartialEq)] pub struct P2 { - x: T, - y: T, + pub x: T, + pub y: T, } impl P2 { @@ -65,7 +65,7 @@ impl> Div for V2 { } pub struct Grid { - dims: V2, + pub dims: V2, elements: Vec, }