Add Bevy ECS Tilemap and initial map setup

This commit is contained in:
2025-11-18 17:19:52 +01:00
parent d13a7b4f96
commit fa4798910d
6 changed files with 73 additions and 2 deletions

11
Cargo.lock generated
View File

@@ -644,6 +644,16 @@ dependencies = [
"syn",
]
[[package]]
name = "bevy_ecs_tilemap"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a7176ff40586face39666fab2c3692c62bd5de67987164e6d8b316bc19e27d7"
dependencies = [
"bevy",
"log",
]
[[package]]
name = "bevy_encase_derive"
version = "0.17.2"
@@ -4556,6 +4566,7 @@ name = "war-in-tunnels"
version = "0.1.0"
dependencies = [
"bevy",
"bevy_ecs_tilemap",
]
[[package]]

View File

@@ -5,6 +5,7 @@ edition = "2024"
[dependencies]
bevy = "0.17.2"
bevy_ecs_tilemap = "0.17.0"
[profile.dev]
opt-level = 1

View File

BIN
assets/base_field.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

BIN
assets/dirt_field.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

View File

@@ -1,3 +1,62 @@
fn main() {
println!("Hello, world!");
use bevy::{prelude::*, window::WindowResolution};
use bevy_ecs_tilemap::prelude::*;
const MAP_SIZE: TilemapSize = TilemapSize { x: 50, y: 25 };
const TILE_SIZE: TilemapTileSize = TilemapTileSize { x: 25.0, y: 25.0 };
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2d);
let dirt_texture_handle: Handle<Image> = asset_server.load("dirt_field.png");
let base_texture_handle: Handle<Image> = asset_server.load("base_field.png");
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);
}
}
commands.entity(tilemap_entity).insert(TilemapBundle {
grid_size: TILE_SIZE.into(),
map_type: TilemapType::default(),
size: MAP_SIZE,
storage: tile_storage,
texture: TilemapTexture::Single(dirt_texture_handle),
tile_size: TILE_SIZE,
anchor: TilemapAnchor::Center,
..Default::default()
});
}
fn main() {
App::new()
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("War in Tunnels"),
resolution: WindowResolution::new(1250, 625)
.with_scale_factor_override(1.0),
..Default::default()
}),
..default()
})
.set(ImagePlugin::default_nearest()),
)
.add_plugins(TilemapPlugin)
.add_systems(Startup, startup)
// .add_systems(Update, helpers::camera::movement)
.run();
}