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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! This example shows how to map custom tiles and objects properties from Tiled to Bevy Components.
use std::env;
use bevy::prelude::*;
use bevy_ecs_tiled::prelude::*;
mod helper;
fn main() {
// Use a custom file path to export registered types in Tiled format
let mut path = env::current_dir().unwrap();
path.push("examples");
path.push("my_tiled_export_file.json");
App::new()
// Bevy default plugins: prevent blur effect by changing default sampling
.add_plugins(DefaultPlugins.build().set(ImagePlugin::default_nearest()))
// Add bevy_ecs_tiled plugin: bevy_ecs_tilemap::TilemapPlugin will
// be automatically added as well if it's not already done
// For demonstration purpose, provide a custom path where to export registered types
.add_plugins(TiledPlugin(TiledPluginConfig {
// Note: if you set this setting to `None`
// properties won't be exported anymore but
// you will still be able to load them from the map
tiled_types_export_file: Some(path),
tiled_types_filter: TiledFilter::from(
RegexSet::new([
r"^properties_basic::.*",
r"^bevy_text::text2d::Text2d$",
r"^bevy_text::text::TextColor$",
r"^bevy_ecs::name::Name$",
])
.unwrap(),
),
}))
// Examples helper plugins, such as the logic to pan and zoom the camera
// This should not be used directly in your game (but you can always have a look)
.add_plugins(helper::HelperPlugin)
// We need to register all the custom types we want to use
.register_type::<BiomeInfos>()
.register_type::<SpawnType>()
.register_type::<ResourceType>()
// Add our systems and run the app!
.add_systems(Startup, startup)
.add_systems(Update, display_custom_tiles)
.add_systems(Update, display_objects)
.run();
}
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2d);
commands.spawn(TiledMap(
asset_server.load("maps/hexagonal/finite_pointy_top_odd.tmx"),
));
}
// You just have to define your Components and make sure they are properly registered and reflected.
// They will be exported in the Tiled .json file so they can be imported then used from Tiled.
// Next time you load your map, they will be automatically added as components on tiles / objects / layers entities
#[derive(Component, Default, Debug, Reflect)]
#[reflect(Component, Default)]
struct BiomeInfos {
ty: BiomeType,
pos: Vec2,
block_line_of_sight: bool,
}
#[derive(Default, Reflect, Debug)]
#[reflect(Default)]
enum BiomeType {
#[default]
Unknown,
Plain,
Desert,
Forest,
Mountain,
Water,
}
#[derive(Component, Default, Debug, Reflect)]
#[reflect(Component, Default)]
enum SpawnType {
#[default]
Unknown,
Player {
color: Color,
id: u32,
other_obj: Option<Entity>,
},
Enemy(Color),
Friendly,
}
#[derive(Component, Default, Debug, Reflect)]
#[reflect(Component, Default)]
enum ResourceType {
#[default]
Unknown,
Wheat,
Strawberry,
Wood,
Copper,
Gold,
}
// Marker component so we only print each entity once
#[derive(Component)]
struct DoNotPrint;
#[allow(clippy::type_complexity)]
fn display_custom_tiles(
mut commands: Commands,
q_tile: Query<
(Entity, &TilePos, Option<&BiomeInfos>, Option<&ResourceType>),
Without<DoNotPrint>,
>,
) {
for (entity, position, biome_infos, resource_type) in q_tile.iter() {
if let Some(i) = biome_infos {
// Only print the first tile to avoid flooding the console
info_once!("Found BiomeInfos [{:?} @ {:?}]", i, position);
}
if let Some(i) = resource_type {
// Only print the first tile to avoid flooding the console
info_once!("Found ResourceType [{:?} @ {:?}]", i, position);
}
commands.entity(entity).insert(DoNotPrint);
}
}
fn display_objects(
mut commands: Commands,
q_object: Query<(Entity, &Transform, &SpawnType), Without<DoNotPrint>>,
) {
for (entity, transform, spawn_type) in q_object.iter() {
info!(
"Found SpawnType [{:?} @ {:?}]",
spawn_type, transform.translation
);
commands.entity(entity).insert(DoNotPrint);
}
}