-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsequence.rs
79 lines (72 loc) · 1.92 KB
/
sequence.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
mod shared;
use bevy::{
asset::{LoadState, LoadedFolder},
prelude::*,
window::PrimaryWindow,
};
use bevy_scroller::*;
use shared::get_app;
use std::collections::VecDeque;
#[derive(Resource)]
pub struct ScrollerImages(Handle<LoadedFolder>);
#[derive(States, Default, Debug, Hash, Eq, PartialEq, Clone)]
pub enum AppStates {
#[default]
Load,
Run,
}
fn main() {
get_app("sequence".into())
.add_systems(Startup, startup)
.add_systems(Update, wait_for_load.run_if(in_state(AppStates::Load)))
.add_systems(OnEnter(AppStates::Run), run)
.init_state::<AppStates>()
.run();
}
pub fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands.insert_resource(ScrollerImages(asset_server.load_folder("gems")));
}
pub fn wait_for_load(
scroller_images: Res<ScrollerImages>,
asset_server: Res<AssetServer>,
mut next_state: ResMut<NextState<AppStates>>,
) {
if let Some(state) = asset_server.get_load_state(&scroller_images.0) {
if state == LoadState::Loaded {
*next_state = NextState(Some(AppStates::Run));
}
}
}
pub fn run(
mut commands: Commands,
windows: Query<&Window, With<PrimaryWindow>>,
asset_server: Res<AssetServer>,
images: Res<Assets<Image>>,
) {
let items = (1..=7)
.map(|i| {
let path = format!("gems/{i}.png");
let handle = asset_server.get_handle(path.clone()).unwrap();
let image = images.get(handle).unwrap();
SpriteScrollerItem {
path,
size: image.size().as_vec2(),
}
})
.collect::<VecDeque<SpriteScrollerItem>>();
let primary_window = windows.get_single().expect("no primary window");
commands.spawn((
ScrollerSize {
size: Vec2::new(primary_window.width(), 128.),
},
ScrollerBundle {
scroller: Scroller {
speed: 5.,
..default()
},
generator: SequenceSpriteGenerator { items },
..default()
},
));
}