Start drawing

This commit is contained in:
Adrian Woźniak 2024-09-16 14:15:42 +02:00
parent 6b7681bad6
commit f650894988
2 changed files with 121 additions and 2 deletions

View File

@ -470,9 +470,42 @@ impl<'s> BufWriter<'s> {
}
}
pub struct BinaryMap<const X: usize, const Y: usize, const SIZE: usize>;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Direction {
North,
South,
East,
West,
}
pub struct BinaryMap<const X: usize, const Y: usize, const SIZE: usize>(pub [MazePart; SIZE]);
impl<const X: usize, const Y: usize, const SIZE: usize> BinaryMap<X, Y, SIZE> {
pub fn new() -> Self {
Self([MazePart::Noop; SIZE])
}
pub fn at(&self, x: usize, y: usize) -> MazePart {
self.0[y * X + x]
}
pub fn can_move(&self, player: (usize, usize), dir: Direction) -> bool {
match dir {
Direction::West if player.0 == 0 => false,
Direction::West => self.at(player.0 - 1, player.1) != MazePart::Wall,
Direction::East if player.0 == X - 1 => false,
Direction::East => self.at(player.0 + 1, player.1) != MazePart::Wall,
Direction::North if player.1 == 0 => false,
Direction::North => self.at(player.0, player.1 - 1) != MazePart::Wall,
Direction::South if player.1 == Y - 1 => false,
Direction::South => self.at(player.0, player.1 + 1) != MazePart::Wall,
}
}
}
pub struct BinaryMapVisitor<const X: usize, const Y: usize, const SIZE: usize>;
impl<const X: usize, const Y: usize, const SIZE: usize> BinaryMapVisitor<X, Y, SIZE> {
fn format(&self, grid: &Grid<X, Y, SIZE>, buffer: &mut [MazePart]) {
let mut bw = BufWriter::new(buffer);
@ -574,3 +607,88 @@ mod print_tests {
println!("{result}");
}
}
#[cfg(test)]
mod bm_tests {
use crate::BinaryMap;
#[test]
fn move_n_valid() {
let player = (1, 1);
let bm = BinaryMap::<3, 3, 9>::new();
assert_eq!(bm.can_move(player, crate::Direction::North), true);
}
#[test]
fn move_n_invalid() {
let player = (0, 0);
let bm = BinaryMap::<3, 3, 9>::new();
assert_eq!(bm.can_move(player, crate::Direction::North), false);
}
#[test]
fn move_n_invalid_wall() {
let player = (1, 1);
let mut bm = BinaryMap::<3, 3, 9>::new();
bm.0.fill(crate::MazePart::Wall);
assert_eq!(bm.can_move(player, crate::Direction::North), false);
}
#[test]
fn move_s_valid() {
let player = (1, 1);
let bm = BinaryMap::<3, 3, 9>::new();
assert_eq!(bm.can_move(player, crate::Direction::South), true);
}
#[test]
fn move_s_invalid() {
let player = (2, 2);
let bm = BinaryMap::<3, 3, 9>::new();
assert_eq!(bm.can_move(player, crate::Direction::South), false);
}
#[test]
fn move_s_invalid_wall() {
let player = (1, 1);
let mut bm = BinaryMap::<3, 3, 9>::new();
bm.0.fill(crate::MazePart::Wall);
assert_eq!(bm.can_move(player, crate::Direction::South), false);
}
#[test]
fn move_e_valid() {
let player = (1, 1);
let bm = BinaryMap::<3, 3, 9>::new();
assert_eq!(bm.can_move(player, crate::Direction::East), true);
}
#[test]
fn move_e_invalid() {
let player = (2, 2);
let bm = BinaryMap::<3, 3, 9>::new();
assert_eq!(bm.can_move(player, crate::Direction::East), false);
}
#[test]
fn move_e_invalid_wall() {
let player = (1, 1);
let mut bm = BinaryMap::<3, 3, 9>::new();
bm.0.fill(crate::MazePart::Wall);
assert_eq!(bm.can_move(player, crate::Direction::East), false);
}
#[test]
fn move_w_valid() {
let player = (1, 1);
let bm = BinaryMap::<3, 3, 9>::new();
assert_eq!(bm.can_move(player, crate::Direction::West), true);
}
#[test]
fn move_w_invalid() {
let player = (0, 0);
let bm = BinaryMap::<3, 3, 9>::new();
assert_eq!(bm.can_move(player, crate::Direction::West), false);
}
#[test]
fn move_w_invalid_wall() {
let player = (1, 1);
let mut bm = BinaryMap::<3, 3, 9>::new();
bm.0.fill(crate::MazePart::Wall);
assert_eq!(bm.can_move(player, crate::Direction::West), false);
}
}

View File

@ -6,6 +6,8 @@ use display_interface_spi::SPIInterface;
use embedded_graphics::{
geometry::Point,
mono_font::MonoTextStyle,
pixelcolor::Rgb565,
primitives::{Line, PrimitiveStyle},
text::{Text, TextStyle},
Drawable,
};
@ -23,7 +25,6 @@ use esp_hal::{
};
use heapless::String;
use profont::PROFONT_24_POINT;
use shared::GenU32;
use weact_studio_epd::{graphics::Display290BlackWhite, Color};
use weact_studio_epd::{graphics::DisplayRotation, WeActStudio290BlackWhiteDriver};