Add engine module

wip
Fabien Freling 2017-10-22 22:22:49 +02:00
parent 381df8d208
commit d57e41a276
2 changed files with 71 additions and 53 deletions

68
src/engine.rs Normal file
View File

@ -0,0 +1,68 @@
extern crate piston_window;
use piston_window::*;
struct Position {
x: f64,
y: f64,
}
struct Player {
pos: Position,
angle: f64, // radian or degree
}
pub struct Engine {
w: f64,
h: f64,
horiz_fov: f64,
player: Player,
}
impl Engine {
pub fn new(size: Size) -> Engine {
Engine {
w: size.width as f64,
h: size.height as f64,
horiz_fov: 90.,
player: Player {
pos: Position { x: 2., y: 2. },
angle: 0.,
}
}
}
pub fn render(&mut self, context: Context, graphics: &mut G2d) {
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);
// 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);
// Walls
let left = self.player.angle + (self.horiz_fov / 2.0);
let right = self.player.angle - (self.horiz_fov / 2.0);
// for every angle (range / w)
let step = (left - right) / self.w;
let mut ray_angle = left;
let width = self.w as i32;
for n in 0..width {
// cast a ray
// see what wall it hits
// compute wall height
// draw wall portion
ray_angle += step;
}
}
}

View File

@ -1,23 +1,13 @@
extern crate piston_window;
use piston_window::*;
mod engine;
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])
@ -33,51 +23,11 @@ fn main() {
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;
let mut engine = engine::Engine::new(window.size());
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;
}
engine.render(context, graphics);
});
}
}