forked from StarArawn/bevy_ecs_tilemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.rs
104 lines (94 loc) · 3.67 KB
/
basic.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
use bevy::{prelude::*, render::texture::ImageSettings};
use bevy_ecs_tilemap::prelude::*;
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 };
// Create a tilemap entity a little early.
// We want this entity early because we need to tell each tile which tilemap entity
// it is associated with. This is done with the TilemapId component on each tile.
// Eventually, we will insert the `TilemapBundle` bundle on the entity, which
// will contain various necessary components, such as `TileStorage`.
let tilemap_entity = commands.spawn().id();
// To begin creating the map we will need a `TileStorage` component.
// This component is a grid of tile entities and is used to help keep track of individual
// tiles in the world. If you have multiple layers of tiles you would have a tilemap entity
// per layer, each with their own `TileStorage` component.
let mut tile_storage = TileStorage::empty(tilemap_size);
// Spawn the elements of the tilemap.
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()
});
}
fn swap_texture_or_hide(
asset_server: Res<AssetServer>,
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&mut TilemapTexture, &mut Visibility)>,
) {
if keyboard_input.just_pressed(KeyCode::Space) {
let texture_handle_a: Handle<Image> = asset_server.load("tiles.png");
let texture_handle_b: Handle<Image> = asset_server.load("tiles2.png");
for (mut tilemap_tex, _) in &mut query {
if &tilemap_tex.0 == &texture_handle_a {
tilemap_tex.0 = texture_handle_b.clone();
} else {
tilemap_tex.0 = texture_handle_a.clone();
}
}
}
if keyboard_input.just_pressed(KeyCode::H) {
for (_, mut visibility) in &mut query {
if visibility.is_visible {
visibility.is_visible = false;
} else {
visibility.is_visible = true;
}
}
}
}
fn main() {
App::new()
.insert_resource(WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from(
"Basic Example - Press Space to change Texture and H to show/hide tilemap.",
),
..Default::default()
})
.insert_resource(ImageSettings::default_nearest())
.add_plugins(DefaultPlugins)
.add_plugin(TilemapPlugin)
.add_startup_system(startup)
.add_system(helpers::camera::movement)
.add_system(swap_texture_or_hide)
.run();
}