-
Notifications
You must be signed in to change notification settings - Fork 7
/
ik.rs
97 lines (89 loc) · 3.22 KB
/
ik.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_spine::{
SkeletonController, SkeletonData, Spine, SpineBone, SpineBundle, SpinePlugin, SpineReadyEvent,
SpineSet, SpineSync, SpineSyncSet,
};
#[derive(Component)]
pub struct Crosshair;
fn main() {
App::new()
.add_plugins((DefaultPlugins, SpinePlugin))
.add_systems(Startup, setup)
.add_systems(
Update,
(
on_spawn.in_set(SpineSet::OnReady),
ik.in_set(SpineSyncSet::DuringSync),
),
)
.run();
}
fn setup(
asset_server: Res<AssetServer>,
mut commands: Commands,
mut skeletons: ResMut<Assets<SkeletonData>>,
) {
commands.spawn(Camera2dBundle::default());
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 {
transform: Transform::from_xyz(-200., -200., 0.).with_scale(Vec3::splat(0.5)),
skeleton: skeleton_handle.clone(),
..Default::default()
},
SpineSync,
));
}
fn on_spawn(
mut spine_ready_event: EventReader<SpineReadyEvent>,
mut spine_query: Query<&mut Spine>,
mut commands: Commands,
) {
for event in spine_ready_event.read() {
if let Ok(mut spine) = spine_query.get_mut(event.entity) {
let Spine(SkeletonController {
skeleton,
animation_state,
..
}) = spine.as_mut();
skeleton.set_scale(Vec2::splat(1.));
let _ = animation_state.set_animation_by_name(0, "run", true);
let _ = animation_state.set_animation_by_name(1, "aim", true);
let _ = animation_state.set_animation_by_name(2, "shoot", true);
if let Some(mut crosshair_entity) = event
.bones
.get("crosshair")
.and_then(|crosshair_entity| commands.get_entity(*crosshair_entity))
{
crosshair_entity.insert(Crosshair);
}
}
}
}
fn ik(
mut crosshair_query: Query<(&mut Transform, &SpineBone), With<Crosshair>>,
window_query: Query<&Window, With<PrimaryWindow>>,
camera_query: Query<(Entity, &Camera)>,
global_transform_query: Query<&GlobalTransform>,
) {
let (camera_entity, camera) = camera_query.single();
let camera_global_transform = global_transform_query.get(camera_entity).unwrap();
let window = window_query.single();
let cursor_position = window
.cursor_position()
.and_then(|cursor| camera.viewport_to_world(camera_global_transform, cursor))
.map(|ray| ray.origin.truncate())
.unwrap_or(Vec2::ZERO);
if let Ok((mut crosshair_transform, crosshair_bone)) = crosshair_query.get_single_mut() {
let parent_global_transform = global_transform_query
.get(crosshair_bone.parent.as_ref().unwrap().entity)
.unwrap();
crosshair_transform.translation = (parent_global_transform.compute_matrix().inverse()
* Vec4::new(cursor_position.x, cursor_position.y, 0., 1.))
.truncate();
}
}