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 crossterm::event::{Event, KeyCode};
|
||||||
use ratatui::{
|
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};
|
use crate::geometry::{Grid, P2, V2};
|
||||||
@ -41,6 +45,12 @@ impl GameEvent {
|
|||||||
struct Room {
|
struct Room {
|
||||||
size: V2<usize>,
|
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 {
|
impl GameModel {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
@ -56,10 +66,14 @@ impl GameModel {
|
|||||||
match GameEvent::from_crossterm(event) {
|
match GameEvent::from_crossterm(event) {
|
||||||
GameEvent::Quit => None,
|
GameEvent::Quit => None,
|
||||||
GameEvent::Nop => Some(self),
|
GameEvent::Nop => Some(self),
|
||||||
GameEvent::MovePlayer(direction) => Some(Self {
|
GameEvent::MovePlayer(direction) => {
|
||||||
player_pos: self.player_pos + direction,
|
let player_pos = self.player_pos + direction;
|
||||||
..self
|
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))
|
.get_mut(P2::new(5, 2))
|
||||||
.map(|tile| *tile = Tile::Amphora);
|
.map(|tile| *tile = Tile::Amphora);
|
||||||
|
|
||||||
tiles
|
tiles.get_mut(P2::new(8, 5)).map(|tile| *tile = Tile::Frog);
|
||||||
.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
|
tiles
|
||||||
.get_mut(self.player_pos)
|
.get_mut(self.player_pos)
|
||||||
.map(|tile| *tile = Tile::Player);
|
.map(|tile| *tile = Tile::Player);
|
||||||
|
|
||||||
|
|
||||||
tiles
|
tiles
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,6 +123,7 @@ enum Tile {
|
|||||||
Player,
|
Player,
|
||||||
Amphora,
|
Amphora,
|
||||||
Frog,
|
Frog,
|
||||||
|
Water,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tile {
|
impl Tile {
|
||||||
@ -123,6 +145,11 @@ impl Tile {
|
|||||||
// Alternative symbol: Ö̶͈
|
// Alternative symbol: Ö̶͈
|
||||||
cell.set_symbol("ä̃").set_fg(Color::Green);
|
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 y in 0..area.height {
|
||||||
for x in 0..area.width {
|
for x in 0..area.width {
|
||||||
let cell = &mut buf[(area.left() + x, area.top() + y)];
|
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);
|
tile.render(cell);
|
||||||
} else {
|
} else {
|
||||||
cell.set_symbol(".");
|
cell.set_symbol(".");
|
||||||
|
|||||||
@ -2,8 +2,8 @@ use std::ops::{Add, Div, Sub};
|
|||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
pub struct P2<T> {
|
pub struct P2<T> {
|
||||||
x: T,
|
pub x: T,
|
||||||
y: T,
|
pub y: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> P2<T> {
|
impl<T> P2<T> {
|
||||||
@ -65,7 +65,7 @@ impl<T: Copy + Div<T>> Div<T> for V2<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Grid<T> {
|
pub struct Grid<T> {
|
||||||
dims: V2<usize>,
|
pub dims: V2<usize>,
|
||||||
elements: Vec<T>,
|
elements: Vec<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user