Skip to content

Commit

Permalink
Bevy ecs tilemap test
Browse files Browse the repository at this point in the history
  • Loading branch information
jgayfer committed Jul 6, 2024
1 parent c06faed commit 06e74f3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bevy = { version = "0.14", default-features = false, features = [
"png",
"x11"
] }
bevy_ecs_tilemap = { git = "https://github.com/StarArawn/bevy_ecs_tilemap" }

[lints.clippy]
type_complexity = "allow"
Expand Down
42 changes: 42 additions & 0 deletions examples/dungeon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::{color::palettes::css::YELLOW, prelude::*};
use bevy_ecs_tilemap::prelude::*;
use bevy_light_2d::prelude::*;

const TILE_INDEX: f32 = 0.0;
Expand All @@ -8,10 +9,12 @@ fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
TilemapPlugin,
Light2dPlugin,
))
.init_resource::<DungeonTileset>()
.init_resource::<CandleSpritesheet>()
.add_systems(Startup, startup)
.add_systems(Startup, (setup_camera, set_clear_color))
.add_systems(Startup, (setup_dungeon_tileset, spawn_tiles).chain())
.add_systems(Startup, (setup_candle_spritesheet, spawn_candles).chain())
Expand Down Expand Up @@ -250,3 +253,42 @@ const BOTTOM_FLOOR_B: usize = 33;

const FLOOR_A: usize = 22;
const FLOOR_B: usize = 23;

fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
let texture_handle: Handle<Image> = asset_server.load("dungeon_tiles.png");

let map_size = TilemapSize { x: 32, y: 32 };

let tilemap_entity = commands.spawn_empty().id();

let mut tile_storage = TileStorage::empty(map_size);

for x in 0..map_size.x {
for y in 0..map_size.y {
let tile_pos = TilePos { x, y };
let tile_entity = commands
.spawn(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(tilemap_entity),
..Default::default()
})
.id();
tile_storage.set(&tile_pos, tile_entity);
}
}

let tile_size = TilemapTileSize { x: 16.0, y: 16.0 };
let grid_size = tile_size.into();
let map_type = TilemapType::default();

commands.entity(tilemap_entity).insert(TilemapBundle {
grid_size,
map_type,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size,
transform: get_tilemap_center_transform(&map_size, &grid_size, &map_type, 0.0),
..Default::default()
});
}

0 comments on commit 06e74f3

Please sign in to comment.