add player model and zooming

main
annieversary 2022-08-13 00:18:20 +01:00
parent b52b1b0d42
commit 96453bfd49
5 changed files with 93 additions and 24 deletions

Binary file not shown.

View File

@ -15,11 +15,10 @@ fn fragment(
@builtin(position) position: vec4<f32>,
#import bevy_sprite::mesh2d_vertex_output
) -> @location(0) vec4<f32> {
// Get screen position with coordinates from 0 to 1
let uv = position.xy / vec2<f32>(view.width, view.height);
let uv = floor(uv * 400.0) / 400.0;
let col = textureSample(texture, our_sampler, uv);
let col = texturesample(texture, our_sampler, uv);
return round(col * 50.0) / 50.0;
}

View File

@ -19,7 +19,9 @@ use crate::{pillar::UnlitPillar, player::*};
pub struct MyRaycastSet;
#[derive(Component)]
pub struct Camera;
pub struct Camera {
distance_to_player: f32,
}
pub fn spawn_camera(
mut commands: Commands,
@ -66,7 +68,9 @@ pub fn spawn_camera(
},
..default()
})
.insert(Camera)
.insert(Camera {
distance_to_player: 100.0,
})
.insert(RayCastSource::<MyRaycastSet>::new());
// This specifies the layer used for the post processing camera, which will be attached to the post processing camera and 2d quad.
@ -139,8 +143,8 @@ pub fn camera_follow_player(
return;
};
for (mut trans, _) in camera.iter_mut() {
trans.translation = player_pos + Vec3::new(-100.0, 200.0, 100.0);
for (mut trans, camera) in camera.iter_mut() {
trans.translation = player_pos + Vec3::new(-1.0, 2.0, 1.0) * camera.distance_to_player;
trans.look_at(player_pos, Vec3::Y);
}
}
@ -195,3 +199,11 @@ pub fn update_processed_mouse_coords(
}
}
}
pub fn update_distance_to_player(input: Res<Input<KeyCode>>, mut camera: Query<&mut Camera>) {
let pressed = input.pressed(KeyCode::Space);
for mut camera in &mut camera {
let dis = if pressed { 20.0 } else { 100.0 };
camera.distance_to_player += (dis - camera.distance_to_player) / 80.0;
}
}

View File

@ -51,12 +51,15 @@ fn main() {
.add_system_set(
SystemSet::on_enter(AppState::Game)
.with_system(setup)
.with_system(spawn_player)
.with_system(spawn_camera),
)
.add_system_set(
SystemSet::on_update(AppState::Game)
.with_system(setup_scene_once_loaded)
.with_system(update_raw_mouse_coords)
.with_system(update_processed_mouse_coords)
.with_system(update_distance_to_player)
.with_system(close_on_esc)
.with_system(light_movement)
.with_system(move_light_friends)
@ -137,9 +140,6 @@ fn setup(
Vec3::new(30.0, 1.0, 50.0),
);
// player
spawn_player(&mut commands, &mut meshes, &mut materials);
// light balls
for i in 2..12 {
spawn_light_ball(

View File

@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::{prelude::*, render::view::NoFrustumCulling};
use crate::{camera::MouseCoords, illumination::Illumination, pillar::PillarActivationProgress};
@ -9,27 +9,41 @@ pub struct PlayerLight {
i: u8,
}
pub struct PlayerAnimations {
idle: Handle<AnimationClip>,
run: Handle<AnimationClip>,
}
pub fn spawn_player(
commands: &mut Commands,
meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>,
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Insert a resource with the current scene information
commands.insert_resource(PlayerAnimations {
idle: asset_server.load("models/scout_girl.glb#Animation0"),
run: asset_server.load("models/scout_girl.glb#Animation1"),
});
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Capsule {
radius: 0.5,
..Default::default()
})),
material: materials.add(Color::rgb(1.0, 1.0, 1.0).into()),
.spawn_bundle(SpatialBundle {
transform: Transform::from_xyz(0.0, 1.0, 0.0),
..Default::default()
})
.insert(Player);
.insert(Player)
.add_children(|parent| {
parent.spawn_bundle(SceneBundle {
scene: asset_server.load("models/scout_girl.glb#Scene0"),
..default()
});
});
// light
commands
.spawn_bundle(SpatialBundle::default())
.insert(LightFriends)
.insert(NoFrustumCulling)
.with_children(|children| {
let count = 6;
for i in 0..count {
@ -59,12 +73,26 @@ pub fn spawn_player(
},
..Default::default()
})
.insert(NoFrustumCulling)
.insert(PlayerLight { i: i as u8 + 1 })
.insert(Illumination { radius: 13.0 });
}
});
}
pub fn setup_scene_once_loaded(
animations: Res<PlayerAnimations>,
mut player: Query<&mut AnimationPlayer>,
mut done: Local<bool>,
) {
if !*done {
if let Ok(mut player) = player.get_single_mut() {
player.play(animations.idle.clone_weak()).repeat();
*done = true;
}
}
}
#[derive(Component)]
pub struct LightFriends;
pub fn move_light_friends(
@ -150,21 +178,51 @@ pub fn move_player(
mut query: Query<(&mut Transform, &Player)>,
input: Res<Input<KeyCode>>,
time: Res<Time>,
animations: Res<PlayerAnimations>,
mut animation_player: Query<&mut AnimationPlayer>,
mut is_walking: Local<bool>,
) {
let ds = time.delta_seconds() * 5.0;
let mut anim = if let Ok(player) = animation_player.get_single_mut() {
player
} else {
return;
};
for (mut transform, _) in query.iter_mut() {
let mut delta = Vec3::ZERO;
if input.pressed(KeyCode::W) {
transform.translation += Vec3::X * ds;
delta += Vec3::X;
}
if input.pressed(KeyCode::S) {
transform.translation -= Vec3::X * ds;
delta -= Vec3::X;
}
if input.pressed(KeyCode::A) {
transform.translation -= Vec3::Z * ds;
delta -= Vec3::Z;
}
if input.pressed(KeyCode::D) {
transform.translation += Vec3::Z * ds;
delta += Vec3::Z;
}
if !*is_walking && delta.length_squared() > 0.2 {
anim.play(animations.run.clone_weak()).repeat();
anim.set_speed(0.5);
*is_walking = true;
}
if *is_walking && delta.length_squared() < 0.2 {
anim.play(animations.idle.clone_weak()).repeat();
anim.set_speed(1.0);
*is_walking = false;
}
if delta.length_squared() > 0.2 {
let mut ct = *transform;
ct.look_at(transform.translation - delta, Vec3::Y);
let rot = transform.rotation.slerp(ct.rotation, ds * 2.0);
transform.rotation = rot;
}
transform.translation += delta * ds;
}
}