-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathanimation.rs
118 lines (104 loc) · 3.34 KB
/
animation.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
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
//! Simple example illustrating how to use updates to the tilemap for animation.
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
math::{uvec2, vec2, vec3},
prelude::*,
window::PresentMode,
};
use bevy_fast_tilemap::prelude::*;
#[path = "common/mouse_controls_camera.rs"]
mod mouse_controls_camera;
use mouse_controls_camera::MouseControlsCameraPlugin;
fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("Fast Tilemap example"),
resolution: (1820., 920.).into(),
// disable vsync so we can see the raw FPS speed
present_mode: PresentMode::Immediate,
..default()
}),
..default()
}),
LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin::default(),
MouseControlsCameraPlugin::default(),
FastTileMapPlugin::default(),
))
.add_systems(Startup, startup)
.add_systems(FixedUpdate, update_map)
// Performance-wise you can step this much faster but it'd require an epillepsy warning.
.insert_resource(Time::<Fixed>::from_seconds(0.2))
.run();
}
#[derive(Component)]
struct AnimationLayer;
fn startup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<Map>>,
) {
commands.spawn(Camera2dBundle::default());
let map = Map::builder(
uvec2(64, 64),
asset_server.load("pixel_tiles_16.png"),
vec2(16., 16.),
)
.build_and_initialize(|m| {
// Initialize using a closure
// Set all tiles in layer 0 to index 4
for y in 0..m.size().y {
for x in 0..m.size().x {
m.set(x, y, ((x + y) % 4 + 1) as u32);
}
}
});
commands.spawn(MapBundleManaged {
material: materials.add(map),
..default()
});
let map = Map::builder(
uvec2(64, 64),
asset_server.load("pixel_tiles_16.png"),
vec2(16., 16.),
)
.build();
let bundle = MapBundleManaged {
material: materials.add(map),
transform: Transform::default().with_translation(vec3(0., 0., 1.)),
..default()
};
commands.spawn(bundle).insert(AnimationLayer);
}
fn update_map(
mut map_materials: ResMut<Assets<Map>>,
maps: Query<&Handle<Map>, With<AnimationLayer>>,
) {
for map_handle in maps.iter() {
// Get the indexer into the map texture
let Some(map) = map_materials.get_mut(map_handle) else {
warn!("No map material");
continue;
};
let mut m = map.indexer_mut();
let k = 10;
let y_min = m.size().y / 2 - k;
let x_min = m.size().x / 2 - k;
let y_max = m.size().y / 2 + k + 1;
let x_max = m.size().x / 2 + k + 1;
for y in y_min..y_max {
for x in x_min..x_max {
// Tile index transitions, since our animation is ridiculously short,
// we can list them here explicitly
let t = match m.at(x, y) {
6 => 7,
7 => 8,
_ => 6,
};
m.set(x, y, t);
}
}
}
}