feo_oop_engine/scene/
mod.rs1pub mod game_object;
6
7use std::any::Any;
8
9use vulkano::{swapchain::SwapchainAcquireFuture, sync::GpuFuture};
10use winit::window::Window;
11
12use crate::graphics::{draw_pass_manager::DrawPassManager, frame_system::FrameSystem, lighting_pass_manager::LightingPassManager};
13
14use game_object::camera::Camera;
15
16use {
17 crate::{
18 scene::{
19 game_object::GameObject,
20 },
21 registration::{
22 relation::{Parent, ParentWrapper},
23 named::Named
24 },
25 scripting::executor::Spawner,
26 components::{
27 triangle_mesh::TriangleMesh
28 },
29 event::UserEvent,
30 graphics::{
31 Drawable,
32 },
33 },
34 feo_math::{
35 linear_algebra::matrix4::Matrix4,
36 utils::space::Space
37 },
38 std::{
39 mem,
40 sync::{Arc, RwLock}
41 },
42 winit::event::Event
43};
44
45#[derive(Clone, Parent, Drawable, Debug)]
47pub struct Scene {
48 pub worldspace: Space,
49 pub children: Vec<Arc<RwLock<dyn GameObject>>>,
50 pub main_camera: Option<Arc<RwLock<dyn Camera>>>,
51}
52
53impl Scene {
54 const NAME: &'static str = "Scene";
55
56 pub fn new(worldspace: Option<Space>) -> Arc<RwLock<Self>>{
65 Arc::new(RwLock::new(Scene{
66 worldspace: worldspace.unwrap_or_else(|| Space::new(None, None, None)),
67 children: Vec::new(),
68 main_camera: None,
69 }))
70 }
71
72 pub fn set_main_camera(&mut self, main_camera: Arc<RwLock<impl Camera>>){
76 self.main_camera = Some(main_camera as Arc<RwLock<dyn Camera>>);
77 }
82
83 pub fn spawn_script_cores(&self, spawner: Spawner){
85 self.children.clone().into_iter().for_each(|game_object| {
86 let game_object_template = game_object.clone();
87 game_object_template.write().unwrap().spawn_script_core(game_object, spawner.clone());
88 }); }
93
94 pub fn spawn_script_handlers(&self, spawner: Spawner, event: Event<'static, UserEvent<Arc<dyn Any + Send + Sync>>>){
96 self.children.clone().into_iter().for_each(|game_object| {
97 game_object.clone().write().unwrap().spawn_script_handler(game_object, spawner.clone(), event.clone());
98 }); }
105
106 #[inline]
108 pub fn render(&self,
109 this: Arc<RwLock<Scene>>,
110 frame_system: &mut FrameSystem,
111 image_num: usize,
112 acquire_future: SwapchainAcquireFuture<Window>,
113 previous_frame_end: &mut Option<Box<dyn GpuFuture>> ) -> Box<dyn GpuFuture> {
114
115 frame_system.draw_pass_manager.clear();
116 frame_system.lighting_pass_manager.clear();
117
118 self.load_into_managers(ParentWrapper::Scene(this), &mut frame_system.draw_pass_manager, &mut frame_system.lighting_pass_manager);
119
120 let main_camera = self.main_camera.clone().expect("No camera defined");
121 frame_system.draw_pass_manager.recreate_camera_set(main_camera.clone());
122
123 let main_camera_read = main_camera.read().unwrap();
124 let future = previous_frame_end.take().unwrap().join(acquire_future);
125 let mut builder = frame_system.pass_builder(
126 future,
127 image_num,
128 main_camera_read.build_projection().inverse().transpose(),
129 main_camera_read.get_inversed_subspace()
130 );
131
132 builder.build()
133 }
134
135 #[inline]
136 pub fn build_space(&mut self) -> Matrix4<f32>{
137 self.worldspace.build()
138 }
139}
140
141impl Named for Scene {
142 fn get_name(&self) -> &str { Scene::NAME }
143}