-
Notifications
You must be signed in to change notification settings - Fork 24
/
toml.rs
61 lines (52 loc) · 1.53 KB
/
toml.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
use bevy::prelude::*;
use bevy::reflect::TypePath;
use bevy_common_assets::toml::TomlAssetPlugin;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
TomlAssetPlugin::<Level>::new(&["level.toml"]),
))
.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.toml"));
commands.insert_resource(level);
let tree = ImageHandle(asset_server.load("tree.png"));
commands.insert_resource(tree);
commands.spawn((Camera2d, Msaa::Off));
}
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((
Sprite::from_image(tree.0.clone()),
Transform::from_translation(position.into()),
));
}
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>);