-
Notifications
You must be signed in to change notification settings - Fork 23
/
csv.rs
71 lines (62 loc) · 1.88 KB
/
csv.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
use bevy::asset::RecursiveDependencyLoadState;
use bevy::prelude::*;
use bevy::reflect::TypePath;
use bevy_common_assets::csv::{CsvAssetPlugin, LoadedCsv};
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
CsvAssetPlugin::<TreePosition>::new(&["level.csv"]),
))
.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.csv"));
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>,
asset_server: Res<AssetServer>,
tree: Res<ImageHandle>,
positions: Res<Assets<TreePosition>>,
mut state: ResMut<NextState<AppState>>,
) {
if asset_server.get_recursive_dependency_load_state(&level.0)
== Some(RecursiveDependencyLoadState::Loaded)
{
for (_, position) in positions.iter() {
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, Debug)]
struct TreePosition {
x: f32,
y: f32,
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<LoadedCsv<TreePosition>>);