Add basic player movement system
This commit is contained in:
		@@ -19,5 +19,6 @@ fn main() {
 | 
				
			|||||||
        .add_plugins(DefaultPlugins)
 | 
					        .add_plugins(DefaultPlugins)
 | 
				
			||||||
        .add_startup_system(setup.system())
 | 
					        .add_startup_system(setup.system())
 | 
				
			||||||
        .add_startup_system(add_player.system())
 | 
					        .add_startup_system(add_player.system())
 | 
				
			||||||
 | 
					        .add_system(player_movement.system())
 | 
				
			||||||
        .run();
 | 
					        .run();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -50,6 +50,35 @@ pub fn add_player(
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn player_movement(
 | 
				
			||||||
 | 
					    keyboard_input: Res<Input<KeyCode>>,
 | 
				
			||||||
 | 
					    mut query: Query<(&Player, &mut Transform)>,
 | 
				
			||||||
 | 
					    time: Res<Time>,
 | 
				
			||||||
 | 
					) {
 | 
				
			||||||
 | 
					    if let Ok((_, mut transform)) = query.single_mut() {
 | 
				
			||||||
 | 
					        let mut direction = Vec2::ZERO;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if keyboard_input.pressed(KeyCode::A) {
 | 
				
			||||||
 | 
					            direction.x -= 1.0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if keyboard_input.pressed(KeyCode::D) {
 | 
				
			||||||
 | 
					            direction.x += 1.0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if keyboard_input.pressed(KeyCode::S) {
 | 
				
			||||||
 | 
					            direction.y -= 1.0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if keyboard_input.pressed(KeyCode::W) {
 | 
				
			||||||
 | 
					            direction.y += 1.0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let translation = &mut transform.translation;
 | 
				
			||||||
 | 
					        let delta_seconds = time.delta_seconds();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        translation.x += delta_seconds * direction.x * 100.0;
 | 
				
			||||||
 | 
					        translation.y += delta_seconds * direction.y * 100.0;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn greet_player(
 | 
					pub fn greet_player(
 | 
				
			||||||
    query: Query<&Name, With<Player>>,
 | 
					    query: Query<&Name, With<Player>>,
 | 
				
			||||||
    time: Res<Time>,
 | 
					    time: Res<Time>,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user