4 Commits

6 changed files with 134 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,123 @@
fn main() {
println!("Hello, world!");
use bevy::{prelude::*, window::WindowResolution};
use bevy_ecs_tilemap::prelude::*;
const TILEMAP_WIDTH: u32 = 50;
const TILEMAP_HEIGHT: u32 = 25;
const TILE_WIDTH: f32 = 25.0;
const TILE_HEIGHT: f32 = 25.0;
const MAP_SIZE: TilemapSize = TilemapSize {
x: TILEMAP_WIDTH,
y: TILEMAP_HEIGHT,
};
const TILE_SIZE: TilemapTileSize = TilemapTileSize {
x: TILE_WIDTH,
y: TILE_HEIGHT,
};
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 base_tilemap_entity = commands.spawn_empty().id();
let mut tile_storage = TileStorage::empty(MAP_SIZE);
let mut base_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);
}
}
for x in 0..3 {
for y in 11..14 {
if x < MAP_SIZE.x && y < MAP_SIZE.y {
let tile_pos = TilePos { x, y };
let tile_entity = commands
.spawn(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(base_tilemap_entity),
..Default::default()
})
.id();
base_tile_storage.set(&tile_pos, tile_entity);
}
}
}
for x in 47..50 {
for y in 11..14 {
if x < MAP_SIZE.x && y < MAP_SIZE.y {
let tile_pos = TilePos { x, y };
let tile_entity = commands
.spawn(TileBundle {
position: tile_pos,
tilemap_id: TilemapId(base_tilemap_entity),
..Default::default()
})
.id();
base_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,
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
..Default::default()
});
commands.entity(base_tilemap_entity).insert(TilemapBundle {
grid_size: TILE_SIZE.into(),
map_type: TilemapType::default(),
size: MAP_SIZE,
storage: base_tile_storage,
texture: TilemapTexture::Single(base_texture_handle),
tile_size: TILE_SIZE,
anchor: TilemapAnchor::Center,
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
..Default::default()
});
}
fn main() {
App::new()
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("War in Tunnels"),
resolution: WindowResolution::new(
TILEMAP_WIDTH * TILE_WIDTH as u32,
TILEMAP_HEIGHT * TILE_HEIGHT as u32,
)
.with_scale_factor_override(1.0),
..Default::default()
}),
..default()
})
.set(ImagePlugin::default_nearest()),
)
.add_plugins(TilemapPlugin)
.add_systems(Startup, startup)
.run();
}