Basic mapgen with bevy_ecs_tilemap

This commit is contained in:
Daniel Flanagan 2024-08-02 16:43:30 -05:00
parent 43d7d3b4e9
commit d289460ced
2 changed files with 48 additions and 51 deletions

View file

@ -3,7 +3,7 @@ use crate::{inspector::HideFromInspector, prelude::*};
// ecs::world::CommandQueue,
// tasks::{AsyncComputeTaskPool, Task},
// };
// use bevy_ecs_tilemap::prelude::*;
use bevy_ecs_tilemap::prelude::*;
use rand::prelude::*;
use rand_pcg::Pcg64;
use rand_seeder::Seeder;
@ -14,59 +14,56 @@ pub struct Tilemap;
// #[derive(Component)]
// struct MapGen(Task<CommandQueue>);
/// This worked but had performance issues, so I decided to go with bevy_ecs_tilemap
pub fn spawn_raw_tiles(mut commands: Commands, assets: Res<AssetServer>) {
pub fn spawn(mut commands: Commands, assets: Res<AssetServer>) {
// TODO: I'm pretty determined to not have this sieze up the game. Should work with a "loading" screen.
// let task_pool = AsyncComputeTaskPool::get();
// let task = task_pool.spawn(async move {});
let texture: Handle<Image> = assets.load("img/Tileset Grass.png");
let mut rng: Pcg64 = Seeder::from("default_seed").make_rng();
let map_size = UVec2::new(1024, 1024);
let tile_size = UVec2 { x: 16, y: 16 };
commands
.spawn((
Tilemap,
Name::new("Tilemap"),
SpatialBundle {
transform: Transform::from_xyz(0., 0., f32::MIN),
..default()
},
))
.with_children(|tilemap| {
let layout = assets.add(TextureAtlasLayout::from_grid(
tile_size,
16,
16,
Some(UVec2 { x: 0, y: 0 }),
Some(UVec2 { x: 0, y: 0 }),
));
let u_center = (map_size * tile_size) / 2;
let center = Vec2::new(u_center.x as f32, u_center.y as f32);
for x in 0..map_size.x {
for y in 0..map_size.y {
let u_pos = UVec2::new(x, y) * tile_size;
let pos = Vec2::new(u_pos.x as f32, u_pos.y as f32) - center;
let index = if rng.gen_range(0..1000) > 925 {
let size = TilemapSize::new(1024, 1024);
let tilemap = commands.spawn((Tilemap, Name::new("Tilemap"))).id();
let mut storage = TileStorage::empty(size);
// let mutex_storage = Mutex::new(storage);
for x in 0..size.x {
for y in 0..size.y {
let position = TilePos::new(x, y);
let texture_index = TileTextureIndex(if rng.gen_range(0..1000) > 925 {
rng.gen_range(0..(16 * 8))
} else {
0
};
tilemap.spawn((
});
let tile = commands
.spawn((
HideFromInspector,
SpriteBundle {
texture: texture.clone(),
transform: Transform::from_xyz(pos.x as f32, pos.y as f32, 0.),
..default()
TileBundle {
position,
tilemap_id: TilemapId(tilemap),
texture_index,
..Default::default()
},
TextureAtlas {
layout: layout.clone(),
index,
..default()
},
));
))
.id();
storage.set(&position, tile);
}
}
let texture = TilemapTexture::Single(assets.load("img/Tileset Grass.png"));
let tile_size = TilemapTileSize::new(16., 16.);
let grid_size = TilemapGridSize::new(16., 16.);
let map_type = TilemapType::Square;
commands.entity(tilemap).insert(TilemapBundle {
grid_size,
map_type,
size,
storage,
texture,
tile_size,
transform: get_tilemap_center_transform(&size, &grid_size, &map_type, f32::MIN),
..Default::default()
});
}

View file

@ -5,7 +5,7 @@ use crate::movement::{Heading, Mover, Speed, Velocity, YSortable};
use crate::prelude::*;
use bevy::sprite::MaterialMesh2dBundle;
const PLAYER_SPEED: f32 = 500.;
const PLAYER_SPEED: f32 = 200.;
#[derive(Component, Debug)]
pub struct Player;