Move World into dedicated Scene struct

This commit is contained in:
Florian RICHER 2025-06-11 22:24:28 +02:00
parent 8ce620a74b
commit c494574389
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
6 changed files with 73 additions and 28 deletions

View file

@ -9,10 +9,10 @@ use crate::core::render::primitives::vulkan_resource::{
};
use bevy_ecs::world::World;
use super::Scene;
use super::{AsScene, Scene};
pub struct SceneManager {
scenes: Vec<(Box<dyn Scene>, World)>,
scenes: Vec<Scene>,
current_scene_index: Option<usize>,
window_context: Option<Rc<RefCell<WindowContext>>>,
}
@ -92,25 +92,30 @@ impl SceneManager {
world
}
pub fn load_scene(&mut self, scene: Box<dyn Scene>, window_context: &WindowContext) {
pub fn load_scene(&mut self, scene_impl: Box<dyn AsScene>, window_context: &WindowContext) {
let world = Self::create_world_with_resources(window_context);
self.scenes.push((scene, world));
let scene = Scene::new(scene_impl, world);
self.scenes.push(scene);
self.current_scene_index = Some(self.scenes.len() - 1);
}
pub fn replace_current_scene(&mut self, scene: Box<dyn Scene>, window_context: &WindowContext) {
pub fn replace_current_scene(
&mut self,
scene_impl: Box<dyn AsScene>,
window_context: &WindowContext,
) {
if let Some(index) = self.current_scene_index {
if index < self.scenes.len() {
self.scenes[index].0.unload();
self.scenes[index].unload();
let world = Self::create_world_with_resources(window_context);
self.scenes[index] = (scene, world);
self.scenes[index] = Scene::new(scene_impl, world);
}
} else {
self.load_scene(scene, window_context);
self.load_scene(scene_impl, window_context);
}
}
pub fn current_scene(&self) -> Option<&(Box<dyn Scene>, World)> {
pub fn current_scene(&self) -> Option<&Scene> {
if let Some(index) = self.current_scene_index {
self.scenes.get(index)
} else {
@ -118,7 +123,7 @@ impl SceneManager {
}
}
pub fn current_scene_mut(&mut self) -> Option<&mut (Box<dyn Scene>, World)> {
pub fn current_scene_mut(&mut self) -> Option<&mut Scene> {
if let Some(index) = self.current_scene_index {
self.scenes.get_mut(index)
} else {
@ -130,9 +135,9 @@ impl SceneManager {
&mut self,
window_context: &mut WindowContext,
) -> Result<(), Box<dyn Error>> {
if let Some((scene, world)) = self.current_scene_mut() {
if let Some(scene) = self.current_scene_mut() {
if !scene.loaded() {
scene.load(world, window_context)?;
scene.load(window_context)?;
}
} else {
tracing::warn!("No scene found in SceneManager!");