forked from StarArawn/bevy_ecs_tilemap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_map.rs
More file actions
108 lines (93 loc) · 3.34 KB
/
random_map.rs
File metadata and controls
108 lines (93 loc) · 3.34 KB
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
use std::collections::HashSet;
use bevy::{
core::Time,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
render::camera::OrthographicCameraBundle,
window::WindowDescriptor,
};
use bevy_ecs_tilemap::prelude::*;
use rand::{thread_rng, Rng};
mod helpers;
fn startup(mut commands: Commands, asset_server: Res<AssetServer>, mut map_query: MapQuery) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
let texture_handle = asset_server.load("tiles.png");
// Create map entity and component:
let map_entity = commands.spawn().id();
let mut map = Map::new(0u16, map_entity);
let layer_settings = LayerSettings::new(
MapSize(10, 10),
ChunkSize(64, 64),
TileSize(16.0, 16.0),
TextureSize(96.0, 16.0),
);
let center = layer_settings.get_pixel_center();
// Chunk sizes of 64x64 seem optimal for meshing updates.
let (mut layer_builder, layer_entity) =
LayerBuilder::<TileBundle>::new(&mut commands, layer_settings, 0u16, 0u16);
map.add_layer(&mut commands, 0u16, layer_entity);
layer_builder.for_each_tiles_mut(|tile_entity, tile_data| {
// True here refers to tile visibility.
*tile_data = Some(TileBundle::default());
// Tile entity might not exist at this point so you'll need to create it.
if tile_entity.is_none() {
*tile_entity = Some(commands.spawn().id());
}
commands
.entity(tile_entity.unwrap())
.insert(LastUpdate::default());
});
map_query.build_layer(&mut commands, layer_builder, texture_handle);
// Spawn Map
// Required in order to use map_query to retrieve layers/tiles.
commands
.entity(map_entity)
.insert(map)
.insert(Transform::from_xyz(-center.x, -center.y, 0.0))
.insert(GlobalTransform::default());
}
#[derive(Default, Component)]
struct LastUpdate {
value: f64,
}
// In this example it's better not to use the default `MapQuery` SystemParam as
// it's faster to do it this way:
fn random(
time: ResMut<Time>,
mut query: Query<(&mut Tile, &TileParent, &mut LastUpdate)>,
mut chunk_query: Query<&mut Chunk>,
) {
let current_time = time.seconds_since_startup();
let mut random = thread_rng();
let mut chunks = HashSet::new();
for (mut tile, tile_parent, mut last_update) in query.iter_mut() {
if (current_time - last_update.value) > 0.05 {
tile.texture_index = random.gen_range(0..6);
last_update.value = current_time;
chunks.insert(tile_parent.chunk);
}
}
for chunk_entity in chunks.drain() {
if let Ok(mut chunk) = chunk_query.get_mut(chunk_entity) {
chunk.needs_remesh = true;
}
}
}
fn main() {
App::new()
.insert_resource(WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from("Random Map Example"),
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(TilemapPlugin)
.add_startup_system(startup)
.add_system(random)
.add_system(helpers::camera::movement)
.add_system(helpers::texture::set_texture_filters_to_nearest)
.run();
}