forked from StarArawn/bevy_ecs_tilemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexagon_column.rs
136 lines (121 loc) · 3.84 KB
/
hexagon_column.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::*;
mod helpers;
// Side length of a colored quadrant (in "number of tiles").
const QUADRANT_SIDE_LENGTH: u32 = 80;
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
let texture_handle: Handle<Image> = asset_server.load("flat_hex_tiles.png");
// In total, there will be `(QUADRANT_SIDE_LENGTH * 2) * (QUADRANT_SIDE_LENGTH * 2)` tiles.
let map_size = TilemapSize {
x: QUADRANT_SIDE_LENGTH * 2,
y: QUADRANT_SIDE_LENGTH * 2,
};
let quadrant_size = TilemapSize {
x: QUADRANT_SIDE_LENGTH,
y: QUADRANT_SIDE_LENGTH,
};
let mut tile_storage = TileStorage::empty(map_size);
let tilemap_entity = commands.spawn_empty().id();
let tilemap_id = TilemapId(tilemap_entity);
fill_tilemap_rect(
TileTextureIndex(0),
TilePos { x: 0, y: 0 },
quadrant_size,
tilemap_id,
&mut commands,
&mut tile_storage,
);
fill_tilemap_rect(
TileTextureIndex(1),
TilePos {
x: QUADRANT_SIDE_LENGTH,
y: 0,
},
quadrant_size,
tilemap_id,
&mut commands,
&mut tile_storage,
);
fill_tilemap_rect(
TileTextureIndex(2),
TilePos {
x: 0,
y: QUADRANT_SIDE_LENGTH,
},
quadrant_size,
tilemap_id,
&mut commands,
&mut tile_storage,
);
fill_tilemap_rect(
TileTextureIndex(3),
TilePos {
x: QUADRANT_SIDE_LENGTH,
y: QUADRANT_SIDE_LENGTH,
},
quadrant_size,
tilemap_id,
&mut commands,
&mut tile_storage,
);
let tile_size = TilemapTileSize { x: 17.0, y: 15.0 };
let grid_size = TilemapGridSize { x: 17.0, y: 15.0 };
let map_type = TilemapType::Hexagon(HexCoordSystem::Column);
commands.entity(tilemap_entity).insert(TilemapBundle {
grid_size,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
map_type,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
}
fn swap_mesh_type(
mut query: Query<(
&mut Transform,
&TilemapSize,
&TilemapGridSize,
&mut TilemapType,
)>,
keyboard_input: Res<ButtonInput<KeyCode>>,
) {
if keyboard_input.just_pressed(KeyCode::Space) {
for (mut transform, map_size, grid_size, mut map_type) in query.iter_mut() {
match *map_type {
TilemapType::Hexagon(HexCoordSystem::Column) => {
*map_type = TilemapType::Hexagon(HexCoordSystem::ColumnEven);
}
TilemapType::Hexagon(HexCoordSystem::ColumnEven) => {
*map_type = TilemapType::Hexagon(HexCoordSystem::ColumnOdd);
}
TilemapType::Hexagon(HexCoordSystem::ColumnOdd) => {
*map_type = TilemapType::Hexagon(HexCoordSystem::Column);
}
_ => {}
}
*transform = get_tilemap_center_transform(map_size, grid_size, &map_type, 0.0);
}
}
}
fn main() {
App::new()
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("Hexagon Column Example"),
..Default::default()
}),
..default()
})
.set(ImagePlugin::default_nearest()),
)
.add_plugins(TilemapPlugin)
.add_systems(Startup, startup)
.add_systems(Update, helpers::camera::movement)
.add_systems(Update, swap_mesh_type)
.run();
}