1 commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+6027
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "bevy_tuto"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.19"
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
use bevy::{input::keyboard, prelude::*};
|
||||
|
||||
#[derive(Component)]
|
||||
struct Vehicle {
|
||||
speed: f32,
|
||||
velocity: Vec3,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(Update, move_vehicle)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn setup(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
// lumière globale = ressource
|
||||
commands.insert_resource(GlobalAmbientLight {
|
||||
color: Color::WHITE,
|
||||
brightness: 500.0,
|
||||
affects_lightmapped_meshes: true,
|
||||
});
|
||||
// ajout caméra
|
||||
commands.spawn((
|
||||
Camera3d::default(),
|
||||
Transform::from_xyz(0.0, 10.0, 15.0).looking_at(Vec3::ZERO, Vec3::Y),
|
||||
));
|
||||
// ajout sol
|
||||
commands.spawn((
|
||||
Mesh3d(meshes.add(Plane3d::default().mesh().size(20.0, 20.0))),
|
||||
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
|
||||
Transform::default(),
|
||||
));
|
||||
// ajout véhicule
|
||||
commands.spawn((
|
||||
Vehicle {
|
||||
speed: 5.0,
|
||||
velocity: Vec3::ZERO,
|
||||
},
|
||||
Mesh3d(meshes.add(Cuboid::new(1.0, 0.5, 2.0))),
|
||||
MeshMaterial3d(materials.add(Color::srgb(0.8, 0.2, 0.2))),
|
||||
Transform::from_xyz(0.0, 0.25, 0.0),
|
||||
));
|
||||
}
|
||||
|
||||
fn move_vehicle(
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
time: Res<Time>,
|
||||
mut query: Query<(&mut Vehicle, &mut Transform)>,
|
||||
) {
|
||||
for (mut vehicle, mut transform) in &mut query {
|
||||
vehicle.velocity = Vec3::ZERO;
|
||||
if keyboard_input.pressed(KeyCode::ArrowUp) {
|
||||
vehicle.velocity.z -= vehicle.speed;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::ArrowDown) {
|
||||
vehicle.velocity.z += vehicle.speed;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::ArrowLeft) {
|
||||
vehicle.velocity.x -= vehicle.speed;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::ArrowRight) {
|
||||
vehicle.velocity.x += vehicle.speed;
|
||||
}
|
||||
transform.translation += vehicle.velocity * time.delta_seconds();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user