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
//! This example shows the basic usage of `TilemapAnchor`.
use bevy::prelude::*;
use bevy_ecs_tiled::prelude::*;
mod helper;
#[derive(Component)]
struct AnchorLabel;
fn main() {
let mut app = App::new();
app
// 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.
.add_plugins(TiledPlugin::default())
// 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)
// Add the axis debug plugin so we can better visualize map anchor changes.
.add_plugins(TiledDebugAxisPlugin)
// Add our systems and run the app!
.add_systems(Startup, startup)
.add_systems(Update, change_anchor)
.run();
}
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Spawn a 2D camera (required by Bevy).
commands.spawn(Camera2d);
// Load a map then spawn it.
commands.spawn((
// Only the [`TiledMap`] component is actually required to spawn a map.
TiledMap(asset_server.load("maps/orthogonal/finite.tmx")),
// But you can add extra components to change the defaults settings and how
// your map is actually displayed.
TilemapAnchor::Center,
));
let font_size = 20.0;
commands
.spawn((
Text::new("[Space] Anchor: "),
TextFont {
font_size,
..default()
},
TextLayout::new_with_justify(JustifyText::Left),
AnchorLabel,
))
.with_children(|parent| {
parent.spawn((
TextSpan::new(format!("{:?}", TilemapAnchor::Center)),
TextFont {
font_size,
..default()
},
));
});
}
fn change_anchor(
mut anchor: Single<&mut TilemapAnchor, With<TiledMap>>,
label: Single<Entity, With<AnchorLabel>>,
mut writer: TextUiWriter,
key: Res<ButtonInput<KeyCode>>,
) {
if key.just_pressed(KeyCode::Space) {
let new_anchor = helper::anchor::rotate_right(&anchor);
*writer.text(*label, 1) = format!("{:?}", &new_anchor);
**anchor = new_anchor;
}
}