Skip to content

Commit 054fe9e

Browse files
committed
Fix warnings, add collision
1 parent 1c44b56 commit 054fe9e

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

src/main.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
extern crate sdl2;
22

3+
use map::MAP;
34
use nalgebra::Rotation2;
45
use player::{Player, MOVE_SPEED};
5-
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
66
use sdl2::event::Event;
77
use sdl2::keyboard::Keycode;
88
use sdl2::pixels::Color;
9-
use sdl2::rect::Point;
109
use std::collections::HashSet;
1110
use std::time::Instant;
11+
use tile::Tile;
1212

1313
mod map;
1414
mod player;
@@ -19,7 +19,6 @@ pub fn main() {
1919
// Create SDL context for video
2020
let sdl_context = sdl2::init().unwrap();
2121
let video_subsystem = sdl_context.video().unwrap();
22-
let timer_subsystem = sdl_context.timer().unwrap();
2322

2423
// Create a window
2524
// position_centered - puts the window in the middle of the screen
@@ -85,23 +84,29 @@ pub fn main() {
8584
// Calculate the movement speed.
8685
let move_speed = MOVE_SPEED * delta_time as f64;
8786

87+
let mut new_position = player.position;
88+
8889
// Handle movement with keyboard
8990
for key in keys {
9091
match key {
91-
Keycode::W => player.position += player.direction * move_speed,
92+
Keycode::W => new_position += player.direction * move_speed,
9293
Keycode::A => {
93-
player.position.x += player.direction.y * move_speed;
94-
player.position.y -= player.direction.x * move_speed;
94+
new_position.x += player.direction.y * move_speed;
95+
new_position.y -= player.direction.x * move_speed;
9596
}
96-
Keycode::S => player.position -= player.direction * move_speed,
97+
Keycode::S => new_position -= player.direction * move_speed,
9798
Keycode::D => {
98-
player.position.x -= player.direction.y * move_speed;
99-
player.position.y += player.direction.x * move_speed;
99+
new_position.x -= player.direction.y * move_speed;
100+
new_position.y += player.direction.x * move_speed;
100101
}
101102
_ => {}
102103
}
103104
}
104105

106+
if MAP[new_position.x as usize][new_position.y as usize] == Tile::Floor {
107+
player.position = new_position;
108+
}
109+
105110
// Rotate the player's direction using the mouse's (relative) X
106111
// position. 0.001 is a random small number.
107112
let rotation = Rotation2::new(mouse_state.x() as f64 * 0.001);

src/player.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use nalgebra::Vector2;
22

33
/// The move speed of the player, to be compensated with a delta time.
4-
pub const MOVE_SPEED: f64 = 0.00000001;
4+
pub const MOVE_SPEED: f64 = 0.000000005;
55

66
/// The Player struct stores our player's position, direction, and plane
77
pub struct Player {

src/ray_caster.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use std::cmp::{max, min};
1+
use std::cmp::max;
22

33
use nalgebra::Vector2;
4+
use sdl2::pixels::Color;
45
use sdl2::rect::Point;
56
use sdl2::render::Canvas;
67
use sdl2::video::Window;
@@ -48,7 +49,7 @@ pub fn draw_ray(player: &Player, x: u32, canvas: &mut Canvas<Window>) {
4849
let delta_dist = Vector2::new((1.0 / ray_dir.x).abs(), (1.0 / ray_dir.y).abs());
4950

5051
// The distance that the ray has travelled
51-
let mut perp_wall_dist = 0.0;
52+
let perp_wall_dist;
5253

5354
// Which direction to step in. The DDA will always jump exactly one square.
5455
let mut step = Vector2::new(0, 0);
@@ -57,7 +58,7 @@ pub fn draw_ray(player: &Player, x: u32, canvas: &mut Canvas<Window>) {
5758
// but it doesn't really matter what the intial value is.
5859
let mut hit_side: HitSide;
5960

60-
let mut hit_tile: Tile;
61+
let hit_tile: Tile;
6162

6263
// Here, we calculate our step and initial distance using the ray direction.
6364

@@ -129,7 +130,14 @@ pub fn draw_ray(player: &Player, x: u32, canvas: &mut Canvas<Window>) {
129130
};
130131

131132
if let Tile::Wall(colour) = hit_tile {
132-
canvas.set_draw_color(colour);
133+
// If the hit side is vertical, half the colour to give some shading
134+
match hit_side {
135+
HitSide::Horizontal => canvas.set_draw_color(colour),
136+
HitSide::Vertical => {
137+
canvas.set_draw_color(Color::RGB(colour.r / 2, colour.g / 2, colour.b / 2))
138+
}
139+
}
140+
133141
let start = Point::new(x as i32, draw_start);
134142
let end = Point::new(x as i32, draw_end);
135143
canvas.draw_line(start, end).expect("Failed to draw ray!");

0 commit comments

Comments
 (0)