Upgrade to bevy 0.16
parent
9db0030511
commit
9e8912506e
File diff suppressed because it is too large
Load Diff
10
Cargo.toml
10
Cargo.toml
|
|
@ -6,10 +6,12 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = "0.15.1"
|
bevy = "0.16.0"
|
||||||
bevy-inspector-egui = "0.28.1"
|
bevy-inspector-egui = "0.31.0"
|
||||||
bevy_mod_debugdump = "0.12.1"
|
bevy_egui = "0.34.1"
|
||||||
bevy_rapier2d = "0.28.0"
|
bevy_mod_debugdump = "0.13.0"
|
||||||
|
bevy_prototype_lyon = "0.13.0"
|
||||||
|
# bevy_rapier2d = "0.29.0"
|
||||||
num-traits = "0.2.19"
|
num-traits = "0.2.19"
|
||||||
|
|
||||||
# Enable a small amount of optimization in debug mode
|
# Enable a small amount of optimization in debug mode
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,13 @@ impl Plugin for DebugPlugin {
|
||||||
|
|
||||||
app.insert_resource(DebugMode::on())
|
app.insert_resource(DebugMode::on())
|
||||||
.add_plugins((
|
.add_plugins((
|
||||||
|
// WorldInspector requires EguiPlugin plugin to be added before it
|
||||||
|
bevy_egui::EguiPlugin {
|
||||||
|
enable_multipass_for_primary_context: true,
|
||||||
|
},
|
||||||
bevy_inspector_egui::quick::WorldInspectorPlugin::new().run_if(is_debug_enabled),
|
bevy_inspector_egui::quick::WorldInspectorPlugin::new().run_if(is_debug_enabled),
|
||||||
bevy_rapier2d::prelude::RapierDebugRenderPlugin::default(),
|
// TODO: uncomment when rapier is updated for bevy 0.16
|
||||||
|
// bevy_rapier2d::prelude::RapierDebugRenderPlugin::default(),
|
||||||
))
|
))
|
||||||
.add_systems(Update, debug_toggle);
|
.add_systems(Update, debug_toggle);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
121
src/game.rs
121
src/game.rs
|
|
@ -1,118 +1,23 @@
|
||||||
use crate::{debug, game_setup};
|
use crate::{debug, game_setup, util};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_rapier2d::prelude::*;
|
// TODO: uncomment when rapier is updated for bevy 0.16
|
||||||
|
// use bevy_rapier2d::prelude::*;
|
||||||
|
|
||||||
|
mod prefab;
|
||||||
|
mod systems;
|
||||||
|
|
||||||
pub fn init(app: &mut App) {
|
pub fn init(app: &mut App) {
|
||||||
let app = app.add_plugins((
|
let app = app.add_plugins((
|
||||||
game_setup::GameSetupPlugin,
|
game_setup::GameSetupPlugin,
|
||||||
RapierPhysicsPlugin::<NoUserData>::default(),
|
util::UtilPlugin,
|
||||||
|
// TODO: uncomment when rapier is updated for bevy 0.16
|
||||||
|
// RapierPhysicsPlugin::<NoUserData>::default(),
|
||||||
debug::DebugPlugin,
|
debug::DebugPlugin,
|
||||||
));
|
));
|
||||||
|
|
||||||
app.add_systems(Startup, setup_2d)
|
// app.add_systems(Startup, systems::setup_demo_2d)
|
||||||
.add_systems(Update, demo_2d);
|
// .add_systems(Update, systems::demo_2d);
|
||||||
|
|
||||||
// app.add_systems(Startup, setup_3d)
|
app.add_systems(Startup, systems::setup_demo_3d)
|
||||||
// .add_systems(Update, demo_3d);
|
.add_systems(Update, systems::demo_3d);
|
||||||
}
|
|
||||||
|
|
||||||
fn setup_2d(mut commands: Commands) {
|
|
||||||
commands.spawn((
|
|
||||||
Name::from("Demo 2D camera"),
|
|
||||||
Camera2d,
|
|
||||||
Transform::from_xyz(0.0, 0.0, 10.0),
|
|
||||||
));
|
|
||||||
commands.spawn((
|
|
||||||
Name::from("Demo 2D sprite"),
|
|
||||||
Sprite::sized(Vec2::splat(256.0)),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn demo_2d(
|
|
||||||
mut camera_query: Query<(&mut Transform, &mut OrthographicProjection)>,
|
|
||||||
mut mouse_events: EventReader<bevy::input::mouse::MouseMotion>,
|
|
||||||
mut scroll_events: EventReader<bevy::input::mouse::MouseWheel>,
|
|
||||||
mouse_input: Res<ButtonInput<MouseButton>>,
|
|
||||||
) {
|
|
||||||
let raw_mouse_motion: Vec2 = mouse_events.read().map(|e| e.delta).sum();
|
|
||||||
let raw_scroll_motion: f32 = scroll_events
|
|
||||||
.read()
|
|
||||||
.map(|event| match event.unit {
|
|
||||||
bevy::input::mouse::MouseScrollUnit::Line => event.y * -0.1,
|
|
||||||
bevy::input::mouse::MouseScrollUnit::Pixel => event.y * -0.05,
|
|
||||||
})
|
|
||||||
.sum();
|
|
||||||
|
|
||||||
for (mut transform, mut projection) in camera_query.iter_mut() {
|
|
||||||
projection.scale += raw_scroll_motion * projection.scale;
|
|
||||||
let mouse_motion = raw_mouse_motion * projection.scale * Vec2::new(-1.0, 1.0);
|
|
||||||
if mouse_input.pressed(MouseButton::Middle) {
|
|
||||||
transform.translation += mouse_motion.extend(0.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn setup_3d(
|
|
||||||
mut commands: Commands,
|
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
||||||
) {
|
|
||||||
commands.spawn((
|
|
||||||
Name::from("Demo 3D camera"),
|
|
||||||
Camera3d::default(),
|
|
||||||
Transform::from_xyz(0.0, 0.0, 10.0),
|
|
||||||
PointLight::default(),
|
|
||||||
));
|
|
||||||
commands.spawn((
|
|
||||||
Name::from("Demo 3D cuboid"),
|
|
||||||
Mesh3d(meshes.add(Cuboid::from_length(1.0))),
|
|
||||||
MeshMaterial3d(materials.add(Color::from(bevy::color::palettes::css::ORANGE))),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn demo_3d(
|
|
||||||
mut camera_query: Query<&mut Transform, With<Camera3d>>,
|
|
||||||
mut mouse_events: EventReader<bevy::input::mouse::MouseMotion>,
|
|
||||||
mouse_input: Res<ButtonInput<MouseButton>>,
|
|
||||||
key_input: Res<ButtonInput<KeyCode>>,
|
|
||||||
time: Res<Time>,
|
|
||||||
) {
|
|
||||||
let raw_mouse_motion: Vec2 = mouse_events.read().map(|e| e.delta).sum();
|
|
||||||
|
|
||||||
if mouse_input.pressed(MouseButton::Right) {
|
|
||||||
let move_forward = key_input.pressed(KeyCode::KeyW);
|
|
||||||
let move_back = key_input.pressed(KeyCode::KeyS);
|
|
||||||
let move_left = key_input.pressed(KeyCode::KeyA);
|
|
||||||
let move_right = key_input.pressed(KeyCode::KeyD);
|
|
||||||
let move_up = key_input.pressed(KeyCode::Space) || key_input.pressed(KeyCode::KeyE);
|
|
||||||
let move_down = key_input.pressed(KeyCode::ControlLeft) || key_input.pressed(KeyCode::KeyQ);
|
|
||||||
|
|
||||||
let raw_movement = Vec3 {
|
|
||||||
x: match (move_right, move_left) {
|
|
||||||
(true, false) => 1.0,
|
|
||||||
(false, true) => -1.0,
|
|
||||||
_ => 0.0,
|
|
||||||
},
|
|
||||||
y: match (move_up, move_down) {
|
|
||||||
(true, false) => 1.0,
|
|
||||||
(false, true) => -1.0,
|
|
||||||
_ => 0.0,
|
|
||||||
},
|
|
||||||
z: match (move_back, move_forward) {
|
|
||||||
(true, false) => 1.0,
|
|
||||||
(false, true) => -1.0,
|
|
||||||
_ => 0.0,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
for mut transform in camera_query.iter_mut() {
|
|
||||||
let mouse_motion = raw_mouse_motion * Vec2::new(-1.0, -1.0) * 0.002;
|
|
||||||
transform.rotate_axis(Dir3::Y, mouse_motion.x);
|
|
||||||
transform.rotate_local_x(mouse_motion.y);
|
|
||||||
|
|
||||||
let local_movement = raw_movement * time.delta_secs() * 10.0;
|
|
||||||
let movement = transform.rotation * local_movement;
|
|
||||||
transform.translation += movement;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
use bevy::pbr::Atmosphere;
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, Component, Reflect)]
|
||||||
|
#[reflect(Component)]
|
||||||
|
#[require(
|
||||||
|
Name = Name::from("2D Camera"),
|
||||||
|
Camera2d,
|
||||||
|
Transform = Transform::from_xyz(0., 0., 10.),
|
||||||
|
)]
|
||||||
|
pub struct DemoCamera2d;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, Component, Reflect)]
|
||||||
|
#[reflect(Component)]
|
||||||
|
#[require(
|
||||||
|
Name = Name::from("3D Camera"),
|
||||||
|
// Atmosphere causes a panic if hdr is not true: https://github.com/bevyengine/bevy/issues/18959
|
||||||
|
Camera = Camera {
|
||||||
|
hdr: true,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
Camera3d,
|
||||||
|
Atmosphere = Atmosphere::EARTH,
|
||||||
|
Transform = Transform::from_xyz(1., 2., 10.).looking_at(Vec3::ZERO, Dir3::Y),
|
||||||
|
PointLight,
|
||||||
|
)]
|
||||||
|
pub struct DemoCamera3d;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
mod demo;
|
||||||
|
|
||||||
|
pub use demo::*;
|
||||||
|
|
@ -0,0 +1,136 @@
|
||||||
|
pub use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::game::prefab::{DemoCamera2d, DemoCamera3d};
|
||||||
|
|
||||||
|
pub fn setup_demo_2d(mut commands: Commands) {
|
||||||
|
commands.spawn((
|
||||||
|
Name::from("2D Demo Scene"),
|
||||||
|
Transform::default(),
|
||||||
|
Visibility::default(),
|
||||||
|
children![
|
||||||
|
DemoCamera2d,
|
||||||
|
(Name::from("2D sprite"), Sprite::sized(Vec2::splat(256.0)),)
|
||||||
|
],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn demo_2d(
|
||||||
|
mut camera_query: Query<(&mut Transform, &mut Projection)>,
|
||||||
|
mut mouse_events: EventReader<bevy::input::mouse::MouseMotion>,
|
||||||
|
mut scroll_events: EventReader<bevy::input::mouse::MouseWheel>,
|
||||||
|
mouse_input: Res<ButtonInput<MouseButton>>,
|
||||||
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||||
|
) {
|
||||||
|
let raw_mouse_motion: Vec2 = mouse_events.read().map(|e| e.delta).sum();
|
||||||
|
let raw_scroll_motion: f32 = scroll_events
|
||||||
|
.read()
|
||||||
|
.map(|event| match event.unit {
|
||||||
|
bevy::input::mouse::MouseScrollUnit::Line => event.y * -0.1,
|
||||||
|
bevy::input::mouse::MouseScrollUnit::Pixel => event.y * -0.05,
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
for (mut transform, mut projection) in camera_query.iter_mut() {
|
||||||
|
let projection = match projection.as_mut() {
|
||||||
|
Projection::Orthographic(projection) => projection,
|
||||||
|
_ => continue,
|
||||||
|
};
|
||||||
|
if keyboard_input.pressed(KeyCode::ControlLeft) {
|
||||||
|
projection.scale += raw_scroll_motion * projection.scale;
|
||||||
|
}
|
||||||
|
let mouse_motion = raw_mouse_motion * projection.scale * Vec2::new(-1.0, 1.0);
|
||||||
|
if mouse_input.pressed(MouseButton::Middle) {
|
||||||
|
transform.translation += mouse_motion.extend(0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn setup_demo_3d(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
|
) {
|
||||||
|
commands.spawn((
|
||||||
|
Name::from("3D Demo Scene"),
|
||||||
|
Transform::from_xyz(0., 300.0, 0.),
|
||||||
|
Visibility::default(),
|
||||||
|
children![
|
||||||
|
(
|
||||||
|
DemoCamera3d,
|
||||||
|
Transform::from_xyz(-6.225, 2.197, -10.470).looking_at(Vec3::ZERO, Dir3::Y),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Name::from("Cuboid"),
|
||||||
|
Transform::from_xyz(0., 0.5, 0.),
|
||||||
|
Mesh3d(meshes.add(Cuboid::from_length(1.0))),
|
||||||
|
MeshMaterial3d(materials.add(Color::from(bevy::color::palettes::css::GREY))),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Name::from("Floor"),
|
||||||
|
Transform::from_xyz(0., -0.25, 0.),
|
||||||
|
Mesh3d(meshes.add(Cuboid::from_size(Vec3::new(10., 0.5, 10.)))),
|
||||||
|
MeshMaterial3d(materials.add(Color::from(bevy::color::palettes::css::GREY))),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Name::from("Sun"),
|
||||||
|
Transform::from_rotation(Quat::from_euler(
|
||||||
|
EulerRot::default(),
|
||||||
|
f32::to_radians(160.0),
|
||||||
|
f32::to_radians(-20.0),
|
||||||
|
0.0,
|
||||||
|
)),
|
||||||
|
DirectionalLight {
|
||||||
|
shadows_enabled: true,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn demo_3d(
|
||||||
|
mut camera_query: Query<&mut Transform, With<Camera3d>>,
|
||||||
|
mut mouse_events: EventReader<bevy::input::mouse::MouseMotion>,
|
||||||
|
mouse_input: Res<ButtonInput<MouseButton>>,
|
||||||
|
key_input: Res<ButtonInput<KeyCode>>,
|
||||||
|
time: Res<Time>,
|
||||||
|
) {
|
||||||
|
let raw_mouse_motion: Vec2 = mouse_events.read().map(|e| e.delta).sum();
|
||||||
|
|
||||||
|
if mouse_input.pressed(MouseButton::Right) {
|
||||||
|
let move_forward = key_input.pressed(KeyCode::KeyW);
|
||||||
|
let move_back = key_input.pressed(KeyCode::KeyS);
|
||||||
|
let move_left = key_input.pressed(KeyCode::KeyA);
|
||||||
|
let move_right = key_input.pressed(KeyCode::KeyD);
|
||||||
|
let move_up = key_input.pressed(KeyCode::Space) || key_input.pressed(KeyCode::KeyE);
|
||||||
|
let move_down = key_input.pressed(KeyCode::ControlLeft) || key_input.pressed(KeyCode::KeyQ);
|
||||||
|
|
||||||
|
let raw_movement = Vec3 {
|
||||||
|
x: match (move_right, move_left) {
|
||||||
|
(true, false) => 1.0,
|
||||||
|
(false, true) => -1.0,
|
||||||
|
_ => 0.0,
|
||||||
|
},
|
||||||
|
y: match (move_up, move_down) {
|
||||||
|
(true, false) => 1.0,
|
||||||
|
(false, true) => -1.0,
|
||||||
|
_ => 0.0,
|
||||||
|
},
|
||||||
|
z: match (move_back, move_forward) {
|
||||||
|
(true, false) => 1.0,
|
||||||
|
(false, true) => -1.0,
|
||||||
|
_ => 0.0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
for mut transform in camera_query.iter_mut() {
|
||||||
|
let mouse_motion = raw_mouse_motion * Vec2::new(-1.0, -1.0) * 0.002;
|
||||||
|
transform.rotate_axis(Dir3::Y, mouse_motion.x);
|
||||||
|
transform.rotate_local_x(mouse_motion.y);
|
||||||
|
|
||||||
|
let local_movement = raw_movement * time.delta_secs() * 10.0;
|
||||||
|
let movement = transform.rotation * local_movement;
|
||||||
|
transform.translation += movement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,14 +4,18 @@ use core::{fmt, ops};
|
||||||
use std::{fs, path::Path};
|
use std::{fs, path::Path};
|
||||||
|
|
||||||
mod basis;
|
mod basis;
|
||||||
|
mod plugin;
|
||||||
mod transform_f64;
|
mod transform_f64;
|
||||||
|
mod types;
|
||||||
mod vector2;
|
mod vector2;
|
||||||
mod vector2_i32;
|
mod vector2_i32;
|
||||||
mod vector3;
|
mod vector3;
|
||||||
mod vector3_i32;
|
mod vector3_i32;
|
||||||
|
|
||||||
pub use basis::*;
|
pub use basis::*;
|
||||||
|
pub use plugin::*;
|
||||||
pub use transform_f64::*;
|
pub use transform_f64::*;
|
||||||
|
pub use types::*;
|
||||||
pub use vector2::*;
|
pub use vector2::*;
|
||||||
pub use vector2_i32::*;
|
pub use vector2_i32::*;
|
||||||
pub use vector3::*;
|
pub use vector3::*;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
pub struct UtilPlugin;
|
||||||
|
|
||||||
|
impl Plugin for UtilPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.register_type::<SpriteLoader>()
|
||||||
|
.add_systems(PostUpdate, load_sprite);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Automatically sets the `Sprite` components `image` to given asset path upon being added.
|
||||||
|
///
|
||||||
|
/// Can be used to set default sprite in required components without having access to AssetServer.
|
||||||
|
#[derive(Clone, Debug, Default, Component, Reflect)]
|
||||||
|
#[reflect(Component)]
|
||||||
|
#[require(Sprite)]
|
||||||
|
pub struct SpriteLoader(pub String);
|
||||||
|
|
||||||
|
impl SpriteLoader {
|
||||||
|
pub fn from(path: impl Into<String>) -> Self {
|
||||||
|
Self(path.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_sprite(
|
||||||
|
mut sprite_query: Query<(&mut Sprite, &SpriteLoader), Added<SpriteLoader>>,
|
||||||
|
assets: Res<AssetServer>,
|
||||||
|
) {
|
||||||
|
for (mut sprite, loader) in sprite_query.iter_mut() {
|
||||||
|
sprite.image = assets.load(&loader.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub type Seconds = f32;
|
||||||
|
pub type Kilograms = f32;
|
||||||
Loading…
Reference in New Issue