Fix update and render
This commit is contained in:
parent
eb9dd99b96
commit
ffde674b89
@ -1,4 +1,4 @@
|
||||
use epd_waveshare::{color::Color, prelude::WaveshareDisplay};
|
||||
use epd_waveshare::color::Color;
|
||||
use maze::{BinaryMapVisitor, Direction};
|
||||
|
||||
use crate::Button;
|
||||
@ -8,6 +8,7 @@ use super::*;
|
||||
pub struct MazeGame {
|
||||
map: maze::BinaryMap<122, 122, 14884>,
|
||||
player: Point,
|
||||
old_player: Option<Point>,
|
||||
}
|
||||
|
||||
impl MazeGame {
|
||||
@ -15,14 +16,59 @@ impl MazeGame {
|
||||
Self {
|
||||
map: maze::BinaryMap::new(),
|
||||
player: Point { x: 0, y: 1 },
|
||||
old_player: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MazeGame {
|
||||
const X_OFFSET: i32 = 2;
|
||||
const Y_OFFSET: i32 = 2;
|
||||
|
||||
fn player_pos(&self) -> (u16, u16) {
|
||||
(self.player.x as u16, self.player.y as u16)
|
||||
}
|
||||
|
||||
fn draw_walls(&self, ctx: &mut Context) {
|
||||
let wall_style = PrimitiveStyleBuilder::new()
|
||||
.stroke_color(Color::Black)
|
||||
.stroke_width(3)
|
||||
.fill_color(Color::Black)
|
||||
.build();
|
||||
|
||||
for x in 0..122 {
|
||||
for y in 0..122 {
|
||||
match self.map.at(x, y) {
|
||||
maze::MazePart::Wall => {
|
||||
let p = Rectangle::new(
|
||||
Point::new(x as i32 + Self::X_OFFSET, y as i32 + Self::Y_OFFSET),
|
||||
Size::new(1, 1),
|
||||
);
|
||||
p.draw_styled(&wall_style, &mut ctx.epaper.display).unwrap();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_player(&self, ctx: &mut Context) {
|
||||
let player_style = PrimitiveStyleBuilder::new()
|
||||
.stroke_color(Color::Black)
|
||||
.stroke_width(1)
|
||||
.fill_color(Color::Black)
|
||||
.build();
|
||||
|
||||
let p = Rectangle::new(
|
||||
Point::new(
|
||||
self.player.x as i32 + Self::X_OFFSET,
|
||||
self.player.y as i32 + Self::Y_OFFSET,
|
||||
),
|
||||
Size::new(1, 1),
|
||||
);
|
||||
p.draw_styled(&player_style, &mut ctx.epaper.display)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl App for MazeGame {
|
||||
@ -32,80 +78,50 @@ impl App for MazeGame {
|
||||
BinaryMapVisitor.format(&mut grid, &mut self.map.0);
|
||||
}
|
||||
|
||||
fn update_and_draw(&mut self, ctx: &mut Context) -> Option<Action> {
|
||||
let Some(button) = ctx.button_pressed else {
|
||||
return None;
|
||||
fn draw(&self, ctx: &mut Context) {
|
||||
let Some(old) = &self.old_player else {
|
||||
self.draw_walls(ctx);
|
||||
self.draw_player(ctx);
|
||||
return;
|
||||
};
|
||||
let player_old = self.player.clone();
|
||||
|
||||
Some(match button {
|
||||
Button::Up if self.map.can_move(self.player_pos(), Direction::North) => {
|
||||
self.player.y -= 1;
|
||||
}
|
||||
Button::Down if self.map.can_move(self.player_pos(), Direction::South) => {
|
||||
self.player.y += 1;
|
||||
}
|
||||
Button::Left if self.map.can_move(self.player_pos(), Direction::West) => {
|
||||
self.player.x -= 1;
|
||||
}
|
||||
Button::Right if self.map.can_move(self.player_pos(), Direction::East) => {
|
||||
self.player.x += 1;
|
||||
}
|
||||
Button::Back => {}
|
||||
_ => {}
|
||||
});
|
||||
ctx.epaper.partial_update(
|
||||
player_old,
|
||||
old.clone(),
|
||||
Size {
|
||||
width: 0,
|
||||
height: 0,
|
||||
},
|
||||
&[Color::White as u8],
|
||||
);
|
||||
|
||||
let wall_style = PrimitiveStyleBuilder::new()
|
||||
.stroke_color(Color::Black)
|
||||
.stroke_width(3)
|
||||
.fill_color(Color::Black)
|
||||
.build();
|
||||
let player_style = PrimitiveStyleBuilder::new()
|
||||
.stroke_color(Color::Black)
|
||||
.stroke_width(1)
|
||||
.fill_color(Color::Black)
|
||||
.build();
|
||||
|
||||
const X_OFFSET: i32 = 2;
|
||||
const Y_OFFSET: i32 = 2;
|
||||
|
||||
for x in 0..122 {
|
||||
for y in 0..122 {
|
||||
match self.map.at(x, y) {
|
||||
maze::MazePart::Wall => {
|
||||
let p = Rectangle::new(
|
||||
Point::new(x as i32 + X_OFFSET, y as i32 + Y_OFFSET),
|
||||
Size::new(1, 1),
|
||||
);
|
||||
p.draw_styled(&wall_style, &mut ctx.epaper.display).unwrap();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let p = Rectangle::new(
|
||||
Point::new(
|
||||
self.player.x as i32 + X_OFFSET,
|
||||
self.player.y as i32 + Y_OFFSET,
|
||||
),
|
||||
Size::new(1, 1),
|
||||
);
|
||||
p.draw_styled(&player_style, &mut ctx.epaper.display)
|
||||
.unwrap();
|
||||
}
|
||||
None
|
||||
self.draw_player(ctx);
|
||||
}
|
||||
|
||||
fn draw(&self, _ctx: &mut Context) {}
|
||||
fn update(&mut self, ctx: &mut Context) -> Option<Action> {
|
||||
let Some(button) = ctx.button_pressed else {
|
||||
return None;
|
||||
};
|
||||
let player_old = self.player.clone();
|
||||
|
||||
match button {
|
||||
Button::Up if self.map.can_move(self.player_pos(), Direction::North) => {
|
||||
self.player.y -= 1;
|
||||
self.old_player = Some(player_old);
|
||||
}
|
||||
Button::Down if self.map.can_move(self.player_pos(), Direction::South) => {
|
||||
self.player.y += 1;
|
||||
self.old_player = Some(player_old);
|
||||
}
|
||||
Button::Left if self.map.can_move(self.player_pos(), Direction::West) => {
|
||||
self.player.x -= 1;
|
||||
self.old_player = Some(player_old);
|
||||
}
|
||||
Button::Right if self.map.can_move(self.player_pos(), Direction::East) => {
|
||||
self.player.x += 1;
|
||||
self.old_player = Some(player_old);
|
||||
}
|
||||
Button::Back => return Some(Action::GoToMenu),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
fn update(&mut self, _ctx: &mut Context) -> Option<Action> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
@ -35,28 +35,6 @@ impl Default for Application {
|
||||
}
|
||||
|
||||
impl Application {
|
||||
pub fn update(&mut self, ctx: &mut Context, trng: &mut Trng) {
|
||||
let Some(action) = (match self {
|
||||
Self::Menu(menu) => menu.update(ctx),
|
||||
Self::Maze(maze) => maze.update(ctx),
|
||||
}) else {
|
||||
return;
|
||||
};
|
||||
match action {
|
||||
Action::StartMaze => {
|
||||
let mut maze = MazeGame::new();
|
||||
maze.start(trng);
|
||||
*self = Application::Maze(maze);
|
||||
}
|
||||
Action::GoToMenu => {
|
||||
let mut menu = Menu::new();
|
||||
menu.start(trng);
|
||||
*self = Application::Menu(menu);
|
||||
}
|
||||
Action::StartPairs => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&self, ctx: &mut Context) {
|
||||
ctx.epaper.full_erase();
|
||||
match self {
|
||||
@ -80,11 +58,13 @@ impl Application {
|
||||
let mut maze = MazeGame::new();
|
||||
maze.start(trng);
|
||||
*self = Application::Maze(maze);
|
||||
self.draw(ctx);
|
||||
}
|
||||
Action::GoToMenu => {
|
||||
let mut menu = Menu::new();
|
||||
menu.start(trng);
|
||||
*self = Application::Menu(menu);
|
||||
self.draw(ctx);
|
||||
}
|
||||
Action::StartPairs => {}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user