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 maze::{BinaryMapVisitor, Direction};
|
||||||
|
|
||||||
use crate::Button;
|
use crate::Button;
|
||||||
@ -8,6 +8,7 @@ use super::*;
|
|||||||
pub struct MazeGame {
|
pub struct MazeGame {
|
||||||
map: maze::BinaryMap<122, 122, 14884>,
|
map: maze::BinaryMap<122, 122, 14884>,
|
||||||
player: Point,
|
player: Point,
|
||||||
|
old_player: Option<Point>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MazeGame {
|
impl MazeGame {
|
||||||
@ -15,14 +16,59 @@ impl MazeGame {
|
|||||||
Self {
|
Self {
|
||||||
map: maze::BinaryMap::new(),
|
map: maze::BinaryMap::new(),
|
||||||
player: Point { x: 0, y: 1 },
|
player: Point { x: 0, y: 1 },
|
||||||
|
old_player: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MazeGame {
|
impl MazeGame {
|
||||||
|
const X_OFFSET: i32 = 2;
|
||||||
|
const Y_OFFSET: i32 = 2;
|
||||||
|
|
||||||
fn player_pos(&self) -> (u16, u16) {
|
fn player_pos(&self) -> (u16, u16) {
|
||||||
(self.player.x as u16, self.player.y as 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 {
|
impl App for MazeGame {
|
||||||
@ -32,80 +78,50 @@ impl App for MazeGame {
|
|||||||
BinaryMapVisitor.format(&mut grid, &mut self.map.0);
|
BinaryMapVisitor.format(&mut grid, &mut self.map.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_and_draw(&mut self, ctx: &mut Context) -> Option<Action> {
|
fn draw(&self, ctx: &mut Context) {
|
||||||
let Some(button) = ctx.button_pressed else {
|
let Some(old) = &self.old_player else {
|
||||||
return None;
|
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(
|
ctx.epaper.partial_update(
|
||||||
player_old,
|
old.clone(),
|
||||||
Size {
|
Size {
|
||||||
width: 0,
|
width: 0,
|
||||||
height: 0,
|
height: 0,
|
||||||
},
|
},
|
||||||
&[Color::White as u8],
|
&[Color::White as u8],
|
||||||
);
|
);
|
||||||
|
self.draw_player(ctx);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(&self, _ctx: &mut Context) {}
|
|
||||||
|
|
||||||
fn update(&mut self, _ctx: &mut Context) -> Option<Action> {
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,28 +35,6 @@ impl Default for Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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) {
|
pub fn draw(&self, ctx: &mut Context) {
|
||||||
ctx.epaper.full_erase();
|
ctx.epaper.full_erase();
|
||||||
match self {
|
match self {
|
||||||
@ -80,11 +58,13 @@ impl Application {
|
|||||||
let mut maze = MazeGame::new();
|
let mut maze = MazeGame::new();
|
||||||
maze.start(trng);
|
maze.start(trng);
|
||||||
*self = Application::Maze(maze);
|
*self = Application::Maze(maze);
|
||||||
|
self.draw(ctx);
|
||||||
}
|
}
|
||||||
Action::GoToMenu => {
|
Action::GoToMenu => {
|
||||||
let mut menu = Menu::new();
|
let mut menu = Menu::new();
|
||||||
menu.start(trng);
|
menu.start(trng);
|
||||||
*self = Application::Menu(menu);
|
*self = Application::Menu(menu);
|
||||||
|
self.draw(ctx);
|
||||||
}
|
}
|
||||||
Action::StartPairs => {}
|
Action::StartPairs => {}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user