forked from StarArawn/bevy_ecs_tilemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_tiles.rs
97 lines (85 loc) · 2.95 KB
/
remove_tiles.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
use bevy::{prelude::*, render::texture::ImageSettings};
use bevy_ecs_tilemap::prelude::*;
use rand::{thread_rng, Rng};
mod helpers;
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(Camera2dBundle::default());
let texture_handle: Handle<Image> = asset_server.load("tiles.png");
let tilemap_size = TilemapSize { x: 32, y: 32 };
let mut tile_storage = TileStorage::empty(tilemap_size);
let tilemap_entity = commands.spawn().id();
for x in 0..32u32 {
for y in 0..32u32 {
let tile_pos = TilePos { x, y };
let tile_entity = commands
.spawn()
.insert_bundle(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
..Default::default()
})
.id();
tile_storage.set(&tile_pos, Some(tile_entity));
}
}
let tile_size = TilemapTileSize { x: 16.0, y: 16.0 };
commands
.entity(tilemap_entity)
.insert_bundle(TilemapBundle {
grid_size: TilemapGridSize { x: 16.0, y: 16.0 },
size: tilemap_size,
storage: tile_storage,
texture: TilemapTexture(texture_handle),
tile_size,
transform: bevy_ecs_tilemap::helpers::get_centered_transform_2d(
&tilemap_size,
&tile_size,
0.0,
),
..Default::default()
})
.insert(LastUpdate::default());
}
#[derive(Default, Component)]
struct LastUpdate {
value: f64,
}
fn remove_tiles(
mut commands: Commands,
time: Res<Time>,
mut last_update_query: Query<(&mut LastUpdate, &mut TileStorage)>,
) {
let current_time = time.seconds_since_startup();
for (mut last_update, mut tile_storage) in last_update_query.iter_mut() {
// Remove a tile every half second.
if (current_time - last_update.value) > 0.1 {
let mut random = thread_rng();
let position = TilePos {
x: random.gen_range(0..32),
y: random.gen_range(0..32),
};
if let Some(tile_entity) = tile_storage.get(&position) {
commands.entity(tile_entity).despawn_recursive();
// Don't forget to remove tiles from the tile storage!
tile_storage.set(&position, None);
}
last_update.value = current_time;
}
}
}
fn main() {
App::new()
.insert_resource(WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from("Remove Tiles Example"),
..Default::default()
})
.insert_resource(ImageSettings::default_nearest())
.add_plugins(DefaultPlugins)
.add_plugin(TilemapPlugin)
.add_startup_system(startup)
.add_system(helpers::camera::movement)
.add_system(remove_tiles)
.run();
}