-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple.rs
106 lines (99 loc) · 3.07 KB
/
simple.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
98
99
100
101
102
103
104
105
106
use bevy::prelude::*;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_touch_stick::{prelude::*, TouchStickUiKnob, TouchStickUiOutline};
/// Marker type for our touch stick
#[derive(Default, Reflect, Hash, Clone, PartialEq, Eq)]
struct MyStick;
fn main() {
App::new()
.insert_resource(ClearColor(Color::BLACK))
.add_plugins((
DefaultPlugins,
// add an inspector for easily changing settings at runtime
WorldInspectorPlugin::default(),
// add the plugin
TouchStickPlugin::<MyStick>::default(),
))
.add_systems(Startup, setup)
.add_systems(Update, move_player)
.run();
}
#[derive(Component)]
struct Player {
max_speed: f32,
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(0., 0., 5.0),
..default()
});
commands.spawn((
Player { max_speed: 50. },
SpriteBundle {
sprite: Sprite {
color: Color::ORANGE,
custom_size: Some(Vec2::splat(50.)),
..default()
},
..default()
},
));
// spawn a touch stick
commands
.spawn(TouchStickUiBundle::<MyStick> {
stick: TouchStick::<MyStick> {
radius: 75.0,
..default()
},
style: Style {
width: Val::Px(150.),
height: Val::Px(150.),
position_type: PositionType::Absolute,
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
top: Val::Auto,
bottom: Val::Vh(15.),
..default()
},
..default()
},
..default()
})
.with_children(|parent| {
parent.spawn((
TouchStickUiKnob,
ImageBundle {
image: asset_server.load("knob.png").into(),
style: Style {
width: Val::Px(75.),
height: Val::Px(75.),
..default()
},
..default()
},
));
parent.spawn((
TouchStickUiOutline,
ImageBundle {
image: asset_server.load("outline.png").into(),
style: Style {
width: Val::Px(150.),
height: Val::Px(150.),
..default()
},
..default()
},
));
});
}
fn move_player(
sticks: Query<&TouchStick<MyStick>>,
mut players: Query<(&mut Transform, &Player)>,
time: Res<Time>,
) {
let (mut player_transform, player) = players.single_mut();
let stick = sticks.single();
let move_delta = stick.value * player.max_speed * time.delta_seconds();
player_transform.translation += move_delta.extend(0.);
}