Add entity spawn on painter

This commit is contained in:
Isidor Nielsen 2022-03-09 21:40:23 +00:00 committed by Joshua Barretto
parent d05cb99007
commit e21f04d456
3 changed files with 27 additions and 4 deletions

View File

@ -44,7 +44,7 @@ fn main() -> Result {
CanvasInfo::with_mock_canvas_info(index.as_index_ref(), world.sim(), |canvas| {
for plot in site.plots() {
if let PlotKind::Dungeon(dungeon) = plot.kind() {
let (prim_tree, fills) = dungeon.render_collect(&site, canvas);
let (prim_tree, fills, _entities) = dungeon.render_collect(&site, canvas);
for (prim, fill) in fills {
let aabb = fill.get_bounds(&prim_tree, prim);

View File

@ -6,6 +6,7 @@ use crate::{
CanvasInfo,
};
use common::{
generation::EntityInfo,
store::{Id, Store},
terrain::{
structure::{Structure as PrefabStructure, StructureBlock},
@ -665,6 +666,7 @@ impl Fill {
pub struct Painter {
prims: RefCell<Store<Primitive>>,
fills: RefCell<Vec<(Id<Primitive>, Fill)>>,
entities: RefCell<Vec<EntityInfo>>,
render_area: Aabr<i32>,
}
@ -1035,7 +1037,15 @@ impl Painter {
}
}
/// The area that the canvas is currently rendering.
pub fn render_aabr(&self) -> Aabr<i32> { self.render_area }
/// Spawns an entity if it is in the render_aabr, otherwise does nothing.
pub fn spawn(&self, entity: EntityInfo) {
if self.render_area.contains_point(entity.pos.xy().as_()) {
self.entities.borrow_mut().push(entity)
}
}
}
#[derive(Copy, Clone)]
@ -1188,10 +1198,15 @@ pub trait Structure {
&self,
site: &Site,
canvas: &CanvasInfo,
) -> (Store<Primitive>, Vec<(Id<Primitive>, Fill)>) {
) -> (
Store<Primitive>,
Vec<(Id<Primitive>, Fill)>,
Vec<EntityInfo>,
) {
let painter = Painter {
prims: RefCell::new(Store::default()),
fills: RefCell::new(Vec::new()),
entities: RefCell::new(Vec::new()),
render_area: Aabr {
min: canvas.wpos,
max: canvas.wpos + TerrainChunkSize::RECT_SIZE.map(|e| e as i32),
@ -1199,7 +1214,11 @@ pub trait Structure {
};
self.render(site, &canvas.land(), &painter);
(painter.prims.into_inner(), painter.fills.into_inner())
(
painter.prims.into_inner(),
painter.fills.into_inner(),
painter.entities.into_inner(),
)
}
}
/// Extend a 2d AABR to a 3d AABB

View File

@ -984,7 +984,7 @@ impl Site {
let info = canvas.info();
for plot in plots_to_render {
let (prim_tree, fills) = match &self.plots[plot].kind {
let (prim_tree, fills, entities) = match &self.plots[plot].kind {
PlotKind::House(house) => house.render_collect(self, canvas),
PlotKind::Workshop(workshop) => workshop.render_collect(self, canvas),
PlotKind::Castle(castle) => castle.render_collect(self, canvas),
@ -994,6 +994,10 @@ impl Site {
_ => continue,
};
for entity in entities {
canvas.spawn(entity);
}
for (prim, fill) in fills {
for mut aabb in fill.get_bounds_disjoint(&prim_tree, prim) {
aabb.min = Vec2::max(aabb.min.xy(), chunk_aabr.min).with_z(aabb.min.z);