change window dep from piston to minifb

This commit is contained in:
Fabien Freling 2021-11-04 14:03:12 +01:00
parent 4620f647ad
commit 3844c0fe80
5 changed files with 728 additions and 1079 deletions

View file

@ -1,5 +1,5 @@
extern crate piston_window;
use piston_window::*;
// extern crate piston_window;
// use piston_window::*;
use std::f64::consts::*;
@ -24,12 +24,12 @@ type Radian = f64;
#[derive(Debug)]
struct Player {
pos: Position,
angle: Degree,
pos: Position,
angle: Degree,
}
impl Player {
pub fn player_space_distance(&self, other: Position) ->f64 {
pub fn player_space_distance(&self, other: Position) -> f64 {
let rad = self.angle.to_radians();
let dx = other.x - self.pos.x;
let dy = other.y - self.pos.y;
@ -42,7 +42,6 @@ impl Player {
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Tile {
Empty,
@ -159,10 +158,10 @@ pub struct Engine {
}
impl Engine {
pub fn new(size: Size) -> Engine {
pub fn new(width: usize, height: usize) -> Engine {
Engine {
w: size.width as f64,
h: size.height as f64,
w: width as f64,
h: height as f64,
horiz_fov: 90.,
player: Player {
pos: Position { x: 1.5, y: 2. },
@ -177,22 +176,26 @@ impl Engine {
}
}
pub fn render(&mut self, context: Context, graphics: &mut G2d) {
clear([1.0; 4], graphics);
pub fn render(&mut self, buffer: &mut Vec<u32>) {
// clear([1.0; 4], graphics);
// Ceiling
let ceiling_color = [0.3, 0.3, 0.3, 1.0];
rectangle(ceiling_color,
[0.0, 0.0, self.w, self.h / 2.0],
context.transform,
graphics);
// // Ceiling
// let ceiling_color = [0.3, 0.3, 0.3, 1.0];
// rectangle(
// ceiling_color,
// [0.0, 0.0, self.w, self.h / 2.0],
// context.transform,
// graphics,
// );
// Floor
let floor_color = [0.5, 0.5, 0.5, 1.0];
rectangle(floor_color,
[0.0, self.h / 2.0, self.w, self.h / 2.0],
context.transform,
graphics);
// // Floor
// let floor_color = [0.5, 0.5, 0.5, 1.0];
// rectangle(
// floor_color,
// [0.0, self.h / 2.0, self.w, self.h / 2.0],
// context.transform,
// graphics,
// );
let left = self.player.angle + (self.horiz_fov / 2.0);
let step = self.horiz_fov / self.w;
@ -211,11 +214,16 @@ impl Engine {
p if (p.x.trunc() + p.y.trunc()) % 4.0 == 3.0 => [0.7, 0.3, 0.9, 1.0],
_ => [1.0, 0.0, 0.0, 1.0],
};
println!("ray: {}, angle: {}, wall at {:?}, distance: {}", n, ray_angle, pos, distance);
rectangle(wall_color,
[n as f64, (self.h - wall_height) / 2.0, 1.0, wall_height],
context.transform,
graphics);
println!(
"ray: {}, angle: {}, wall at {:?}, distance: {}",
n, ray_angle, pos, distance
);
// rectangle(
// wall_color,
// [n as f64, (self.h - wall_height) / 2.0, 1.0, wall_height],
// context.transform,
// graphics,
// );
};
}
@ -241,7 +249,7 @@ impl Engine {
Movement::Backward => {
self.player.pos.x -= self.player.angle.to_radians().cos() * dt;
self.player.pos.y -= self.player.angle.to_radians().sin() * dt;
},
}
Movement::TurnLeft => {
self.player.angle += 90.0 * dt;
self.player.angle = (self.player.angle + 360.0) % 360.0;
@ -251,8 +259,14 @@ impl Engine {
self.player.angle = (self.player.angle + 360.0) % 360.0;
}
}
if !self.level.contains(self.player.pos) || self.level.tile_at(self.player.pos) == Tile::Wall {
println!("Invalid position {:?}, tile = {:?}", self.player.pos, self.level.tile_at(self.player.pos));
if !self.level.contains(self.player.pos)
|| self.level.tile_at(self.player.pos) == Tile::Wall
{
println!(
"Invalid position {:?}, tile = {:?}",
self.player.pos,
self.level.tile_at(self.player.pos)
);
self.player.pos = previous;
}
}
@ -278,31 +292,71 @@ mod tests {
angle: 0.,
};
fcmp(player.player_space_distance(super::Position { x: 4., y: 1. }), 2.);
fcmp(player.player_space_distance(super::Position { x: 4., y: 2. }), 2.);
fcmp(player.player_space_distance(super::Position { x: 4., y: 3. }), 2.);
fcmp(
player.player_space_distance(super::Position { x: 4., y: 1. }),
2.,
);
fcmp(
player.player_space_distance(super::Position { x: 4., y: 2. }),
2.,
);
fcmp(
player.player_space_distance(super::Position { x: 4., y: 3. }),
2.,
);
player.angle = 90.;
fcmp(player.player_space_distance(super::Position { x: 1., y: 4. }), 2.);
fcmp(player.player_space_distance(super::Position { x: 2., y: 4. }), 2.);
fcmp(player.player_space_distance(super::Position { x: 3., y: 4. }), 2.);
fcmp(
player.player_space_distance(super::Position { x: 1., y: 4. }),
2.,
);
fcmp(
player.player_space_distance(super::Position { x: 2., y: 4. }),
2.,
);
fcmp(
player.player_space_distance(super::Position { x: 3., y: 4. }),
2.,
);
player.angle = 135.;
fcmp(player.player_space_distance(super::Position { x: 0., y: 2. }), 2.);
fcmp(
player.player_space_distance(super::Position { x: 0., y: 2. }),
2.,
);
player.angle = 180.;
fcmp(player.player_space_distance(super::Position { x: 0., y: 1. }), 2.);
fcmp(player.player_space_distance(super::Position { x: 0., y: 2. }), 2.);
fcmp(player.player_space_distance(super::Position { x: 0., y: 3. }), 2.);
fcmp(
player.player_space_distance(super::Position { x: 0., y: 1. }),
2.,
);
fcmp(
player.player_space_distance(super::Position { x: 0., y: 2. }),
2.,
);
fcmp(
player.player_space_distance(super::Position { x: 0., y: 3. }),
2.,
);
player.angle = 270.;
fcmp(player.player_space_distance(super::Position { x: 1., y: 0. }), 2.);
fcmp(player.player_space_distance(super::Position { x: 2., y: 0. }), 2.);
fcmp(player.player_space_distance(super::Position { x: 3., y: 0. }), 2.);
fcmp(
player.player_space_distance(super::Position { x: 1., y: 0. }),
2.,
);
fcmp(
player.player_space_distance(super::Position { x: 2., y: 0. }),
2.,
);
fcmp(
player.player_space_distance(super::Position { x: 3., y: 0. }),
2.,
);
}
#[test]
fn tile_at() {
#[rustfmt::skip]
let tiles = vec![
Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall,
Tile::Wall, Tile::Empty, Tile::Empty, Tile::Empty, Tile::Wall,
@ -313,9 +367,12 @@ mod tests {
let level = Level {
width: 5,
height: 5,
tiles
tiles,
};
assert_eq!(level.tile_at(super::Position { x: 2.0, y: 2.0 }), Tile::Empty);
assert_eq!(
level.tile_at(super::Position { x: 2.0, y: 2.0 }),
Tile::Empty
);
}
#[test]
@ -336,6 +393,7 @@ mod tests {
#[test]
fn closest_point() {
#[rustfmt::skip]
let tiles = vec![
Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall,
Tile::Wall, Tile::Empty, Tile::Empty, Tile::Empty, Tile::Wall,
@ -346,7 +404,7 @@ mod tests {
let level = Level {
width: 5,
height: 5,
tiles
tiles,
};
let position = super::Position { x: 2.5, y: 2.0 };
@ -377,7 +435,8 @@ mod tests {
let right_ray_angle = ((-(n as f64) * step) + 360.0) % 360.0;
let right_ray_radian = right_ray_angle.to_radians();
let (_right_tile, right_pos) = super::closest_point(&level, &position, right_ray_radian);
let (_right_tile, right_pos) =
super::closest_point(&level, &position, right_ray_radian);
println!("left: {:?}, angle: {}", left_pos, left_ray_angle);
println!("right: {:?}, angle: {}", right_pos, right_ray_angle);