This commit is contained in:
Felix Ableitner 2021-06-07 23:50:39 +02:00
parent 0059b97198
commit f64c07d382
7 changed files with 446 additions and 411 deletions

756
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2018"
[dependencies]
bevy = "0.4"
bevy_rapier3d = "0.7.0"
bevy = "0.5"
bevy_rapier3d = "0.9"

View file

@ -24,7 +24,7 @@ fn fps_update_system(diagnostics: Res<Diagnostics>, mut query: Query<&mut Text,
for mut text in query.iter_mut() {
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(average) = fps.value() {
text.value = format!("{:.0} fps", average);
text.sections.first_mut().unwrap().value = format!("{:.0} fps", average);
}
}
}
@ -44,17 +44,17 @@ fn speed_update_system(
let player_speed_2d = player_velocity_2d.length() / time.delta_seconds();
for mut text in text_query.iter_mut() {
text.value = format!("{:.0} m/s", player_speed_2d);
text.sections.first_mut().unwrap().value = format!("{:.0} m/s", player_speed_2d);
}
}
}
fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands
// UI camera
.spawn(CameraUiBundle::default())
.spawn_bundle(UiCameraBundle::default());
// fps display
.spawn(TextBundle {
commands.spawn_bundle(TextBundle {
style: Style {
align_self: AlignSelf::FlexEnd,
position_type: PositionType::Absolute,
@ -65,20 +65,21 @@ fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
},
..Default::default()
},
text: Text {
value: "".to_string(),
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
style: TextStyle {
text: Text::with_section(
"".to_string(),
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 16.0,
color: Color::WHITE,
..Default::default()
},
},
TextAlignment::default()
),
..Default::default()
})
.with(FpsText)
.insert(FpsText);
// speed display
.spawn(TextBundle {
commands.spawn_bundle(TextBundle {
style: Style {
// TODO: dont know how to properly center this horizontally
align_self: AlignSelf::Center,
@ -90,16 +91,16 @@ fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
},
..Default::default()
},
text: Text {
value: "speed".to_string(),
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
style: TextStyle {
text: Text::with_section(
"speed".to_string(),
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 16.0,
color: Color::rgba(1.0, 1.0, 1.0, 0.5),
..Default::default()
},
},
..Default::default()
TextAlignment { vertical: VerticalAlign::Center, horizontal: HorizontalAlign::Center},
), ..Default::default()
})
.with(SpeedText);
.insert(SpeedText);
}

View file

@ -4,8 +4,7 @@ use rake::{hud::HudPlugin, player::PlayerPlugin, weapon::WeaponPlugin, world::Wo
fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.add_resource(WindowDescriptor {
.insert_resource(WindowDescriptor {
title: "Rake".to_string(),
width: 960.,
height: 540.,

View file

@ -57,16 +57,16 @@ const MOUSE_SENSITIVITY: f32 = 0.5;
const PLAYER_HEIGHT: f32 = 1.8;
const PLAYER_WIDTH: f32 = 0.25;
fn init_player(commands: &mut Commands) {
fn init_player(mut commands: Commands) {
commands
.spawn(PlayerBundle::default())
.spawn_bundle(PlayerBundle::default())
.with_children(|parent| {
parent
.spawn(Camera3dBundle {
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_translation(Vec3::new(0.0, PLAYER_HEIGHT / 2., 0.0)),
..Default::default()
})
.with(CameraData::default());
.insert(CameraData::default());
});
}
@ -124,7 +124,7 @@ fn player_movement_system(
}
fn straight_vector(rotation: &Quat) -> Vec3 {
let v = rotation.mul_vec3(Vec3::unit_z());
let v = rotation.mul_vec3(Vec3::Z);
Vec3::new(v.x, 0.0, v.z).normalize()
}
@ -148,12 +148,11 @@ fn movement_axis(input: &Res<Input<KeyCode>>, plus: KeyCode, minus: KeyCode) ->
fn player_look_system(
time: Res<Time>,
mut mouse_event_reader: Local<EventReader<MouseMotion>>,
mouse_motion_events: Res<Events<MouseMotion>>,
mut mouse_event_reader: EventReader<MouseMotion>,
mut query: Query<(&mut Transform, &mut CameraData)>,
) {
let mut delta: Vec2 = Vec2::zero();
for event in mouse_event_reader.iter(&mouse_motion_events) {
let mut delta: Vec2 = Vec2::ZERO;
for event in mouse_event_reader.iter() {
delta += event.delta;
}
if delta.is_nan() {
@ -169,10 +168,10 @@ fn player_look_system(
data.yaw = clamp(data.yaw, -half_pi, half_pi);
data.pitch = wrap(data.pitch, -PI, PI);
let quat_yaw = Quat::from_axis_angle(Vec3::unit_x(), data.yaw);
let quat_pitch = Quat::from_axis_angle(Vec3::unit_y(), data.pitch);
let quat_yaw = Quat::from_axis_angle(Vec3::X, data.yaw);
let quat_pitch = Quat::from_axis_angle(Vec3::Y, data.pitch);
let mut orientation = Quat::identity();
let mut orientation = Quat::IDENTITY;
orientation = orientation * quat_yaw;
orientation = quat_pitch * orientation;

View file

@ -18,7 +18,7 @@ pub struct WeaponPlugin;
impl Plugin for WeaponPlugin {
fn build(&self, app: &mut AppBuilder) {
app
.add_resource(WeaponData::default())
.insert_resource(WeaponData::default())
.add_system(shoot_system.system())
.add_system(projectile_system.system());
}
@ -39,17 +39,16 @@ impl Default for WeaponData {
}
fn shoot_system(
mut mouse_event_reader: Local<EventReader<MouseButtonInput>>,
mouse_button_events: Res<Events<MouseButtonInput>>,
mut mouse_event_reader: EventReader<MouseButtonInput>,
time: Res<Time>,
mut weapon: ResMut<WeaponData>,
commands: &mut Commands,
commands: Commands,
meshes: ResMut<Assets<Mesh>>,
materials: ResMut<Assets<StandardMaterial>>,
player_location_query: Query<&Transform, With<PlayerMarker>>,
) {
if weapon.reloading {
weapon.reload_timer.tick(time.delta_seconds());
weapon.reload_timer.tick(time.delta());
if weapon.reload_timer.finished() {
weapon.reload_timer.reset();
weapon.reloading = false;
@ -57,7 +56,7 @@ fn shoot_system(
}
let mut fired = false;
for event in mouse_event_reader.iter(&mouse_button_events) {
for event in mouse_event_reader.iter() {
if event.button == MouseButton::Left && event.state == ElementState::Pressed {
fired = true;
}
@ -76,7 +75,7 @@ fn shoot_system(
struct ProjectileMarker;
fn spawn_projectile(
commands: &mut Commands,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
player_transform: &Transform,
@ -88,22 +87,21 @@ fn spawn_projectile(
let player_rot = player_transform.rotation;
commands
.spawn(PbrBundle {
.spawn_bundle(PbrBundle {
mesh: meshes.add(projectile_mesh),
material: materials.add(projectile_material),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
..Default::default()
})
.with(ProjectileMarker::default())
.with(
.insert(ProjectileMarker::default())
.insert(
RigidBodyBuilder::new_dynamic()
.translation(player_trans.x, player_trans.y, player_trans.z)
.rotation(Matrix3x1::new(player_rot.x, player_rot.y, player_rot.z))
.mass(0.0, false)
.lock_translations()
.lock_rotations(),
)
.with(ColliderBuilder::cylinder(0.1, 0.1));
.insert(ColliderBuilder::cylinder(0.1, 0.1));
}
fn projectile_system(

View file

@ -10,7 +10,7 @@ impl Plugin for WorldPlugin {
}
fn create_world(
commands: &mut Commands,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
@ -22,7 +22,7 @@ fn create_world(
for x in -(AMOUNT / 2)..(AMOUNT / 2) {
for y in -(AMOUNT / 2)..(AMOUNT / 2) {
for z in -(AMOUNT / 2)..(AMOUNT / 2) {
commands.spawn(PbrBundle {
commands.spawn_bundle(PbrBundle {
mesh: box_mesh.clone(),
material: box_material.clone(),
transform: Transform::from_translation(Vec3::new(x as f32, y as f32, z as f32)),
@ -33,7 +33,7 @@ fn create_world(
}
// a simple light
commands.spawn(LightBundle {
commands.spawn_bundle(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
..Default::default()
});
@ -42,8 +42,8 @@ fn create_world(
let floor_texture_handle = materials.add(asset_server.load("textures/blocks17floor2.png").into());
let rigid_body1 = RigidBodyBuilder::new_static();
let collider1 = ColliderBuilder::cuboid(10.0, 0.1, 10.0);
commands.spawn((rigid_body1, collider1));
commands.spawn(PbrBundle {
commands.spawn_bundle((rigid_body1, collider1));
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 20. })),
material: floor_texture_handle,
transform: Transform::from_translation(Vec3::new(0.0, 0.1, 0.0)),