forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
154 lines (147 loc) · 4.63 KB
/
lib.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use bevy::{input::touch::TouchPhase, prelude::*, window::WindowMode};
// the `bevy_main` proc_macro generates the required boilerplate for iOS and Android
#[bevy_main]
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
resizable: false,
mode: WindowMode::BorderlessFullscreen,
..default()
}),
..default()
}))
.add_startup_system(setup_scene)
.add_startup_system(setup_music)
.add_system(touch_camera)
.add_system(button_handler)
.run();
}
fn touch_camera(
windows: Query<&Window>,
mut touches: EventReader<TouchInput>,
mut camera: Query<&mut Transform, With<Camera3d>>,
mut last_position: Local<Option<Vec2>>,
) {
let window = windows.single();
for touch in touches.iter() {
if touch.phase == TouchPhase::Started {
*last_position = None;
}
if let Some(last_position) = *last_position {
let mut transform = camera.single_mut();
*transform = Transform::from_xyz(
transform.translation.x
+ (touch.position.x - last_position.x) / window.width() * 5.0,
transform.translation.y,
transform.translation.z
+ (touch.position.y - last_position.y) / window.height() * 5.0,
)
.looking_at(Vec3::ZERO, Vec3::Y);
}
*last_position = Some(touch.position);
}
}
/// set up a simple 3D scene
fn setup_scene(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.1, 0.2, 0.1).into()),
..default()
});
// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.5, 0.4, 0.3).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
});
// sphere
commands.spawn(PbrBundle {
mesh: meshes.add(
Mesh::try_from(shape::Icosphere {
subdivisions: 4,
radius: 0.5,
})
.unwrap(),
),
material: materials.add(Color::rgb(0.1, 0.4, 0.8).into()),
transform: Transform::from_xyz(1.5, 1.5, 1.5),
..default()
});
// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
point_light: PointLight {
intensity: 5000.0,
shadows_enabled: true,
..default()
},
..default()
});
// camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
// Test ui
commands
.spawn(ButtonBundle {
style: Style {
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(50.0),
right: Val::Px(50.0),
top: Val::Auto,
bottom: Val::Px(50.0),
},
..default()
},
..default()
})
.with_children(|b| {
b.spawn(
TextBundle::from_section(
"Test Button",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
color: Color::BLACK,
},
)
.with_text_alignment(TextAlignment::Center),
);
});
}
fn button_handler(
mut interaction_query: Query<
(&Interaction, &mut BackgroundColor),
(Changed<Interaction>, With<Button>),
>,
) {
for (interaction, mut color) in &mut interaction_query {
match *interaction {
Interaction::Clicked => {
*color = Color::BLUE.into();
}
Interaction::Hovered => {
*color = Color::GRAY.into();
}
Interaction::None => {
*color = Color::WHITE.into();
}
}
}
}
fn setup_music(asset_server: Res<AssetServer>, audio: Res<Audio>) {
let music = asset_server.load("sounds/Windless Slopes.ogg");
audio.play(music);
}