Skip to content

Instantly share code, notes, and snippets.

@B-Reif
Created May 4, 2021 00:50
Show Gist options
  • Save B-Reif/4bad92bceea904a59bf0b5286caef661 to your computer and use it in GitHub Desktop.
Save B-Reif/4bad92bceea904a59bf0b5286caef661 to your computer and use it in GitHub Desktop.
Chunk auto_spawn bug
use bevy::prelude::*;
pub fn setup_system(mut commands: Commands) {
println!("Spawning Camera bundle...");
commands
.spawn()
.insert_bundle(OrthographicCameraBundle::new_2d());
}
mod camera;
mod tiles;
use bevy::{prelude::*, window::WindowMode};
use bevy_tilemap::prelude::*;
fn main() {
App::build()
.insert_resource(WindowDescriptor {
title: "Square Tiles".to_string(),
width: (32. * 16.),
height: (18. * 16.),
vsync: false,
resizable: true,
mode: WindowMode::Windowed,
..Default::default()
})
.init_resource::<tiles::SpriteHandles>()
.add_plugins(DefaultPlugins)
.add_plugins(TilemapDefaultPlugins)
.add_startup_system(camera::setup_system.system())
.add_startup_system(tiles::setup_system.system())
.add_system_set(tiles::system_set())
.run()
}
use bevy::prelude::*;
use bevy_tilemap::prelude::*;
#[derive(Default, Clone)]
pub struct SpriteHandles {
handle: Handle<Texture>,
}
pub struct MyTilemap;
pub fn setup_system(mut sprite_handle: ResMut<SpriteHandles>, asset_server: Res<AssetServer>) {
sprite_handle.handle = asset_server.load("textures/tiles.png");
}
pub fn system_set() -> SystemSet {
SystemSet::new()
.label("tiles")
.with_system(load.system())
.with_system(build_world.system())
}
fn build_tilemap(atlas_handle: Handle<TextureAtlas>) -> Tilemap {
Tilemap::builder()
.auto_chunk()
.auto_spawn(16, 9)
.dimensions(2, 2)
.chunk_dimensions(16, 9, 1)
.texture_dimensions(16, 16)
.z_layers(3)
.texture_atlas(atlas_handle)
.finish()
.unwrap()
}
fn tilemap_components_from(tilemap: Tilemap) -> TilemapBundle {
TilemapBundle {
tilemap,
visible: Visible {
is_visible: true,
is_transparent: true,
},
transform: Default::default(),
global_transform: Default::default(),
}
}
fn load(
mut commands: Commands,
mut ev_asset: EventReader<AssetEvent<Texture>>,
sprite_handles: Res<SpriteHandles>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
for ev in ev_asset.iter() {
if let AssetEvent::Created { handle } = ev {
if *handle == sprite_handles.handle {
let handle = handle.clone_weak();
let texture_atlas = TextureAtlas::from_grid(handle, Vec2::new(16., 16.), 2, 1);
let atlas_handle = texture_atlases.add(texture_atlas);
let tilemap = build_tilemap(atlas_handle);
let tilemap_components = tilemap_components_from(tilemap);
commands
.spawn()
.insert_bundle(OrthographicCameraBundle::new_2d());
println!("Spawning Tilemap bundle...");
commands
.spawn()
.insert(MyTilemap)
.insert_bundle(tilemap_components)
.insert(Timer::from_seconds(0.075, true));
}
}
}
}
fn build_world(mut query: Query<&mut Tilemap, (Added<Tilemap>, With<MyTilemap>)>) {
for mut map in query.iter_mut() {
let chunks_width = map.width().unwrap();
let chunks_height = map.height().unwrap();
let tiles_width = (chunks_width * map.chunk_width()) as i32;
let tiles_height = (chunks_height * map.chunk_height()) as i32;
let mut tiles = Vec::new();
for y in 0..tiles_height {
for x in 0..tiles_width {
let y = y - tiles_height / 2;
let x = x - tiles_width / 2;
let sprite_index = 0;
let tile = Tile {
point: (x, y),
sprite_index,
..Default::default()
};
tiles.push(tile);
}
}
map.insert_tiles(tiles).unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment