Add engine module
This commit is contained in:
parent
381df8d208
commit
d57e41a276
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
56
src/main.rs
56
src/main.rs
|
@ -1,23 +1,13 @@
|
||||||
extern crate piston_window;
|
extern crate piston_window;
|
||||||
|
|
||||||
use piston_window::*;
|
use piston_window::*;
|
||||||
|
mod engine;
|
||||||
|
|
||||||
enum Tile {
|
enum Tile {
|
||||||
Empty,
|
Empty,
|
||||||
Wall,
|
Wall,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Position {
|
|
||||||
x: f64,
|
|
||||||
y: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Player {
|
|
||||||
pos: Position,
|
|
||||||
angle: f64, // radian or degree
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut window: PistonWindow =
|
let mut window: PistonWindow =
|
||||||
WindowSettings::new("Rustenstein", [640, 480])
|
WindowSettings::new("Rustenstein", [640, 480])
|
||||||
|
@ -33,51 +23,11 @@ fn main() {
|
||||||
Tile::Wall, Tile::Wall, Tile::Wall, Tile::Wall, 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 mut engine = engine::Engine::new(window.size());
|
||||||
|
|
||||||
let horiz_fov = 90 as f64;
|
|
||||||
//let height_unit = 64;
|
|
||||||
|
|
||||||
while let Some(event) = window.next() {
|
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| {
|
window.draw_2d(&event, |context, graphics| {
|
||||||
clear([1.0; 4], graphics);
|
engine.render(context, 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;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue