84 lines
2.3 KiB
Rust
84 lines
2.3 KiB
Rust
extern crate piston_window;
|
|
|
|
use piston_window::*;
|
|
|
|
enum Tile {
|
|
Empty,
|
|
Wall,
|
|
}
|
|
|
|
struct Position {
|
|
x: f64,
|
|
y: f64,
|
|
}
|
|
|
|
struct Player {
|
|
pos: Position,
|
|
angle: f64, // radian or degree
|
|
}
|
|
|
|
|
|
fn main() {
|
|
let mut window: PistonWindow =
|
|
WindowSettings::new("Rustenstein", [640, 480])
|
|
.exit_on_esc(true)
|
|
.resizable(false)
|
|
.build().unwrap();
|
|
|
|
let level: [Tile; 5 * 5] = [
|
|
Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall,
|
|
Tile::Wall, Tile::Empty, Tile::Empty, Tile::Empty, Tile::Wall,
|
|
Tile::Wall, Tile::Empty, Tile::Empty, Tile::Empty, Tile::Wall,
|
|
Tile::Wall, Tile::Empty, Tile::Empty, Tile::Empty, Tile::Wall,
|
|
Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall,
|
|
];
|
|
|
|
let mut player = Player { pos: Position { x: 2.0, y: 2.0}, angle: 0.0};
|
|
|
|
let horiz_fov = 90 as f64;
|
|
//let height_unit = 64;
|
|
|
|
while let Some(event) = window.next() {
|
|
let window_size = window.size();
|
|
let w = window_size.width as f64;
|
|
let h = window_size.height as f64;
|
|
|
|
window.draw_2d(&event, |context, graphics| {
|
|
clear([1.0; 4], graphics);
|
|
|
|
rectangle([1.0, 0.0, 0.0, 1.0], // red
|
|
[0.0, 0.0, 100.0, 100.0],
|
|
context.transform,
|
|
graphics);
|
|
|
|
// Ceiling
|
|
let ceiling_color = [0.3, 0.3, 0.3, 1.0];
|
|
rectangle(ceiling_color,
|
|
[0.0, 0.0, w, h / 2.0],
|
|
context.transform,
|
|
graphics);
|
|
|
|
// Floor
|
|
let floor_color = [0.5, 0.5, 0.5, 1.0];
|
|
rectangle(floor_color,
|
|
[0.0, h / 2.0, w, h / 2.0],
|
|
context.transform,
|
|
graphics);
|
|
|
|
// Walls
|
|
let left = player.angle + (horiz_fov / 2.0);
|
|
let right = player.angle - (horiz_fov / 2.0);
|
|
// for every angle (range / w)
|
|
let step = (left - right) / w;
|
|
let mut ray_angle = left;
|
|
for n in 0..(window_size.width - 1) {
|
|
// cast a ray
|
|
// see what wall it hits
|
|
// compute wall height
|
|
// draw wall portion
|
|
ray_angle += step;
|
|
}
|
|
});
|
|
}
|
|
}
|