Basic mapgen with bevy_ecs_tilemap
This commit is contained in:
parent
43d7d3b4e9
commit
d289460ced
97
src/map.rs
97
src/map.rs
|
@ -3,7 +3,7 @@ use crate::{inspector::HideFromInspector, prelude::*};
|
||||||
// ecs::world::CommandQueue,
|
// ecs::world::CommandQueue,
|
||||||
// tasks::{AsyncComputeTaskPool, Task},
|
// tasks::{AsyncComputeTaskPool, Task},
|
||||||
// };
|
// };
|
||||||
// use bevy_ecs_tilemap::prelude::*;
|
use bevy_ecs_tilemap::prelude::*;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use rand_pcg::Pcg64;
|
use rand_pcg::Pcg64;
|
||||||
use rand_seeder::Seeder;
|
use rand_seeder::Seeder;
|
||||||
|
@ -14,60 +14,57 @@ pub struct Tilemap;
|
||||||
// #[derive(Component)]
|
// #[derive(Component)]
|
||||||
// struct MapGen(Task<CommandQueue>);
|
// struct MapGen(Task<CommandQueue>);
|
||||||
|
|
||||||
/// This worked but had performance issues, so I decided to go with bevy_ecs_tilemap
|
pub fn spawn(mut commands: Commands, assets: Res<AssetServer>) {
|
||||||
pub fn spawn_raw_tiles(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_pool = AsyncComputeTaskPool::get();
|
||||||
|
|
||||||
// let task = task_pool.spawn(async move {});
|
// 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 mut rng: Pcg64 = Seeder::from("default_seed").make_rng();
|
||||||
let map_size = UVec2::new(1024, 1024);
|
let size = TilemapSize::new(1024, 1024);
|
||||||
let tile_size = UVec2 { x: 16, y: 16 };
|
let tilemap = commands.spawn((Tilemap, Name::new("Tilemap"))).id();
|
||||||
commands
|
|
||||||
.spawn((
|
let mut storage = TileStorage::empty(size);
|
||||||
Tilemap,
|
// let mutex_storage = Mutex::new(storage);
|
||||||
Name::new("Tilemap"),
|
|
||||||
SpatialBundle {
|
for x in 0..size.x {
|
||||||
transform: Transform::from_xyz(0., 0., f32::MIN),
|
for y in 0..size.y {
|
||||||
..default()
|
let position = TilePos::new(x, y);
|
||||||
},
|
let texture_index = TileTextureIndex(if rng.gen_range(0..1000) > 925 {
|
||||||
))
|
rng.gen_range(0..(16 * 8))
|
||||||
.with_children(|tilemap| {
|
} else {
|
||||||
let layout = assets.add(TextureAtlasLayout::from_grid(
|
0
|
||||||
tile_size,
|
});
|
||||||
16,
|
let tile = commands
|
||||||
16,
|
.spawn((
|
||||||
Some(UVec2 { x: 0, y: 0 }),
|
HideFromInspector,
|
||||||
Some(UVec2 { x: 0, y: 0 }),
|
TileBundle {
|
||||||
));
|
position,
|
||||||
let u_center = (map_size * tile_size) / 2;
|
tilemap_id: TilemapId(tilemap),
|
||||||
let center = Vec2::new(u_center.x as f32, u_center.y as f32);
|
texture_index,
|
||||||
for x in 0..map_size.x {
|
..Default::default()
|
||||||
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;
|
.id();
|
||||||
let index = if rng.gen_range(0..1000) > 925 {
|
storage.set(&position, tile);
|
||||||
rng.gen_range(0..(16 * 8))
|
}
|
||||||
} else {
|
}
|
||||||
0
|
|
||||||
};
|
let texture = TilemapTexture::Single(assets.load("img/Tileset Grass.png"));
|
||||||
tilemap.spawn((
|
let tile_size = TilemapTileSize::new(16., 16.);
|
||||||
HideFromInspector,
|
let grid_size = TilemapGridSize::new(16., 16.);
|
||||||
SpriteBundle {
|
let map_type = TilemapType::Square;
|
||||||
texture: texture.clone(),
|
|
||||||
transform: Transform::from_xyz(pos.x as f32, pos.y as f32, 0.),
|
commands.entity(tilemap).insert(TilemapBundle {
|
||||||
..default()
|
grid_size,
|
||||||
},
|
map_type,
|
||||||
TextureAtlas {
|
size,
|
||||||
layout: layout.clone(),
|
storage,
|
||||||
index,
|
texture,
|
||||||
..default()
|
tile_size,
|
||||||
},
|
transform: get_tilemap_center_transform(&size, &grid_size, &map_type, f32::MIN),
|
||||||
));
|
..Default::default()
|
||||||
}
|
});
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn spawn_in_background(mut commands: Commands, assets: Res<AssetServer>) {
|
// pub fn spawn_in_background(mut commands: Commands, assets: Res<AssetServer>) {
|
||||||
|
|
|
@ -5,7 +5,7 @@ use crate::movement::{Heading, Mover, Speed, Velocity, YSortable};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use bevy::sprite::MaterialMesh2dBundle;
|
use bevy::sprite::MaterialMesh2dBundle;
|
||||||
|
|
||||||
const PLAYER_SPEED: f32 = 500.;
|
const PLAYER_SPEED: f32 = 200.;
|
||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct Player;
|
pub struct Player;
|
||||||
|
|
Loading…
Reference in a new issue