-
Notifications
You must be signed in to change notification settings - Fork 6
/
tileset.rs
92 lines (82 loc) · 2.7 KB
/
tileset.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
80
81
82
83
84
85
86
87
88
89
90
91
92
//! This example demonstrates the most basic setup for loading a tileset
//!
//! Essentially, all we need to do is load the config file via the `AssetServer` and keep
//! a handle to the asset. That's it! From there, you can use the `Tilesets` system parameter
//! (or simply `Res<Assets<Tileset>>` if you prefer) to access the stored tile and texture atlas
//! data.
use bevy::prelude::*;
use bevy_tileset::prelude::*;
fn main() {
App::new()
// === Required === //
.add_plugins((DefaultPlugins, TilesetPlugin::default()))
// /== Required === //
.init_resource::<MyTileset>()
.add_systems(Startup, load_tileset)
.add_systems(Update, show_tileset)
.run();
}
#[derive(Resource, Default)]
struct MyTileset {
/// This stores the handle to our tileset so it doesn't get unloaded
handle: Option<Handle<Tileset>>,
}
/// Starts the tileset loading process
fn load_tileset(mut my_tileset: ResMut<MyTileset>, asset_server: Res<AssetServer>) {
my_tileset.handle = Some(asset_server.load("tilesets/my_tileset.ron"));
}
/// Shows the tileset
///
/// This uses the `Tilesets` system parameter. Internally it gets the `Res<Assets<Tileset>>`, but also provides
/// additional niceties (specifically fetching a tileset by name or ID).
fn show_tileset(
tilesets: Tilesets,
mut commands: Commands,
my_tileset: Res<MyTileset>,
mut has_ran: Local<bool>,
) {
if my_tileset.handle.is_none() || *has_ran || !tilesets.contains_name("My Awesome Tileset") {
return;
}
let handle = my_tileset.handle.as_ref().unwrap();
if let Some(_) = tilesets.get(handle) {
println!("Got tileset by handle! ({:?})", my_tileset.handle);
}
if let Some(tileset) = tilesets.get_by_id(&0) {
println!("Got tileset by ID! ({})", tileset.id());
}
if let Some(tileset) = tilesets.get_by_name("My Awesome Tileset") {
println!("Got tileset by name! ({})", tileset.name());
println!("{:#?}", tileset);
// === Display Tileset === //
let atlas = tileset.atlas();
let texture = tileset.texture().clone();
commands.spawn(Camera2dBundle::default());
commands.spawn(SpriteBundle {
texture,
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..Default::default()
});
// === Display Tile === //
if let Some((ref tile_index, ..)) = tileset.select_tile("Grass") {
match tile_index {
TileIndex::Standard(index) => {
// Do something standard
commands.spawn(SpriteSheetBundle {
transform: Transform {
translation: Vec3::new(08.0, -48.0, 0.0),
..Default::default()
},
sprite: TextureAtlasSprite::new(*index),
texture_atlas: atlas.clone(),
..Default::default()
});
},
TileIndex::Animated(start, end, speed) => {
// Do something ✨ animated ✨
},
}
}
*has_ran = true;
}
}