language swap to rust, using Bevy ECS, rotating colour swapping cube, movement needs translating
This commit is contained in:
77
src/main.rs
Normal file
77
src/main.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(Update, rotate_cube)
|
||||
.run();
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct RotatingCube {
|
||||
rotation_speed: f32,
|
||||
}
|
||||
|
||||
fn setup(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
// Camera
|
||||
commands.spawn((
|
||||
Camera3d::default(),
|
||||
Transform::from_xyz(10.0, 10.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
|
||||
));
|
||||
|
||||
// Cube
|
||||
commands.spawn((
|
||||
Mesh3d(meshes.add(Cuboid::new(2.0, 2.0, 2.0))),
|
||||
MeshMaterial3d(materials.add(Color::srgb(1.0, 0.0, 0.0))),
|
||||
Transform::from_xyz(0.0, 1.0, 0.0),
|
||||
RotatingCube {
|
||||
rotation_speed: 2.0,
|
||||
},
|
||||
));
|
||||
|
||||
// Light (DOESN'T WORK)
|
||||
commands.spawn((
|
||||
PointLight {
|
||||
intensity: 1000000.0,
|
||||
shadows_enabled: true,
|
||||
..default()
|
||||
},
|
||||
Transform::from_xyz(10.0, 10.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
|
||||
));
|
||||
|
||||
// Plane
|
||||
commands.spawn((
|
||||
Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(50.0)))),
|
||||
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
|
||||
));
|
||||
}
|
||||
|
||||
fn rotate_cube(
|
||||
mut query: Query<
|
||||
(
|
||||
&mut Transform,
|
||||
&RotatingCube,
|
||||
&MeshMaterial3d<StandardMaterial>,
|
||||
),
|
||||
Without<Camera3d>,
|
||||
>,
|
||||
time: Res<Time>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
for (mut transform, rotating, material_handle) in query.iter_mut() {
|
||||
transform.rotate_y(rotating.rotation_speed * time.delta_secs());
|
||||
|
||||
// Get mutable reference to the actual material
|
||||
if let Some(material) = materials.get_mut(&material_handle.0) {
|
||||
let blue = (time.elapsed_secs().sin() + 1.0) / 2.0;
|
||||
let red = 1.0 - blue;
|
||||
let color = Color::srgb(red, 0.0, blue); // cycling colours
|
||||
material.base_color = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user