Initial tile push with item cycle
This commit is contained in:
+99
@@ -0,0 +1,99 @@
|
||||
use crate::citizen::Citizen;
|
||||
use crate::item::{Item, ItemBundle};
|
||||
use crate::tile::{FloorTile, Tile, TileState};
|
||||
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);
|
||||
|
||||
for x in 0..25 {
|
||||
for y in 0..25 {
|
||||
if random::<f32>() < 0.1 {
|
||||
commands.spawn(Citizen::new(
|
||||
&asset_server,
|
||||
Vec3::new(x as f32 * 64., y as f32 * 64., 1.),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_tilemap(commands: &mut Commands, asset_server: &Res<AssetServer>) {
|
||||
for x in 0..25 {
|
||||
for y in 0..25 {
|
||||
let position = Vec3::new(
|
||||
x as f32 * 16. * PIXEL_RATIO,
|
||||
y as f32 * 16. * PIXEL_RATIO,
|
||||
-0.5,
|
||||
);
|
||||
|
||||
let texture = if random::<f32>() < 0.5 {
|
||||
asset_server.load("grass.png")
|
||||
} else {
|
||||
asset_server.load("rock.png")
|
||||
};
|
||||
|
||||
let mut items: Vec<ItemBundle> = vec![];
|
||||
|
||||
if random::<f32>() < 0.1 {
|
||||
items.push(ItemBundle {
|
||||
transform: Transform::from_translation(Vec3::new(0., 0., 0.1)),
|
||||
sprite: Sprite {
|
||||
image: asset_server.load("beer.png"),
|
||||
..Default::default()
|
||||
},
|
||||
item: Item::new(),
|
||||
visibility: Visibility::Visible,
|
||||
});
|
||||
}
|
||||
if random::<f32>() < 0.1 {
|
||||
items.push(ItemBundle {
|
||||
transform: Transform::from_translation(Vec3::new(0., 0., 0.1)),
|
||||
sprite: Sprite {
|
||||
image: asset_server.load("chalice.png"),
|
||||
..Default::default()
|
||||
},
|
||||
item: Item::new(),
|
||||
visibility: Visibility::Visible,
|
||||
});
|
||||
}
|
||||
if random::<f32>() < 0.1 {
|
||||
items.push(ItemBundle {
|
||||
transform: Transform::from_translation(Vec3::new(0., 0., 0.1)),
|
||||
sprite: Sprite {
|
||||
image: asset_server.load("fish.png"),
|
||||
..Default::default()
|
||||
},
|
||||
item: Item::new(),
|
||||
visibility: Visibility::Visible,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
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(0.5, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user