Skip to content

Commit

Permalink
feat: bevy 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
matoous committed Dec 13, 2024
1 parent 63eae2d commit 1fba16f
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 132 deletions.
55 changes: 28 additions & 27 deletions examples/3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,25 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(5.0, 5.0)),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..default()
});
commands.spawn((
Mesh3d(meshes.add(Plane3d::default().mesh().size(5.0, 5.0))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
));

// light
commands.spawn(PointLightBundle {
point_light: PointLight {
commands.spawn((
PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
Transform::from_xyz(4.0, 8.0, 4.0),
));

// camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
Camera3d::default(),
Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
Orbit::default(),
));

Expand All @@ -77,16 +73,21 @@ fn setup(
asset_server.load("spineboy/export/spineboy-pma.atlas"),
);
let skeleton_handle = skeletons.add(skeleton);
commands.spawn(SpineBundle {
skeleton: skeleton_handle.clone(),
transform: Transform::from_xyz(0., 0., 0.).with_scale(Vec3::ONE * 0.005),
settings: SpineSettings {
default_materials: false,
mesh_type: SpineMeshType::Mesh3D,
commands.spawn((
SpineLoader {
skeleton: skeleton_handle.clone(),
..Default::default()
},
..Default::default()
});
SpineBundle {
transform: Transform::from_xyz(0., 0., 0.).with_scale(Vec3::ONE * 0.005),
settings: SpineSettings {
default_materials: false,
mesh_type: SpineMeshType::Mesh3D,
..Default::default()
},
..Default::default()
},
));
}

fn on_spawn(
Expand All @@ -112,17 +113,17 @@ fn controls(
) {
let mut window = window_query.single_mut();
if mouse_buttons.just_pressed(MouseButton::Left) {
window.cursor.grab_mode = CursorGrabMode::Locked;
window.cursor.visible = false;
window.cursor_options.grab_mode = CursorGrabMode::Locked;
window.cursor_options.visible = false;
}
if keys.just_pressed(KeyCode::Escape) {
window.cursor.grab_mode = CursorGrabMode::None;
window.cursor.visible = true;
window.cursor_options.grab_mode = CursorGrabMode::None;
window.cursor_options.visible = true;
}

let mut mouse_movement = Vec2::ZERO;
for mouse_motion_event in mouse_motion_events.read() {
if window.cursor.grab_mode == CursorGrabMode::Locked {
if window.cursor_options.grab_mode == CursorGrabMode::Locked {
mouse_movement += mouse_motion_event.delta;
}
}
Expand Down
30 changes: 16 additions & 14 deletions examples/crossfades.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use bevy::prelude::*;
use bevy_spine::{
Crossfades, SkeletonController, SkeletonData, Spine, SpineBundle, SpinePlugin, SpineReadyEvent,
SpineSet, SpineSystem,
};
use bevy_spine::prelude::*;

fn main() {
App::new()
Expand All @@ -23,7 +20,7 @@ fn setup(
mut commands: Commands,
mut skeletons: ResMut<Assets<SkeletonData>>,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let skeleton = SkeletonData::new_from_json(
asset_server.load("spineboy/export/spineboy-pro.json"),
Expand All @@ -35,14 +32,19 @@ fn setup(
crossfades.add("idle", "walk", 0.5);
crossfades.add("walk", "idle", 0.5);

commands.spawn(SpineBundle {
skeleton: skeleton_handle.clone(),
crossfades,
transform: Transform::default()
.with_translation(Vec3::new(0., -200., 0.))
.with_scale(Vec3::ONE * 0.5),
..Default::default()
});
commands.spawn((
SpineLoader {
skeleton: skeleton_handle.clone(),
..Default::default()
},
SpineBundle {
crossfades,
transform: Transform::default()
.with_translation(Vec3::new(0., -200., 0.))
.with_scale(Vec3::ONE * 0.5),
..Default::default()
},
));
}

fn on_spawn(
Expand All @@ -68,7 +70,7 @@ fn crossfades(mut spine_query: Query<&mut Spine>, time: Res<Time>) {
.animation()
.name()
.to_owned();
if time.elapsed_seconds() % 2. > 1. {
if time.elapsed_secs() % 2. > 1. {
if current_animation != "walk" {
let _ = spine.animation_state.set_animation_by_name(0, "walk", true);
}
Expand Down
28 changes: 18 additions & 10 deletions examples/custom_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use bevy_spine::{
SpineMaterial, SpineMaterialInfo, SpineMaterialPlugin, DARK_COLOR_ATTRIBUTE,
DARK_COLOR_SHADER_POSITION,
},
SkeletonController, SkeletonData, Spine, SpineBundle, SpineDrawer, SpinePlugin,
SpineReadyEvent, SpineSet, SpineSettings,
prelude::*,
SpineDrawer,
};

fn main() {
Expand All @@ -37,7 +37,7 @@ fn setup(
mut commands: Commands,
mut skeletons: ResMut<Assets<SkeletonData>>,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let skeleton = SkeletonData::new_from_json(
asset_server.load("spineboy/export/spineboy-pro.json"),
Expand All @@ -46,16 +46,24 @@ fn setup(
let skeleton_handle = skeletons.add(skeleton);

// Spine with no custom materials
commands.spawn((SpineBundle {
skeleton: skeleton_handle.clone(),
transform: Transform::from_xyz(-230., -130., 0.).with_scale(Vec3::ONE * 0.375),
..Default::default()
},));
commands.spawn((
SpineLoader {
skeleton: skeleton_handle.clone(),
..Default::default()
},
SpineBundle {
transform: Transform::from_xyz(-230., -130., 0.).with_scale(Vec3::ONE * 0.375),
..Default::default()
},
));

// Spine with custom materials
commands.spawn((
SpineBundle {
SpineLoader {
skeleton: skeleton_handle.clone(),
..Default::default()
},
SpineBundle {
transform: Transform::from_xyz(230., -130., 0.).with_scale(Vec3::ONE * 0.375),
settings: SpineSettings {
default_materials: false,
Expand Down Expand Up @@ -141,7 +149,7 @@ impl SpineMaterial for MyMaterial {
if let Ok(spine) = params.my_spine_query.get(entity) {
let mut material = material.unwrap_or_default();
material.image = renderable_data.texture;
material.time = params.time.elapsed_seconds();
material.time = params.time.elapsed_secs();
if let Some(slot) = spine
.skeleton
.slot_at_index(renderable_data.slot_index.unwrap_or(9999))
Expand Down
58 changes: 29 additions & 29 deletions examples/events.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use bevy::prelude::*;
use bevy_spine::{
SkeletonController, SkeletonData, Spine, SpineBundle, SpineEvent, SpinePlugin, SpineReadyEvent,
SpineSet,
};
use bevy_spine::prelude::*;

fn main() {
App::new()
Expand All @@ -24,19 +21,24 @@ fn setup(
mut commands: Commands,
mut skeletons: ResMut<Assets<SkeletonData>>,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let skeleton = SkeletonData::new_from_json(
asset_server.load("spineboy/export/spineboy-pro.json"),
asset_server.load("spineboy/export/spineboy-pma.atlas"),
);
let skeleton_handle = skeletons.add(skeleton);

commands.spawn(SpineBundle {
skeleton: skeleton_handle.clone(),
transform: Transform::from_xyz(0., -200., 0.).with_scale(Vec3::ONE * 0.5),
..Default::default()
});
commands.spawn((
SpineLoader {
skeleton: skeleton_handle.clone(),
..Default::default()
},
SpineBundle {
transform: Transform::from_xyz(0., -200., 0.).with_scale(Vec3::ONE * 0.5),
..Default::default()
},
));
}

fn on_spawn(
Expand All @@ -61,19 +63,17 @@ fn on_spine_event(
for event in spine_events.read() {
if let SpineEvent::Event { name, .. } = event {
commands
.spawn(Text2dBundle {
text: Text::from_section(
name.as_str(),
TextStyle {
font: asset_server.load("FiraMono-Medium.ttf"),
font_size: 22.0,
color: Color::WHITE,
},
)
.with_justify(JustifyText::Center),
transform: Transform::from_xyz(0., -200., 1.),
..Default::default()
})
.spawn((
Text2d(name.clone()),
TextFont {
font: asset_server.load("FiraMono-Medium.ttf"),
font_size: 22.0,
..Default::default()
},
TextColor(Color::WHITE),
Transform::from_xyz(0., -200., 1.),
TextLayout::new_with_justify(JustifyText::Center),
))
.insert(Footstep);
}
}
Expand All @@ -83,15 +83,15 @@ fn on_spine_event(
struct Footstep;

fn footstep_update(
mut footstep_query: Query<(&mut Transform, &mut Text, Entity), With<Footstep>>,
mut footstep_query: Query<(&mut Transform, &mut TextColor, Entity), With<Footstep>>,
mut commands: Commands,
time: Res<Time>,
) {
for (mut transform, mut text, entity) in footstep_query.iter_mut() {
transform.translation.y += time.delta_seconds() * 70.;
let mut alpha = text.sections[0].style.color.alpha();
alpha = (alpha - time.delta_seconds() * 2.).clamp(0., 1.);
text.sections[0].style.color.set_alpha(alpha);
for (mut transform, mut text_color, entity) in footstep_query.iter_mut() {
transform.translation.y += time.delta_secs() * 70.;
let mut alpha = text_color.0.alpha();
alpha = (alpha - time.delta_secs() * 2.).clamp(0., 1.);
text_color.0.set_alpha(alpha);
if alpha == 0. {
commands.entity(entity).despawn();
}
Expand Down
18 changes: 11 additions & 7 deletions examples/ik.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_spine::{
SkeletonController, SkeletonData, Spine, SpineBone, SpineBundle, SpinePlugin, SpineReadyEvent,
SpineSet, SpineSync, SpineSyncSet,
};
use bevy_spine::prelude::*;

#[derive(Component)]
pub struct Crosshair;
Expand All @@ -26,7 +23,7 @@ fn setup(
mut commands: Commands,
mut skeletons: ResMut<Assets<SkeletonData>>,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let skeleton = SkeletonData::new_from_json(
asset_server.load("spineboy/export/spineboy-pro.json"),
Expand All @@ -35,9 +32,12 @@ fn setup(
let skeleton_handle = skeletons.add(skeleton);

commands.spawn((
SpineLoader {
skeleton: skeleton_handle.clone(),
..Default::default()
},
SpineBundle {
transform: Transform::from_xyz(-200., -200., 0.).with_scale(Vec3::splat(0.5)),
skeleton: skeleton_handle.clone(),
..Default::default()
},
SpineSync,
Expand Down Expand Up @@ -82,7 +82,11 @@ fn ik(
let window = window_query.single();
let cursor_position = window
.cursor_position()
.and_then(|cursor| camera.viewport_to_world(camera_global_transform, cursor))
.and_then(|cursor| {
camera
.viewport_to_world(camera_global_transform, cursor)
.ok()
})
.map(|ray| ray.origin.truncate())
.unwrap_or(Vec2::ZERO);

Expand Down
21 changes: 12 additions & 9 deletions examples/immediate_spawn.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! Demonstrates how to spawn a [`SpineBundle`] and use it in one frame.
use bevy::{app::AppExit, core::FrameCount, prelude::*};
use bevy_spine::{
SkeletonData, Spine, SpineBundle, SpinePlugin, SpineReadyEvent, SpineSet, SpineSystem,
};
use bevy_spine::prelude::*;

#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum ExampleSet {
Expand Down Expand Up @@ -40,7 +38,7 @@ fn setup(
mut skeletons: ResMut<Assets<SkeletonData>>,
mut demo_data: ResMut<DemoData>,
) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

let skeleton = SkeletonData::new_from_json(
asset_server.load("spineboy/export/spineboy-pro.json"),
Expand All @@ -58,11 +56,16 @@ fn spawn(
if !demo_data.spawned {
if let Some(skeleton) = skeletons.get(&demo_data.skeleton_handle) {
if skeleton.is_loaded() {
commands.spawn(SpineBundle {
skeleton: demo_data.skeleton_handle.clone(),
transform: Transform::from_xyz(0., -200., 0.).with_scale(Vec3::ONE * 0.5),
..Default::default()
});
commands.spawn((
SpineLoader {
skeleton: demo_data.skeleton_handle.clone(),
..Default::default()
},
SpineBundle {
transform: Transform::from_xyz(0., -200., 0.).with_scale(Vec3::ONE * 0.5),
..Default::default()
},
));
demo_data.spawned = true;
println!("spawned on frame: {}", frame_count.0);
}
Expand Down
Loading

0 comments on commit 1fba16f

Please sign in to comment.