From e21f04d45615ff7f4b13e97acf1624481dd6a822 Mon Sep 17 00:00:00 2001 From: Isidor Nielsen Date: Wed, 9 Mar 2022 21:40:23 +0000 Subject: [PATCH] Add entity spawn on painter --- world/examples/dungeon_voxel_export.rs | 2 +- world/src/site2/gen.rs | 23 +++++++++++++++++++++-- world/src/site2/mod.rs | 6 +++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/world/examples/dungeon_voxel_export.rs b/world/examples/dungeon_voxel_export.rs index 1a83274038..0e0a4c4959 100644 --- a/world/examples/dungeon_voxel_export.rs +++ b/world/examples/dungeon_voxel_export.rs @@ -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); diff --git a/world/src/site2/gen.rs b/world/src/site2/gen.rs index b835f16745..a89f624e6a 100644 --- a/world/src/site2/gen.rs +++ b/world/src/site2/gen.rs @@ -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>, fills: RefCell, Fill)>>, + entities: RefCell>, render_area: Aabr, } @@ -1035,7 +1037,15 @@ impl Painter { } } + /// The area that the canvas is currently rendering. pub fn render_aabr(&self) -> Aabr { 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, Vec<(Id, Fill)>) { + ) -> ( + Store, + Vec<(Id, Fill)>, + Vec, + ) { 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 diff --git a/world/src/site2/mod.rs b/world/src/site2/mod.rs index d820754ef5..a4a340728e 100644 --- a/world/src/site2/mod.rs +++ b/world/src/site2/mod.rs @@ -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);