Room bounds check
This commit is contained in:
parent
1b71e4c62f
commit
57a442c5e2
50
src/game.rs
50
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<usize>,
|
||||
}
|
||||
impl Room {
|
||||
fn in_bounds(&self, player_pos: P2<isize>) -> 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(".");
|
||||
|
||||
@ -2,8 +2,8 @@ use std::ops::{Add, Div, Sub};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct P2<T> {
|
||||
x: T,
|
||||
y: T,
|
||||
pub x: T,
|
||||
pub y: T,
|
||||
}
|
||||
|
||||
impl<T> P2<T> {
|
||||
@ -65,7 +65,7 @@ impl<T: Copy + Div<T>> Div<T> for V2<T> {
|
||||
}
|
||||
|
||||
pub struct Grid<T> {
|
||||
dims: V2<usize>,
|
||||
pub dims: V2<usize>,
|
||||
elements: Vec<T>,
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user