tilemap part 1
This commit is contained in:
+26
-28
@@ -1,16 +1,21 @@
|
||||
use crate::citizen::Citizen;
|
||||
use crate::item::{Item, ItemBundle};
|
||||
use crate::tile::{FloorTile, Tile, TileState};
|
||||
use crate::tiles::TilePrefab;
|
||||
use bevy::prelude::*;
|
||||
use rand::prelude::*;
|
||||
|
||||
pub const PIXEL_RATIO: f32 = 4.0;
|
||||
|
||||
pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
setup_tilemap(&mut commands, &asset_server);
|
||||
#[derive(Resource)]
|
||||
pub struct TileMap {
|
||||
pub tiles: Vec<Vec<Vec<Entity>>>,
|
||||
}
|
||||
|
||||
pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>, mut tilemap: ResMut<TileMap>) {
|
||||
setup_tilemap(&mut commands, &asset_server, tilemap);
|
||||
|
||||
for x in 0..25 {
|
||||
for y in 0..25 {
|
||||
for y in 0..250 {
|
||||
if random::<f32>() < 0.1 {
|
||||
commands.spawn(Citizen::new(
|
||||
&asset_server,
|
||||
@@ -21,9 +26,11 @@ pub fn setup_level(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>) {
|
||||
fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>, mut tilemap: ResMut<TileMap>) {
|
||||
for x in 0..25 {
|
||||
let mut row:Vec<Vec<Entity>> = Vec::new();
|
||||
for y in 0..25 {
|
||||
let mut column:Vec<Entity> = Vec::new();
|
||||
for z in 0..4 {
|
||||
let position = Vec3::new(
|
||||
x as f32 * 16. * PIXEL_RATIO,
|
||||
@@ -32,15 +39,21 @@ fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>) {
|
||||
);
|
||||
let mut noitem: bool = false;
|
||||
|
||||
let texture: Handle<Image>;
|
||||
let tile_entity: Entity;
|
||||
if random::<f32>() < 0.4 {
|
||||
texture = asset_server.load("grass.png");
|
||||
tile_entity = commands
|
||||
.spawn(TilePrefab::grass(position, asset_server))
|
||||
.id();
|
||||
} else if random::<f32>() < 0.2 {
|
||||
texture = asset_server.load("dirt.png");
|
||||
tile_entity = commands
|
||||
.spawn(TilePrefab::dirt(position, asset_server))
|
||||
.id();
|
||||
} else if random::<f32>() < 0.1 {
|
||||
texture = asset_server.load("rock.png");
|
||||
tile_entity = commands
|
||||
.spawn(TilePrefab::rock(position, asset_server))
|
||||
.id();
|
||||
} else {
|
||||
texture = asset_server.load("sky.png");
|
||||
tile_entity = commands.spawn(TilePrefab::air(position, asset_server)).id();
|
||||
noitem = true;
|
||||
};
|
||||
|
||||
@@ -82,30 +95,15 @@ fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>) {
|
||||
}
|
||||
}
|
||||
|
||||
let tile_entity = commands
|
||||
.spawn((
|
||||
Transform::from_translation(position).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
Sprite {
|
||||
image: texture,
|
||||
..Default::default()
|
||||
},
|
||||
FloorTile {
|
||||
walkable: random::<f32>() < 0.7,
|
||||
astar_weight: random::<u8>() % 10 + 1,
|
||||
},
|
||||
Tile { position: position },
|
||||
TileState {
|
||||
timer: Timer::from_seconds(1., TimerMode::Repeating),
|
||||
},
|
||||
))
|
||||
.id();
|
||||
|
||||
let mut child_entities = Vec::new();
|
||||
for item in items {
|
||||
child_entities.push(commands.spawn(item).id());
|
||||
}
|
||||
commands.entity(tile_entity).add_children(&child_entities);
|
||||
column.push(tile_entity);
|
||||
}
|
||||
row.push(column);
|
||||
}
|
||||
tilemap.tiles.push(row);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user