forked from StarArawn/bevy_ecs_tilemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexagon_column.rs
102 lines (90 loc) · 3.14 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
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("flat_hex_tiles.png");
let tilemap_size = TilemapSize { x: 128, y: 128 };
let mut tile_storage = TileStorage::empty(tilemap_size);
let tilemap_entity = commands.spawn().id();
let tilemap_id = TilemapId(tilemap_entity);
bevy_ecs_tilemap::helpers::fill_tilemap_rect(
TileTexture(0),
TilePos { x: 0, y: 0 },
TilemapSize { x: 128, y: 128 },
tilemap_id,
&mut commands,
&mut tile_storage,
);
bevy_ecs_tilemap::helpers::fill_tilemap_rect(
TileTexture(1),
TilePos { x: 64, y: 0 },
TilemapSize { x: 128, y: 64 },
tilemap_id,
&mut commands,
&mut tile_storage,
);
bevy_ecs_tilemap::helpers::fill_tilemap_rect(
TileTexture(2),
TilePos { x: 0, y: 64 },
TilemapSize { x: 64, y: 128 },
tilemap_id,
&mut commands,
&mut tile_storage,
);
bevy_ecs_tilemap::helpers::fill_tilemap_rect(
TileTexture(3),
TilePos { x: 64, y: 64 },
TilemapSize { x: 128, y: 128 },
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 };
commands
.entity(tilemap_entity)
.insert_bundle(TilemapBundle {
grid_size,
size: tilemap_size,
storage: tile_storage,
texture: TilemapTexture(texture_handle),
tile_size,
mesh_type: TilemapMeshType::Hexagon(HexType::Column),
..Default::default()
});
}
fn swap_mesh_type(mut query: Query<&mut TilemapMeshType>, keyboard_input: Res<Input<KeyCode>>) {
if keyboard_input.just_pressed(KeyCode::Space) {
for mut tilemap_mesh_type in query.iter_mut() {
match *tilemap_mesh_type {
TilemapMeshType::Hexagon(HexType::Column) => {
*tilemap_mesh_type = TilemapMeshType::Hexagon(HexType::ColumnEven);
}
TilemapMeshType::Hexagon(HexType::ColumnEven) => {
*tilemap_mesh_type = TilemapMeshType::Hexagon(HexType::ColumnOdd);
}
TilemapMeshType::Hexagon(HexType::ColumnOdd) => {
*tilemap_mesh_type = TilemapMeshType::Hexagon(HexType::Column);
}
_ => {}
}
}
}
}
fn main() {
App::new()
.insert_resource(WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from("Hexagon Column 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(swap_mesh_type)
.run();
}