11extern crate sdl2;
22
3+ use map:: MAP ;
34use nalgebra:: Rotation2 ;
45use player:: { Player , MOVE_SPEED } ;
5- use rayon:: prelude:: { IntoParallelIterator , ParallelIterator } ;
66use sdl2:: event:: Event ;
77use sdl2:: keyboard:: Keycode ;
88use sdl2:: pixels:: Color ;
9- use sdl2:: rect:: Point ;
109use std:: collections:: HashSet ;
1110use std:: time:: Instant ;
11+ use tile:: Tile ;
1212
1313mod map;
1414mod 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 ) ;
0 commit comments