Add engine module
This commit is contained in:
parent
381df8d208
commit
d57e41a276
2 changed files with 71 additions and 53 deletions
68
src/engine.rs
Normal file
68
src/engine.rs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue