-
Notifications
You must be signed in to change notification settings - Fork 40
/
parent_entity.rs
83 lines (70 loc) · 2.46 KB
/
parent_entity.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
use bevy::prelude::*;
use bevy_tiled_prototype::{MapRoot, TiledMapCenter};
// this example demonstrates moving the map mesh entities using
// the MapRoot marker on a passed-in parent element
const SCALE: f32 = 0.25;
#[derive(Debug, Default)]
struct MovementData {
transform: Transform,
}
fn main() {
App::build()
.insert_resource(MovementData::default())
.add_plugins(DefaultPlugins)
.add_plugin(bevy_tiled_prototype::TiledMapPlugin)
.add_system(bevy::input::system::exit_on_esc_system.system())
.add_system(process_input.system())
.add_system(move_parent_entity.system())
.add_startup_system(setup.system())
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// let's pass in a parent to append map tiles to
let parent = commands
.spawn_bundle((
Transform {
..Default::default()
},
GlobalTransform {
..Default::default()
},
))
.id();
commands.spawn_bundle(bevy_tiled_prototype::TiledMapBundle {
map_asset: asset_server.load("ortho-map.tmx"),
parent_option: Some(parent),
center: TiledMapCenter(true),
origin: Transform::from_scale(Vec3::new(4.0, 4.0, 1.0)),
..Default::default()
});
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
}
fn process_input(
mut movement_data: ResMut<MovementData>,
time: Res<Time>,
keyboard_input: Res<Input<KeyCode>>,
) {
let mut direction = Vec3::ZERO;
if keyboard_input.pressed(KeyCode::A) || keyboard_input.pressed(KeyCode::Left) {
direction -= Vec3::new(SCALE, 0.0, 0.0);
}
if keyboard_input.pressed(KeyCode::D) || keyboard_input.pressed(KeyCode::Right) {
direction += Vec3::new(SCALE, 0.0, 0.0);
}
if keyboard_input.pressed(KeyCode::W) || keyboard_input.pressed(KeyCode::Up) {
direction += Vec3::new(0.0, SCALE, 0.0);
}
if keyboard_input.pressed(KeyCode::S) || keyboard_input.pressed(KeyCode::Down) {
direction -= Vec3::new(0.0, SCALE, 0.0);
}
movement_data.transform.translation += time.delta_seconds() * direction * 1000.;
movement_data.transform.scale = Vec3::splat(SCALE);
}
fn move_parent_entity(
movement_data: Res<MovementData>,
mut query: Query<(&MapRoot, &mut Transform)>,
) {
for (_, mut transform) in query.iter_mut() {
transform.clone_from(&movement_data.transform);
}
}