Improve Scene & SceneManager system
- Implement AsRef and AsMut for Scene - Add "new" method to SceneManager
This commit is contained in:
parent
8ffa01ad48
commit
147a9d3a85
1 changed files with 44 additions and 11 deletions
|
|
@ -1,30 +1,63 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub struct Scene {
|
pub struct Scene {
|
||||||
title: String,
|
title: String,
|
||||||
world: hecs::World,
|
world: hecs::World,
|
||||||
skybox_texture_path: String,
|
skybox_texture_path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AsRef<Scene> for Scene {
|
||||||
|
fn as_ref(&self) -> &Scene {
|
||||||
|
&self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsMut<Scene> for Scene {
|
||||||
|
fn as_mut(&mut self) -> &mut Scene {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type SceneID = String;
|
||||||
|
|
||||||
pub struct SceneManager {
|
pub struct SceneManager {
|
||||||
scenes: Vec<Option<Scene>>,
|
scenes: HashMap<SceneID, Scene>,
|
||||||
active_index: usize,
|
active_scene: Option<SceneID>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SceneManager {
|
impl SceneManager {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let scenes = HashMap::new();
|
||||||
|
Self {
|
||||||
|
scenes,
|
||||||
|
active_scene: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn current(&self) -> &Scene {
|
pub fn current(&self) -> &Scene {
|
||||||
self.scenes[self.active_index].as_ref().unwrap()
|
match &self.active_scene {
|
||||||
|
Some(id) => self.scenes[id].as_ref(),
|
||||||
|
None => panic!("No active scene"),
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_mut(&mut self) -> &mut Scene {
|
pub fn current_mut(&mut self) -> &mut Scene {
|
||||||
self.scenes[self.active_index].as_mut().unwrap()
|
match &mut self.active_scene {
|
||||||
|
Some(id) => self.scenes.get_mut(id.as_mut()).unwrap().as_mut(),
|
||||||
|
None => panic!("No active scene"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_scene(&mut self, scene: Scene) -> usize {
|
pub fn set_active_scene(&mut self, scene: SceneID) {
|
||||||
let idx = self.scenes.len();
|
self.active_scene = Some(scene);
|
||||||
self.scenes.push(Some(scene));
|
|
||||||
idx
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove_scene(&mut self, idx: usize) {
|
pub fn add_scene(&mut self, id: SceneID, scene: Scene) {
|
||||||
self.scenes[idx] = None;
|
self.scenes.insert(id, scene);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_scene(&mut self, id: SceneID) {
|
||||||
|
self.scenes.remove(&id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue