-
Notifications
You must be signed in to change notification settings - Fork 23
/
yaml.rs
63 lines (54 loc) · 1.6 KB
/
yaml.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
use bevy::prelude::*;
use bevy::reflect::TypePath;
use bevy_common_assets::yaml::YamlAssetPlugin;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
YamlAssetPlugin::<Level>::new(&["level.yaml"]),
))
.insert_resource(Msaa::Off)
.init_state::<AppState>()
.add_systems(Startup, setup)
.add_systems(Update, spawn_level.run_if(in_state(AppState::Loading)))
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let level = LevelHandle(asset_server.load("trees.level.yaml"));
commands.insert_resource(level);
let tree = ImageHandle(asset_server.load("tree.png"));
commands.insert_resource(tree);
commands.spawn(Camera2dBundle::default());
}
fn spawn_level(
mut commands: Commands,
level: Res<LevelHandle>,
tree: Res<ImageHandle>,
mut levels: ResMut<Assets<Level>>,
mut state: ResMut<NextState<AppState>>,
) {
if let Some(level) = levels.remove(level.0.id()) {
for position in level.positions {
commands.spawn(SpriteBundle {
transform: Transform::from_translation(position.into()),
texture: tree.0.clone(),
..default()
});
}
state.set(AppState::Level);
}
}
#[derive(serde::Deserialize, Asset, TypePath)]
struct Level {
positions: Vec<[f32; 3]>,
}
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
enum AppState {
#[default]
Loading,
Level,
}
#[derive(Resource)]
struct ImageHandle(Handle<Image>);
#[derive(Resource)]
struct LevelHandle(Handle<Level>);