-
Notifications
You must be signed in to change notification settings - Fork 23
/
xml.rs
74 lines (64 loc) · 1.86 KB
/
xml.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
use bevy::math::f32::Vec3;
use bevy::prelude::*;
use bevy::reflect::TypePath;
use bevy_common_assets::xml::XmlAssetPlugin;
fn main() {
App::new()
.add_plugins((DefaultPlugins, XmlAssetPlugin::<Level>::new(&["level.xml"])))
.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.xml"));
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(Vec3::new(
position.x, position.y, position.z,
)),
texture: tree.0.clone(),
..default()
});
}
state.set(AppState::Level);
}
}
#[derive(serde::Deserialize, Asset, TypePath)]
struct Level {
#[serde(rename = "Position")]
positions: Vec<Position>,
}
#[derive(serde::Deserialize)]
struct Position {
#[serde(rename = "@x")]
x: f32,
#[serde(rename = "@y")]
y: f32,
#[serde(rename = "@z")]
z: f32,
}
#[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>);